Python fixes; starting adding support for using item_handlers in python
This commit is contained in:
parent
3d4259e6d0
commit
72d69d3ec9
6 changed files with 170 additions and 2 deletions
20
format.cc
20
format.cc
|
|
@ -431,3 +431,23 @@ bool format_account::display_account(const account_t& account,
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
||||||
|
#ifdef USE_BOOST_PYTHON
|
||||||
|
|
||||||
|
#include <boost/python.hpp>
|
||||||
|
|
||||||
|
using namespace boost::python;
|
||||||
|
using namespace ledger;
|
||||||
|
|
||||||
|
void export_format()
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
class_< format_transactions > ("FormatTransactions")
|
||||||
|
.def(init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &format_transactions::flush)
|
||||||
|
.def("__call__", &format_transactions::operator());
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // USE_BOOST_PYTHON
|
||||||
|
|
|
||||||
|
|
@ -397,6 +397,11 @@ using namespace ledger;
|
||||||
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(journal_find_account_overloads,
|
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(journal_find_account_overloads,
|
||||||
find_account, 1, 2)
|
find_account, 1, 2)
|
||||||
|
|
||||||
|
entry_t& transaction_entry(const transaction_t& xact)
|
||||||
|
{
|
||||||
|
return *xact.entry;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int transactions_len(entry_t& entry)
|
unsigned int transactions_len(entry_t& entry)
|
||||||
{
|
{
|
||||||
return entry.transactions.size();
|
return entry.transactions.size();
|
||||||
|
|
@ -504,7 +509,9 @@ void export_journal()
|
||||||
class_< transaction_t > ("Transaction")
|
class_< transaction_t > ("Transaction")
|
||||||
.def(init<account_t *, amount_t, optional<unsigned int, std::string> >())
|
.def(init<account_t *, amount_t, optional<unsigned int, std::string> >())
|
||||||
|
|
||||||
.def_readwrite("entry", &transaction_t::entry)
|
.add_property("entry",
|
||||||
|
make_getter(&transaction_t::entry,
|
||||||
|
return_value_policy<reference_existing_object>()))
|
||||||
.def_readwrite("account", &transaction_t::account)
|
.def_readwrite("account", &transaction_t::account)
|
||||||
.def_readwrite("amount", &transaction_t::amount)
|
.def_readwrite("amount", &transaction_t::amount)
|
||||||
.def_readwrite("cost", &transaction_t::cost)
|
.def_readwrite("cost", &transaction_t::cost)
|
||||||
|
|
|
||||||
7
ledger.h
7
ledger.h
|
|
@ -62,6 +62,13 @@ class transaction_t
|
||||||
delete cost;
|
delete cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_BOOST_PYTHON
|
||||||
|
entry_t& get_entry() const {
|
||||||
|
assert(entry);
|
||||||
|
return *entry;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool valid() const;
|
bool valid() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
10
main.py
10
main.py
|
|
@ -19,5 +19,13 @@ register_parser (parser)
|
||||||
journal = Journal ()
|
journal = Journal ()
|
||||||
parse_journal_file (args[0], journal)
|
parse_journal_file (args[0], journal)
|
||||||
|
|
||||||
|
class OutputTransaction (TransactionHandler):
|
||||||
|
def __call__ (self, xact):
|
||||||
|
print xact.entry.payee
|
||||||
|
|
||||||
|
handler = OutputTransaction()
|
||||||
|
chain = FilterTransactions (handler, "/Checking/")
|
||||||
|
|
||||||
for entry in journal:
|
for entry in journal:
|
||||||
print entry.payee
|
for xact in entry:
|
||||||
|
chain (xact)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ void export_qif();
|
||||||
void export_gnucash();
|
void export_gnucash();
|
||||||
#endif
|
#endif
|
||||||
void export_option();
|
void export_option();
|
||||||
|
void export_walk();
|
||||||
|
void export_format();
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE(ledger) {
|
BOOST_PYTHON_MODULE(ledger) {
|
||||||
export_amount();
|
export_amount();
|
||||||
|
|
@ -31,4 +33,6 @@ BOOST_PYTHON_MODULE(ledger) {
|
||||||
export_gnucash();
|
export_gnucash();
|
||||||
#endif
|
#endif
|
||||||
export_option();
|
export_option();
|
||||||
|
export_walk();
|
||||||
|
export_format();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
122
walk.cc
122
walk.cc
|
|
@ -300,3 +300,125 @@ void dow_transactions::flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
||||||
|
#ifdef USE_BOOST_PYTHON
|
||||||
|
|
||||||
|
#include <boost/python.hpp>
|
||||||
|
|
||||||
|
using namespace boost::python;
|
||||||
|
using namespace ledger;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct item_handler_wrap : public item_handler<T>
|
||||||
|
{
|
||||||
|
PyObject* self;
|
||||||
|
item_handler_wrap(PyObject * self_) : self(self_) {}
|
||||||
|
item_handler_wrap(PyObject * self_, const item_handler<T>& handler)
|
||||||
|
: item_handler<T>(const_cast<item_handler<T> *>(&handler)), self(self_) {}
|
||||||
|
|
||||||
|
void flush() {
|
||||||
|
call_method<void>(self, "flush");
|
||||||
|
}
|
||||||
|
void default_flush() {
|
||||||
|
item_handler<T>::flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(T& item) {
|
||||||
|
call_method<void>(self, "__call__", item);
|
||||||
|
}
|
||||||
|
void default_call(T& item) {
|
||||||
|
item_handler<T>::operator()(item);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void (subtotal_transactions::*subtotal_transactions_flush)() =
|
||||||
|
&subtotal_transactions::flush;
|
||||||
|
|
||||||
|
void export_walk()
|
||||||
|
{
|
||||||
|
class_< item_handler<transaction_t>,
|
||||||
|
item_handler_wrap<transaction_t> > ("TransactionHandler")
|
||||||
|
.def(init<item_handler<transaction_t> *>())
|
||||||
|
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush,
|
||||||
|
&item_handler_wrap<transaction_t>::default_flush)
|
||||||
|
.def("__call__", &item_handler<transaction_t>::operator(),
|
||||||
|
&item_handler_wrap<transaction_t>::default_call)
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< ignore_transactions > ("IgnoreTransactions")
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &ignore_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< clear_transaction_data > ("ClearTransactionData")
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &clear_transaction_data::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< set_account_value >
|
||||||
|
("SetAccountValue", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &set_account_value::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
class_< sort_transactions >
|
||||||
|
("SortTransactions", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &sort_transactions::flush)
|
||||||
|
.def("__call__", &sort_transactions::operator());
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class_< filter_transactions >
|
||||||
|
("FilterTransactions", init<item_handler<transaction_t> *, std::string>())
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &filter_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< calc_transactions >
|
||||||
|
("CalcTransactions", init<item_handler<transaction_t> *, optional<bool> >())
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &calc_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< collapse_transactions >
|
||||||
|
("CollapseTransactions", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &collapse_transactions::flush)
|
||||||
|
.def("__call__", &collapse_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< changed_value_transactions >
|
||||||
|
("ChangeValueTransactions", init<item_handler<transaction_t> *, bool>())
|
||||||
|
.def("flush", &changed_value_transactions::flush)
|
||||||
|
.def("__call__", &changed_value_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< subtotal_transactions >
|
||||||
|
("SubtotalTransactions", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", subtotal_transactions_flush)
|
||||||
|
.def("__call__", &subtotal_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
class_< interval_transactions >
|
||||||
|
("IntervalTransactions", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &interval_transactions::operator());
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class_< dow_transactions >
|
||||||
|
("DowTransactions", init<item_handler<transaction_t> *>())
|
||||||
|
.def("flush", &dow_transactions::flush)
|
||||||
|
.def("__call__", &dow_transactions::operator());
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< related_transactions >
|
||||||
|
("RelatedTransactions", init<item_handler<transaction_t> *, optional<bool> >())
|
||||||
|
.def("flush", &item_handler<transaction_t>::flush)
|
||||||
|
.def("__call__", &related_transactions::operator());
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // USE_BOOST_PYTHON
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue