added python support for Format() objects. see main.py
This commit is contained in:
parent
e1d6c4bff2
commit
435b9ed779
4 changed files with 27 additions and 18 deletions
22
format.cc
22
format.cc
|
|
@ -185,8 +185,7 @@ element_t * format_t::parse_elements(const std::string& fmt)
|
||||||
return result.release();
|
return result.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
void format_t::format_elements(std::ostream& out,
|
void format_t::format(std::ostream& out, const details_t& details) const
|
||||||
const details_t& details) const
|
|
||||||
{
|
{
|
||||||
for (const element_t * elem = elements; elem; elem = elem->next) {
|
for (const element_t * elem = elements; elem; elem = elem->next) {
|
||||||
if (elem->align_left)
|
if (elem->align_left)
|
||||||
|
|
@ -439,15 +438,22 @@ bool format_account::display_account(const account_t& account,
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
using namespace ledger;
|
using namespace ledger;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string py_format(format_t& format, T& item) {
|
||||||
|
std::ostringstream out;
|
||||||
|
format.format(out, details_t(item));
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
void export_format()
|
void export_format()
|
||||||
{
|
{
|
||||||
#if 0
|
class_< format_t > ("Format")
|
||||||
class_< format_transactions > ("FormatTransactions")
|
.def(init<std::string>())
|
||||||
.def(init<item_handler<transaction_t> *>())
|
.def("reset", &format_t::reset)
|
||||||
.def("flush", &format_transactions::flush)
|
.def("format", py_format<account_t>)
|
||||||
.def("__call__", &format_transactions::operator());
|
.def("format", py_format<entry_t>)
|
||||||
|
.def("format", py_format<transaction_t>)
|
||||||
;
|
;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // USE_BOOST_PYTHON
|
#endif // USE_BOOST_PYTHON
|
||||||
|
|
|
||||||
14
format.h
14
format.h
|
|
@ -73,7 +73,7 @@ struct format_t
|
||||||
|
|
||||||
static element_t * parse_elements(const std::string& fmt);
|
static element_t * parse_elements(const std::string& fmt);
|
||||||
|
|
||||||
void format_elements(std::ostream& out, const details_t& details) const;
|
void format(std::ostream& out, const details_t& details) const;
|
||||||
|
|
||||||
static void compute_value(value_t& result, const details_t& details) {
|
static void compute_value(value_t& result, const details_t& details) {
|
||||||
if (value_expr)
|
if (value_expr)
|
||||||
|
|
@ -109,10 +109,10 @@ class format_transactions : public item_handler<transaction_t>
|
||||||
if (! xact.data ||
|
if (! xact.data ||
|
||||||
! (XACT_DATA_(xact)->dflags & TRANSACTION_DISPLAYED)) {
|
! (XACT_DATA_(xact)->dflags & TRANSACTION_DISPLAYED)) {
|
||||||
if (last_entry != xact.entry) {
|
if (last_entry != xact.entry) {
|
||||||
first_line_format.format_elements(output_stream, details_t(xact));
|
first_line_format.format(output_stream, details_t(xact));
|
||||||
last_entry = xact.entry;
|
last_entry = xact.entry;
|
||||||
} else {
|
} else {
|
||||||
next_lines_format.format_elements(output_stream, details_t(xact));
|
next_lines_format.format(output_stream, details_t(xact));
|
||||||
}
|
}
|
||||||
if (! xact.data)
|
if (! xact.data)
|
||||||
xact.data = new transaction_data_t;
|
xact.data = new transaction_data_t;
|
||||||
|
|
@ -157,7 +157,7 @@ class format_account : public item_handler<account_t>
|
||||||
account.data = new account_data_t;
|
account.data = new account_data_t;
|
||||||
ACCT_DATA_(account)->dflags |= ACCOUNT_TO_DISPLAY;
|
ACCT_DATA_(account)->dflags |= ACCOUNT_TO_DISPLAY;
|
||||||
} else {
|
} else {
|
||||||
format.format_elements(output_stream, details_t(account));
|
format.format(output_stream, details_t(account));
|
||||||
if (! account.data)
|
if (! account.data)
|
||||||
account.data = new account_data_t;
|
account.data = new account_data_t;
|
||||||
ACCT_DATA_(account)->dflags |= ACCOUNT_DISPLAYED;
|
ACCT_DATA_(account)->dflags |= ACCOUNT_DISPLAYED;
|
||||||
|
|
@ -188,7 +188,7 @@ class format_equity : public item_handler<account_t>
|
||||||
entry_t header_entry;
|
entry_t header_entry;
|
||||||
header_entry.payee = "Opening Balances";
|
header_entry.payee = "Opening Balances";
|
||||||
header_entry.date = std::time(NULL);
|
header_entry.date = std::time(NULL);
|
||||||
first_line_format.format_elements(output_stream, details_t(header_entry));
|
first_line_format.format(output_stream, details_t(header_entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void flush() {
|
virtual void flush() {
|
||||||
|
|
@ -197,13 +197,13 @@ class format_equity : public item_handler<account_t>
|
||||||
summary.data = acct_data.get();
|
summary.data = acct_data.get();
|
||||||
((account_data_t *) summary.data)->value = total;
|
((account_data_t *) summary.data)->value = total;
|
||||||
((account_data_t *) summary.data)->value.negate();
|
((account_data_t *) summary.data)->value.negate();
|
||||||
next_lines_format.format_elements(output_stream, details_t(summary));
|
next_lines_format.format(output_stream, details_t(summary));
|
||||||
output_stream.flush();
|
output_stream.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void operator()(account_t& account) {
|
virtual void operator()(account_t& account) {
|
||||||
if (format_account::display_account(account, disp_pred)) {
|
if (format_account::display_account(account, disp_pred)) {
|
||||||
next_lines_format.format_elements(output_stream, details_t(account));
|
next_lines_format.format(output_stream, details_t(account));
|
||||||
if (! account.data)
|
if (! account.data)
|
||||||
account.data = new account_data_t;
|
account.data = new account_data_t;
|
||||||
else
|
else
|
||||||
|
|
|
||||||
2
main.cc
2
main.cc
|
|
@ -347,7 +347,7 @@ int parse_and_report(int argc, char * argv[], char * envp[])
|
||||||
|
|
||||||
if (ACCT_DATA(journal->master)->dflags & ACCOUNT_TO_DISPLAY) {
|
if (ACCT_DATA(journal->master)->dflags & ACCOUNT_TO_DISPLAY) {
|
||||||
out << "--------------------\n";
|
out << "--------------------\n";
|
||||||
config.format.format_elements(out, details_t(*journal->master));
|
config.format.format(out, details_t(*journal->master));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
main.py
7
main.py
|
|
@ -21,8 +21,11 @@ journal = Journal ()
|
||||||
parse_journal_file (args[0], journal)
|
parse_journal_file (args[0], journal)
|
||||||
|
|
||||||
class OutputTransaction (TransactionHandler):
|
class OutputTransaction (TransactionHandler):
|
||||||
|
def __init__ (self):
|
||||||
|
self.formatter = Format ("%D %-20P %N")
|
||||||
|
TransactionHandler.__init__ (self)
|
||||||
def __call__ (self, xact):
|
def __call__ (self, xact):
|
||||||
print xact.entry.payee
|
print self.formatter.format(xact)
|
||||||
|
|
||||||
handler = OutputTransaction()
|
handler = OutputTransaction()
|
||||||
handler = FilterTransactions (handler, "/Checking/")
|
handler = FilterTransactions (handler, "/Checking/")
|
||||||
|
|
@ -31,6 +34,6 @@ for entry in journal:
|
||||||
for xact in entry:
|
for xact in entry:
|
||||||
handler (xact)
|
handler (xact)
|
||||||
|
|
||||||
span = Interval ("monthly last year")
|
span = Interval ("weekly last month")
|
||||||
for date in span:
|
for date in span:
|
||||||
print time.strftime ("%c", time.localtime (date))
|
print time.strftime ("%c", time.localtime (date))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue