From 528019ce253fb8f0aff77516e3e900048257a00a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 24 Sep 2004 07:00:52 -0400 Subject: added support for "equity" in main.py --- journal.cc | 12 +++++++++++- main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ value.cc | 2 ++ value.h | 12 ++++++++++-- walk.cc | 12 ++++++++++++ walk.h | 9 +-------- 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/journal.cc b/journal.cc index 01740d13..a949d7a4 100644 --- a/journal.cc +++ b/journal.cc @@ -569,6 +569,16 @@ account_t * py_find_account_2(journal_t& journal, const std::string& name, return journal.find_account(name, auto_create); } +PyObject * py_account_get_data(account_t& account) +{ + return (PyObject *) account.data; +} + +void py_account_set_data(account_t& account, PyObject * obj) +{ + account.data = obj; +} + struct py_entry_finalizer_t : public entry_finalizer_t { object pyobj; py_entry_finalizer_t() {} @@ -656,7 +666,7 @@ void export_journal() .def_readwrite("note", &account_t::note) .def_readonly("depth", &account_t::depth) .def_readonly("transactions", &account_t::transactions) - .def_readwrite("data", &account_t::data) + .add_property("data", py_account_get_data, py_account_set_data) .def_readonly("ident", &account_t::ident) .def(self_ns::str(self)) diff --git a/main.py b/main.py index 2f6690d5..ab7bca1e 100755 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ import os import sys import string +import time true, false = 1, 0 @@ -243,6 +244,50 @@ class FormatAccount (AccountHandler): self.output.write(self.formatter.format(account)) account_xdata (account).dflags |= ACCOUNT_DISPLAYED +class FormatEquity (AccountHandler): + output = None + + def __init__ (self, fmt, pred): + try: + i = string.index (fmt, '%/') + self.formatter = Format (fmt[: i]) + self.nformatter = Format (fmt[i + 2 :]) + except ValueError: + self.formatter = Format (fmt) + self.nformatter = None + + self.predicate = AccountPredicate (pred) + self.total = Value () + + if config.output_file: + self.output = open(config.output_file, "w") + else: + self.output = sys.stdout + + AccountHandler.__init__ (self) + + header_entry = Entry () + header_entry.payee = "Opening Balances" + header_entry.date = int(time.time()) + self.output.write (self.formatter.format (header_entry)) + + def __del__ (self): + if config.output_file: + self.output.close () + + def flush (self): + summary = Account(Account (), "Equity:Opening Balances") + account_xdata (summary).value = - self.total + self.output.write (self.nformatter.format (summary)) + self.output.flush () + + def __call__ (self, account): + if display_account (account, self.predicate): + self.output.write(self.nformatter.format (account)) + if account_has_xdata (account): + self.total += account_xdata (account).value + account_xdata (account).dflags |= ACCOUNT_DISPLAYED + # Set the final transaction handler: for balances and equity reports, # it will simply add the value of the transaction to the account's # xdata, which is used a bit later to report those totals. For all @@ -332,12 +377,11 @@ if command == "b": # print "--------------------" # config.format.format(out, details_t(*journal->master)); -#elif command == "E": -# format_equity acct_formatter(out, config.format, config.nformat, -# config.display_predicate); -# sum_accounts(*journal->master); -# walk_accounts(*journal->master, acct_formatter, config.sort_string); -# acct_formatter.flush(); +elif command == "E": + acct_formatter = FormatEquity (format, config.display_predicate) + sum_accounts (journal.master) + walk_accounts (journal.master, acct_formatter, config.sort_string) + acct_formatter.flush () # If the cache is being used, and is dirty, update it now. @@ -345,3 +389,6 @@ if config.use_cache and config.cache_dirty and config.cache_file: write_binary_journal(config.cache_file, journal); # We're done! + +clear_transactions_xdata () +clear_accounts_xdata () diff --git a/value.cc b/value.cc index 4aa46def..31081ef6 100644 --- a/value.cc +++ b/value.cc @@ -707,6 +707,8 @@ void export_value() .def(other() / self) .def(int() / self) + .def(- self) + .def(self += self) .def(self += other()) .def(self += other()) diff --git a/value.h b/value.h index 48b45248..61e20eaa 100644 --- a/value.h +++ b/value.h @@ -242,10 +242,18 @@ class value_t template operator T() const; - void cast(type_t cast_type); void negate(); - void abs(); + value_t negated() const { + value_t temp = *this; + temp.negate(); + return temp; + } + value_t operator-() const { + return negated(); + } + void abs(); + void cast(type_t cast_type); value_t cost() const; }; diff --git a/walk.cc b/walk.cc index 8b528141..fa27d6bc 100644 --- a/walk.cc +++ b/walk.cc @@ -309,6 +309,16 @@ void dow_transactions::flush() } } +void clear_transactions_xdata() +{ + transactions_xdata.clear(); + + for (std::list::iterator i = transactions_xdata_ptrs.begin(); + i != transactions_xdata_ptrs.end(); + i++) + **i = NULL; +} + void sum_accounts(account_t& account) { for (accounts_map::iterator i = account.accounts.begin(); @@ -469,6 +479,7 @@ void export_walk() def("transaction_has_xdata", transaction_has_xdata); def("transaction_xdata", transaction_xdata, return_internal_reference<1>()); + def("clear_transactions_xdata", clear_transactions_xdata); class_< xact_handler_t, item_handler_wrap > ("TransactionHandler") @@ -579,6 +590,7 @@ void export_walk() def("account_has_xdata", account_has_xdata); def("account_xdata", account_xdata, return_internal_reference<1>()); + def("clear_accounts_xdata", clear_accounts_xdata); class_< account_handler_t, item_handler_wrap > ("AccountHandler") .def(init()) diff --git a/walk.h b/walk.h index 0c343fd3..42c4ff44 100644 --- a/walk.h +++ b/walk.h @@ -143,14 +143,7 @@ inline void walk_entries(entries_list& list, walk_entries(list.begin(), list.end(), handler); } -inline void clear_transactions_xdata() { - transactions_xdata.clear(); - - for (std::list::iterator i = transactions_xdata_ptrs.begin(); - i != transactions_xdata_ptrs.end(); - i++) - **i = NULL; -} +void clear_transactions_xdata(); ////////////////////////////////////////////////////////////////////// -- cgit v1.2.3