The --anon option now anonymizes commodities
Fixes #227 / 1C90D8AF-830E-43C2-A5B7-D382F68EBDE3
This commit is contained in:
parent
8637dd6ccf
commit
c28fad384c
4 changed files with 72 additions and 14 deletions
|
|
@ -36,6 +36,7 @@
|
||||||
#include "journal.h"
|
#include "journal.h"
|
||||||
#include "report.h"
|
#include "report.h"
|
||||||
#include "compare.h"
|
#include "compare.h"
|
||||||
|
#include "pool.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
@ -216,6 +217,43 @@ namespace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void anonymize_posts::render_commodity(amount_t& amt)
|
||||||
|
{
|
||||||
|
commodity_t& comm(amt.commodity());
|
||||||
|
|
||||||
|
std::size_t id;
|
||||||
|
bool newly_added = false;
|
||||||
|
|
||||||
|
commodity_index_map::iterator i = comms.find(&comm);
|
||||||
|
if (i == comms.end()) {
|
||||||
|
id = next_comm_id++;
|
||||||
|
newly_added = true;
|
||||||
|
comms.insert(commodity_index_map::value_type(&comm, id));
|
||||||
|
} else {
|
||||||
|
id = (*i).second;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream buf;
|
||||||
|
do {
|
||||||
|
buf << static_cast<char>('A' + (id % 26));
|
||||||
|
id /= 26;
|
||||||
|
}
|
||||||
|
while (id > 0);
|
||||||
|
|
||||||
|
if (amt.has_annotation())
|
||||||
|
amt.set_commodity
|
||||||
|
(*commodity_pool_t::current_pool->find_or_create(buf.str(),
|
||||||
|
amt.annotation()));
|
||||||
|
else
|
||||||
|
amt.set_commodity
|
||||||
|
(*commodity_pool_t::current_pool->find_or_create(buf.str()));
|
||||||
|
|
||||||
|
if (newly_added) {
|
||||||
|
amt.commodity().set_flags(comm.flags());
|
||||||
|
amt.commodity().set_precision(comm.precision());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void anonymize_posts::operator()(post_t& post)
|
void anonymize_posts::operator()(post_t& post)
|
||||||
{
|
{
|
||||||
SHA1 sha;
|
SHA1 sha;
|
||||||
|
|
@ -258,6 +296,20 @@ void anonymize_posts::operator()(post_t& post)
|
||||||
temp.note = none;
|
temp.note = none;
|
||||||
temp.add_flags(POST_ANONYMIZED);
|
temp.add_flags(POST_ANONYMIZED);
|
||||||
|
|
||||||
|
DEBUG("foo", "1.rendering amount: " << temp.amount);
|
||||||
|
render_commodity(temp.amount);
|
||||||
|
DEBUG("foo", "2.rendering amount: " << temp.amount);
|
||||||
|
if (temp.amount.has_annotation()) {
|
||||||
|
temp.amount.annotation().tag = none;
|
||||||
|
if (temp.amount.annotation().price)
|
||||||
|
render_commodity(*temp.amount.annotation().price);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temp.cost)
|
||||||
|
render_commodity(*temp.cost);
|
||||||
|
if (temp.assigned_amount)
|
||||||
|
render_commodity(*temp.assigned_amount);
|
||||||
|
|
||||||
(*handler)(temp);
|
(*handler)(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -334,20 +334,26 @@ public:
|
||||||
|
|
||||||
class anonymize_posts : public item_handler<post_t>
|
class anonymize_posts : public item_handler<post_t>
|
||||||
{
|
{
|
||||||
|
typedef std::map<commodity_t *, std::size_t> commodity_index_map;
|
||||||
|
|
||||||
temporaries_t temps;
|
temporaries_t temps;
|
||||||
|
commodity_index_map comms;
|
||||||
|
std::size_t next_comm_id;
|
||||||
xact_t * last_xact;
|
xact_t * last_xact;
|
||||||
|
|
||||||
anonymize_posts();
|
anonymize_posts();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
anonymize_posts(post_handler_ptr handler)
|
anonymize_posts(post_handler_ptr handler)
|
||||||
: item_handler<post_t>(handler), last_xact(NULL) {
|
: item_handler<post_t>(handler), next_comm_id(0), last_xact(NULL) {
|
||||||
TRACE_CTOR(anonymize_posts, "post_handler_ptr");
|
TRACE_CTOR(anonymize_posts, "post_handler_ptr");
|
||||||
}
|
}
|
||||||
virtual ~anonymize_posts() {
|
virtual ~anonymize_posts() {
|
||||||
TRACE_DTOR(anonymize_posts);
|
TRACE_DTOR(anonymize_posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void render_commodity(amount_t& amt);
|
||||||
|
|
||||||
virtual void operator()(post_t& post);
|
virtual void operator()(post_t& post);
|
||||||
|
|
||||||
virtual void clear() {
|
virtual void clear() {
|
||||||
|
|
|
||||||
16
src/pool.cc
16
src/pool.cc
|
|
@ -57,7 +57,7 @@ commodity_t * commodity_pool_t::create(const string& symbol)
|
||||||
base_commodity(new commodity_t::base_t(symbol));
|
base_commodity(new commodity_t::base_t(symbol));
|
||||||
std::auto_ptr<commodity_t> commodity(new commodity_t(this, base_commodity));
|
std::auto_ptr<commodity_t> commodity(new commodity_t(this, base_commodity));
|
||||||
|
|
||||||
DEBUG("amounts.commodities", "Creating base commodity " << symbol);
|
DEBUG("pool.commodities", "Creating base commodity " << symbol);
|
||||||
|
|
||||||
// Create the "qualified symbol" version of this commodity's symbol
|
// Create the "qualified symbol" version of this commodity's symbol
|
||||||
if (commodity_t::symbol_needs_quotes(symbol)) {
|
if (commodity_t::symbol_needs_quotes(symbol)) {
|
||||||
|
|
@ -66,7 +66,7 @@ commodity_t * commodity_pool_t::create(const string& symbol)
|
||||||
*commodity->qualified_symbol += "\"";
|
*commodity->qualified_symbol += "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG("amounts.commodities",
|
DEBUG("pool.commodities",
|
||||||
"Creating commodity '" << commodity->symbol() << "'");
|
"Creating commodity '" << commodity->symbol() << "'");
|
||||||
|
|
||||||
std::pair<commodities_map::iterator, bool> result
|
std::pair<commodities_map::iterator, bool> result
|
||||||
|
|
@ -79,7 +79,7 @@ commodity_t * commodity_pool_t::create(const string& symbol)
|
||||||
|
|
||||||
commodity_t * commodity_pool_t::find_or_create(const string& symbol)
|
commodity_t * commodity_pool_t::find_or_create(const string& symbol)
|
||||||
{
|
{
|
||||||
DEBUG("amounts.commodities", "Find-or-create commodity " << symbol);
|
DEBUG("pool.commodities", "Find-or-create commodity " << symbol);
|
||||||
|
|
||||||
commodity_t * commodity = find(symbol);
|
commodity_t * commodity = find(symbol);
|
||||||
if (commodity)
|
if (commodity)
|
||||||
|
|
@ -89,7 +89,7 @@ commodity_t * commodity_pool_t::find_or_create(const string& symbol)
|
||||||
|
|
||||||
commodity_t * commodity_pool_t::find(const string& symbol)
|
commodity_t * commodity_pool_t::find(const string& symbol)
|
||||||
{
|
{
|
||||||
DEBUG("amounts.commodities", "Find commodity " << symbol);
|
DEBUG("pool.commodities", "Find commodity " << symbol);
|
||||||
|
|
||||||
commodities_map::const_iterator i = commodities.find(symbol);
|
commodities_map::const_iterator i = commodities.find(symbol);
|
||||||
if (i != commodities.end())
|
if (i != commodities.end())
|
||||||
|
|
@ -124,10 +124,10 @@ string commodity_pool_t::make_qualified_name(const commodity_t& comm,
|
||||||
|
|
||||||
#if defined(DEBUG_ON)
|
#if defined(DEBUG_ON)
|
||||||
if (comm.qualified_symbol)
|
if (comm.qualified_symbol)
|
||||||
DEBUG("amounts.commodities", "make_qualified_name for "
|
DEBUG("pool.commodities", "make_qualified_name for "
|
||||||
<< *comm.qualified_symbol << std::endl << details);
|
<< *comm.qualified_symbol << std::endl << details);
|
||||||
#endif
|
#endif
|
||||||
DEBUG("amounts.commodities", "qualified_name is " << name.str());
|
DEBUG("pool.commodities", "qualified_name is " << name.str());
|
||||||
|
|
||||||
return name.str();
|
return name.str();
|
||||||
}
|
}
|
||||||
|
|
@ -156,7 +156,7 @@ commodity_t *
|
||||||
commodity_pool_t::find_or_create(const string& symbol,
|
commodity_pool_t::find_or_create(const string& symbol,
|
||||||
const annotation_t& details)
|
const annotation_t& details)
|
||||||
{
|
{
|
||||||
commodity_t * comm = find(symbol);
|
commodity_t * comm = find_or_create(symbol);
|
||||||
if (! comm)
|
if (! comm)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
@ -181,7 +181,7 @@ commodity_pool_t::create(commodity_t& comm,
|
||||||
commodity->qualified_symbol = comm.symbol();
|
commodity->qualified_symbol = comm.symbol();
|
||||||
assert(! commodity->qualified_symbol->empty());
|
assert(! commodity->qualified_symbol->empty());
|
||||||
|
|
||||||
DEBUG("amounts.commodities", "Creating annotated commodity "
|
DEBUG("pool.commodities", "Creating annotated commodity "
|
||||||
<< "symbol " << commodity->symbol()
|
<< "symbol " << commodity->symbol()
|
||||||
<< " key " << mapping_key << std::endl << details);
|
<< " key " << mapping_key << std::endl << details);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ reg --anon
|
||||||
Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00
|
Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00
|
||||||
Income:Dividends:Vanguard:VMMXX $-0.35
|
Income:Dividends:Vanguard:VMMXX $-0.35
|
||||||
>>>1
|
>>>1
|
||||||
07-Feb-02 6a93dcb3 da:20:5d:27:988a9c3a 0.350 VMMXX 0.350 VMMXX
|
07-Feb-02 6a93dcb3 da:20:5d:27:988a9c3a 0.350 A 0.350 A
|
||||||
da:1c:b6:27:988a9c3a $-0.35 $-0.35
|
da:1c:b6:27:988a9c3a B-0.35 0.350 A
|
||||||
0.350 VMMXX
|
B-0.35
|
||||||
>>>2
|
>>>2
|
||||||
=== 0
|
=== 0
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue