added support for ValueExpr objects to python.
This commit is contained in:
parent
435b9ed779
commit
ff4589caae
5 changed files with 50 additions and 8 deletions
11
format.cc
11
format.cc
|
|
@ -14,11 +14,11 @@ std::string truncated(const std::string& str, unsigned int width)
|
|||
return buf;
|
||||
}
|
||||
|
||||
std::string partial_account_name(const account_t * account)
|
||||
std::string partial_account_name(const account_t& account)
|
||||
{
|
||||
std::string name;
|
||||
|
||||
for (const account_t * acct = account;
|
||||
for (const account_t * acct = &account;
|
||||
acct && acct->parent;
|
||||
acct = acct->parent) {
|
||||
if (acct->data && ACCT_DATA(acct)->dflags & ACCOUNT_DISPLAYED)
|
||||
|
|
@ -284,7 +284,7 @@ void format_t::format(std::ostream& out, const details_t& details) const
|
|||
if (details.account) {
|
||||
std::string name = (elem->type == element_t::ACCOUNT_FULLNAME ?
|
||||
details.account->fullname() :
|
||||
partial_account_name(details.account));
|
||||
partial_account_name(*details.account));
|
||||
|
||||
if (details.xact && details.xact->flags & TRANSACTION_VIRTUAL) {
|
||||
if (elem->max_width > 2)
|
||||
|
|
@ -454,6 +454,11 @@ void export_format()
|
|||
.def("format", py_format<entry_t>)
|
||||
.def("format", py_format<transaction_t>)
|
||||
;
|
||||
|
||||
def("truncated", truncated);
|
||||
#if 0
|
||||
def("partial_account_name", partial_account_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // USE_BOOST_PYTHON
|
||||
|
|
|
|||
2
format.h
2
format.h
|
|
@ -9,7 +9,7 @@ namespace ledger {
|
|||
|
||||
std::string truncated(const std::string& str, unsigned int width);
|
||||
|
||||
std::string partial_account_name(const account_t * account,
|
||||
std::string partial_account_name(const account_t& account,
|
||||
const unsigned int start_depth);
|
||||
|
||||
struct element_t
|
||||
|
|
|
|||
11
main.py
11
main.py
|
|
@ -20,16 +20,19 @@ register_parser (parser)
|
|||
journal = Journal ()
|
||||
parse_journal_file (args[0], journal)
|
||||
|
||||
class OutputTransaction (TransactionHandler):
|
||||
def __init__ (self):
|
||||
self.formatter = Format ("%D %-20P %N")
|
||||
class FormatTransaction (TransactionHandler):
|
||||
def __init__ (self, fmt):
|
||||
self.formatter = Format (fmt)
|
||||
TransactionHandler.__init__ (self)
|
||||
|
||||
def __call__ (self, xact):
|
||||
print self.formatter.format(xact)
|
||||
|
||||
handler = OutputTransaction()
|
||||
handler = FormatTransaction("%D %-20P %N")
|
||||
handler = FilterTransactions (handler, "/Checking/")
|
||||
|
||||
expr = parse_value_expr ("a*2")
|
||||
|
||||
for entry in journal:
|
||||
for xact in entry:
|
||||
handler (xact)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ void export_gnucash();
|
|||
void export_option();
|
||||
void export_walk();
|
||||
void export_format();
|
||||
void export_valexpr();
|
||||
void export_datetime();
|
||||
|
||||
BOOST_PYTHON_MODULE(ledger) {
|
||||
|
|
@ -36,5 +37,6 @@ BOOST_PYTHON_MODULE(ledger) {
|
|||
export_option();
|
||||
export_walk();
|
||||
export_format();
|
||||
export_valexpr();
|
||||
export_datetime();
|
||||
}
|
||||
|
|
|
|||
32
valexpr.cc
32
valexpr.cc
|
|
@ -854,6 +854,38 @@ void dump_value_expr(std::ostream& out, const value_expr_t * node)
|
|||
|
||||
} // namespace ledger
|
||||
|
||||
#ifdef USE_BOOST_PYTHON
|
||||
|
||||
#include <boost/python.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
using namespace ledger;
|
||||
|
||||
template <typename T>
|
||||
value_t py_compute(value_expr_t& value_expr, const T& item) {
|
||||
value_t result;
|
||||
value_expr.compute(result, details_t(item));
|
||||
return result;
|
||||
}
|
||||
|
||||
value_expr_t * py_parse_value_expr(const std::string& str) {
|
||||
return parse_value_expr(str);
|
||||
}
|
||||
|
||||
void export_valexpr()
|
||||
{
|
||||
class_< value_expr_t > ("ValueExpr", init<value_expr_t::kind_t>())
|
||||
.def("compute", py_compute<account_t>)
|
||||
.def("compute", py_compute<entry_t>)
|
||||
.def("compute", py_compute<transaction_t>)
|
||||
;
|
||||
|
||||
def("parse_value_expr", py_parse_value_expr,
|
||||
return_value_policy<manage_new_object>());
|
||||
}
|
||||
|
||||
#endif // USE_BOOST_PYTHON
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue