From af571c104790b0703d94b0f94b949041cf4f5c1d Mon Sep 17 00:00:00 2001 From: ELIPTUS Date: Wed, 12 Nov 2014 22:51:56 -0800 Subject: Improve Embedded Python Support Discussion: https://groups.google.com/d/msg/ledger-cli/kq9sKoRX9vw/fmyhvPslzokJ --- src/py_account.cc | 27 +++++++++++++++++++++++++-- src/py_balance.cc | 2 +- src/py_session.cc | 2 ++ src/session.cc | 5 +++++ src/session.h | 2 ++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/py_account.cc b/src/py_account.cc index 3054512e..7b8eda8c 100644 --- a/src/py_account.cc +++ b/src/py_account.cc @@ -35,6 +35,7 @@ #include "pyutils.h" #include "account.h" #include "post.h" +#include "expr.h" namespace ledger { @@ -97,6 +98,26 @@ namespace { return str_to_py_unicode(account.fullname()); } + value_t py_amount_0(const account_t& account) + { + return account.amount(); + } + + value_t py_amount_1(const account_t& account, const boost::optional& expr) + { + return account.amount(expr); + } + + value_t py_total_0(const account_t& account) + { + return account.total(); + } + + value_t py_total_1(const account_t& account, const boost::optional& expr) + { + return account.total(expr); + } + } // unnamed namespace void export_account() @@ -221,8 +242,10 @@ void export_account() .def("xdata", py_xdata, return_internal_reference<>()) - .def("amount", &account_t::amount) - .def("total", &account_t::total) + .def("amount", py_amount_0) + .def("amount", py_amount_1, args("expr")) + .def("total", py_total_0) + .def("total", py_total_1, args("expr")) .def("self_details", &account_t::self_details, return_internal_reference<>()) diff --git a/src/py_balance.cc b/src/py_balance.cc index 8bf30050..dec08a51 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -64,7 +64,7 @@ namespace { boost::optional py_commodity_amount_1(const balance_t& balance, - const boost::optional& commodity) { + const commodity_t& commodity) { return balance.commodity_amount(commodity); } diff --git a/src/py_session.cc b/src/py_session.cc index 25d13cc0..5253dba3 100644 --- a/src/py_session.cc +++ b/src/py_session.cc @@ -61,6 +61,8 @@ void export_session() .def("read_journal_files", &session_t::read_journal_files, return_internal_reference<>()) .def("close_journal_files", &session_t::close_journal_files) + .def("journal", &session_t::get_journal, + return_internal_reference<>()) ; scope().attr("session") = diff --git a/src/session.cc b/src/session.cc index e373eb03..f20b4f6c 100644 --- a/src/session.cc +++ b/src/session.cc @@ -265,6 +265,11 @@ void session_t::close_journal_files() amount_t::initialize(); } +journal_t * session_t::get_journal() +{ + return journal.get(); +} + value_t session_t::fn_account(call_scope_t& args) { if (args[0].is_string()) diff --git a/src/session.h b/src/session.h index d20ba74a..88146c1e 100644 --- a/src/session.h +++ b/src/session.h @@ -84,6 +84,8 @@ public: journal_t * read_journal_files(); void close_journal_files(); + journal_t * get_journal(); + value_t fn_account(call_scope_t& scope); value_t fn_min(call_scope_t& scope); value_t fn_max(call_scope_t& scope); -- cgit v1.2.3