summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/py_balance.cc202
-rw-r--r--python/py_expr.cc79
-rw-r--r--python/py_journal.cc373
-rw-r--r--python/py_parser.cc45
4 files changed, 699 insertions, 0 deletions
diff --git a/python/py_balance.cc b/python/py_balance.cc
index 05d04441..4215519c 100644
--- a/python/py_balance.cc
+++ b/python/py_balance.cc
@@ -66,3 +66,205 @@ void export_balance()
}
} // namespace ledger
+
+#if 0
+unsigned int balance_len(balance_t& bal)
+{
+ return bal.amounts.size();
+}
+
+amount_t balance_getitem(balance_t& bal, int i)
+{
+ std::size_t len = bal.amounts.size();
+
+ if (abs(i) >= len) {
+ PyErr_SetString(PyExc_IndexError, "Index out of range");
+ throw_error_already_set();
+ }
+
+ int x = i < 0 ? len + i : i;
+ balance_t::amounts_map::iterator elem = bal.amounts.begin();
+ while (--x >= 0)
+ elem++;
+
+ return (*elem).second;
+}
+
+unsigned int balance_pair_len(balance_pair_t& bal_pair)
+{
+ return balance_len(bal_pair.quantity);
+}
+
+amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i)
+{
+ return balance_getitem(bal_pair.quantity, i);
+}
+
+void export_balance()
+{
+ class_< balance_t > ("Balance")
+ .def(init<balance_t>())
+ .def(init<amount_t>())
+ .def(init<long>())
+ .def(init<unsigned long>())
+ .def(init<double>())
+
+ .def(self += self)
+ .def(self += other<amount_t>())
+ .def(self += long())
+ .def(self + self)
+ .def(self + other<amount_t>())
+ .def(self + long())
+ .def(self -= self)
+ .def(self -= other<amount_t>())
+ .def(self -= long())
+ .def(self - self)
+ .def(self - other<amount_t>())
+ .def(self - long())
+ .def(self *= self)
+ .def(self *= other<amount_t>())
+ .def(self *= long())
+ .def(self * self)
+ .def(self * other<amount_t>())
+ .def(self * long())
+ .def(self /= self)
+ .def(self /= other<amount_t>())
+ .def(self /= long())
+ .def(self / self)
+ .def(self / other<amount_t>())
+ .def(self / long())
+ .def(- self)
+
+ .def(self < self)
+ .def(self < other<amount_t>())
+ .def(self < long())
+ .def(self <= self)
+ .def(self <= other<amount_t>())
+ .def(self <= long())
+ .def(self > self)
+ .def(self > other<amount_t>())
+ .def(self > long())
+ .def(self >= self)
+ .def(self >= other<amount_t>())
+ .def(self >= long())
+ .def(self == self)
+ .def(self == other<amount_t>())
+ .def(self == long())
+ .def(self != self)
+ .def(self != other<amount_t>())
+ .def(self != long())
+ .def(! self)
+
+ .def(self_ns::str(self))
+
+ .def("__abs__", &balance_t::abs)
+ .def("__len__", balance_len)
+ .def("__getitem__", balance_getitem)
+
+ .def("valid", &balance_t::valid)
+
+ .def("realzero", &balance_t::realzero)
+ .def("amount", &balance_t::amount)
+ .def("value", &balance_t::value)
+ .def("price", &balance_t::price)
+ .def("date", &balance_t::date)
+ .def("strip_annotations", &balance_t::strip_annotations)
+ .def("write", &balance_t::write)
+ .def("round", &balance_t::round)
+ .def("negate", &balance_t::negate)
+ .def("negated", &balance_t::negated)
+ ;
+
+ class_< balance_pair_t > ("BalancePair")
+ .def(init<balance_pair_t>())
+ .def(init<balance_t>())
+ .def(init<amount_t>())
+ .def(init<long>())
+ .def(init<unsigned long>())
+ .def(init<double>())
+
+ .def(self += self)
+ .def(self += other<balance_t>())
+ .def(self += other<amount_t>())
+ .def(self += long())
+ .def(self + self)
+ .def(self + other<balance_t>())
+ .def(self + other<amount_t>())
+ .def(self + long())
+ .def(self -= self)
+ .def(self -= other<balance_t>())
+ .def(self -= other<amount_t>())
+ .def(self -= long())
+ .def(self - self)
+ .def(self - other<balance_t>())
+ .def(self - other<amount_t>())
+ .def(self - long())
+ .def(self *= self)
+ .def(self *= other<balance_t>())
+ .def(self *= other<amount_t>())
+ .def(self *= long())
+ .def(self * self)
+ .def(self * other<balance_t>())
+ .def(self * other<amount_t>())
+ .def(self * long())
+ .def(self /= self)
+ .def(self /= other<balance_t>())
+ .def(self /= other<amount_t>())
+ .def(self /= long())
+ .def(self / self)
+ .def(self / other<balance_t>())
+ .def(self / other<amount_t>())
+ .def(self / long())
+ .def(- self)
+
+ .def(self < self)
+ .def(self < other<balance_t>())
+ .def(self < other<amount_t>())
+ .def(self < long())
+ .def(self <= self)
+ .def(self <= other<balance_t>())
+ .def(self <= other<amount_t>())
+ .def(self <= long())
+ .def(self > self)
+ .def(self > other<balance_t>())
+ .def(self > other<amount_t>())
+ .def(self > long())
+ .def(self >= self)
+ .def(self >= other<balance_t>())
+ .def(self >= other<amount_t>())
+ .def(self >= long())
+ .def(self == self)
+ .def(self == other<balance_t>())
+ .def(self == other<amount_t>())
+ .def(self == long())
+ .def(self != self)
+ .def(self != other<balance_t>())
+ .def(self != other<amount_t>())
+ .def(self != long())
+ .def(! self)
+
+ .def(self_ns::str(self))
+
+ .def("__abs__", &balance_pair_t::abs)
+ .def("__len__", balance_pair_len)
+ .def("__getitem__", balance_pair_getitem)
+
+ .def("valid", &balance_pair_t::valid)
+
+ .def("realzero", &balance_pair_t::realzero)
+ .def("amount", &balance_pair_t::amount)
+ .def("value", &balance_pair_t::value)
+ .def("price", &balance_pair_t::price)
+ .def("date", &balance_pair_t::date)
+ .def("strip_annotations", &balance_pair_t::strip_annotations)
+ .def("write", &balance_pair_t::write)
+ .def("round", &balance_pair_t::round)
+ .def("negate", &balance_pair_t::negate)
+ .def("negated", &balance_pair_t::negated)
+
+ .add_property("cost",
+ make_getter(&balance_pair_t::cost,
+ return_value_policy<reference_existing_object>()))
+ ;
+}
+#endif
diff --git a/python/py_expr.cc b/python/py_expr.cc
index e4230377..07f390af 100644
--- a/python/py_expr.cc
+++ b/python/py_expr.cc
@@ -64,3 +64,82 @@ void export_expr()
}
} // namespace ledger
+
+#if 0
+value_t py_calc_1(xpath_t::op_t& xpath_t, const details_t& item)
+{
+ value_t result;
+ xpath_t.calc(result, item);
+ return result;
+}
+
+template <typename T>
+value_t py_calc(xpath_t::op_t& xpath_t, const T& item)
+{
+ value_t result;
+ xpath_t.calc(result, details_t(item));
+ return result;
+}
+
+xpath_t::op_t * py_parse_xpath_t_1(const string& str)
+{
+ return parse_xpath_t(str);
+}
+
+#define EXC_TRANSLATOR(type) \
+ void exc_translate_ ## type(const type& err) { \
+ PyErr_SetString(PyExc_RuntimeError, err.what()); \
+ }
+
+EXC_TRANSLATOR(xpath_t_error)
+EXC_TRANSLATOR(calc_error)
+#if 0
+EXC_TRANSLATOR(mask_error)
+#endif
+
+void export_xpath()
+{
+ class_< details_t > ("Details", init<const entry_t&>())
+ .def(init<const transaction_t&>())
+ .def(init<const account_t&>())
+ .add_property("entry",
+ make_getter(&details_t::entry,
+ return_value_policy<reference_existing_object>()))
+ .add_property("xact",
+ make_getter(&details_t::xact,
+ return_value_policy<reference_existing_object>()))
+ .add_property("account",
+ make_getter(&details_t::account,
+ return_value_policy<reference_existing_object>()))
+ ;
+
+ class_< xpath_t::op_t > ("ValueExpr", init<xpath_t::op_t::kind_t>())
+ .def("calc", py_calc_1)
+ .def("calc", py_calc<account_t>)
+ .def("calc", py_calc<entry_t>)
+ .def("calc", py_calc<transaction_t>)
+ ;
+
+ def("parse_xpath_t", py_parse_xpath_t_1,
+ return_value_policy<manage_new_object>());
+
+ class_< item_predicate<transaction_t> >
+ ("TransactionPredicate", init<string>())
+ .def("__call__", &item_predicate<transaction_t>::operator())
+ ;
+
+ class_< item_predicate<account_t> >
+ ("AccountPredicate", init<string>())
+ .def("__call__", &item_predicate<account_t>::operator())
+ ;
+
+#define EXC_TRANSLATE(type) \
+ register_error_translator<type>(&exc_translate_ ## type);
+
+ EXC_TRANSLATE(xpath_t_error);
+ EXC_TRANSLATE(calc_error);
+#if 0
+ EXC_TRANSLATE(mask_error);
+#endif
+}
+#endif
diff --git a/python/py_journal.cc b/python/py_journal.cc
index 82affed4..e9832a16 100644
--- a/python/py_journal.cc
+++ b/python/py_journal.cc
@@ -66,3 +66,376 @@ void export_journal()
}
} // namespace ledger
+
+#if 0
+entry_t& transaction_entry(const transaction_t& xact)
+{
+ return *xact.entry;
+}
+
+unsigned int transactions_len(entry_base_t& entry)
+{
+ return entry.transactions.size();
+}
+
+transaction_t& transactions_getitem(entry_base_t& entry, int i)
+{
+ static int last_index = 0;
+ static entry_base_t * last_entry = NULL;
+ static transactions_list::iterator elem;
+
+ std::size_t len = entry.transactions.size();
+
+ if (abs(i) >= len) {
+ PyErr_SetString(PyExc_IndexError, "Index out of range");
+ throw_error_already_set();
+ }
+
+ if (&entry == last_entry && i == last_index + 1) {
+ last_index = i;
+ return **++elem;
+ }
+
+ int x = i < 0 ? len + i : i;
+ elem = entry.transactions.begin();
+ while (--x >= 0)
+ elem++;
+
+ last_entry = &entry;
+ last_index = i;
+
+ return **elem;
+}
+
+unsigned int entries_len(journal_t& journal)
+{
+ return journal.entries.size();
+}
+
+entry_t& entries_getitem(journal_t& journal, int i)
+{
+ static int last_index = 0;
+ static journal_t * last_journal = NULL;
+ static entries_list::iterator elem;
+
+ std::size_t len = journal.entries.size();
+
+ if (abs(i) >= len) {
+ PyErr_SetString(PyExc_IndexError, "Index out of range");
+ throw_error_already_set();
+ }
+
+ if (&journal == last_journal && i == last_index + 1) {
+ last_index = i;
+ return **++elem;
+ }
+
+ int x = i < 0 ? len + i : i;
+ elem = journal.entries.begin();
+ while (--x >= 0)
+ elem++;
+
+ last_journal = &journal;
+ last_index = i;
+
+ return **elem;
+}
+
+unsigned int accounts_len(account_t& account)
+{
+ return account.accounts.size();
+}
+
+account_t& accounts_getitem(account_t& account, int i)
+{
+ static int last_index = 0;
+ static account_t * last_account = NULL;
+ static accounts_map::iterator elem;
+
+ std::size_t len = account.accounts.size();
+
+ if (abs(i) >= len) {
+ PyErr_SetString(PyExc_IndexError, "Index out of range");
+ throw_error_already_set();
+ }
+
+ if (&account == last_account && i == last_index + 1) {
+ last_index = i;
+ return *(*++elem).second;
+ }
+
+ int x = i < 0 ? len + i : i;
+ elem = account.accounts.begin();
+ while (--x >= 0)
+ elem++;
+
+ last_account = &account;
+ last_index = i;
+
+ return *(*elem).second;
+}
+
+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;
+}
+
+account_t * py_find_account_1(journal_t& journal, const string& name)
+{
+ return journal.find_account(name);
+}
+
+account_t * py_find_account_2(journal_t& journal, const string& name,
+ const bool auto_create)
+{
+ return journal.find_account(name, auto_create);
+}
+
+bool py_add_entry(journal_t& journal, entry_t * entry) {
+ return journal.add_entry(new entry_t(*entry));
+}
+
+void py_add_transaction(entry_base_t& entry, transaction_t * xact) {
+ return entry.add_transaction(new transaction_t(*xact));
+}
+
+struct entry_base_wrap : public entry_base_t
+{
+ PyObject * self;
+ entry_base_wrap(PyObject * self_) : self(self_) {}
+
+ virtual bool valid() const {
+ return call_method<bool>(self, "valid");
+ }
+};
+
+struct py_entry_finalizer_t : public entry_finalizer_t {
+ object pyobj;
+ py_entry_finalizer_t() {}
+ py_entry_finalizer_t(object obj) : pyobj(obj) {}
+ py_entry_finalizer_t(const py_entry_finalizer_t& other)
+ : pyobj(other.pyobj) {}
+ virtual bool operator()(entry_t& entry, bool post) {
+ return call<bool>(pyobj.ptr(), entry, post);
+ }
+};
+
+std::list<py_entry_finalizer_t> py_finalizers;
+
+void py_add_entry_finalizer(journal_t& journal, object x)
+{
+ py_finalizers.push_back(py_entry_finalizer_t(x));
+ journal.add_entry_finalizer(&py_finalizers.back());
+}
+
+void py_remove_entry_finalizer(journal_t& journal, object x)
+{
+ for (std::list<py_entry_finalizer_t>::iterator i = py_finalizers.begin();
+ i != py_finalizers.end();
+ i++)
+ if ((*i).pyobj == x) {
+ journal.remove_entry_finalizer(&(*i));
+ py_finalizers.erase(i);
+ return;
+ }
+}
+
+void py_run_entry_finalizers(journal_t& journal, entry_t& entry, bool post)
+{
+ run_hooks(journal.entry_finalize_hooks, entry, post);
+}
+
+#define EXC_TRANSLATOR(type) \
+ void exc_translate_ ## type(const type& err) { \
+ PyErr_SetString(PyExc_RuntimeError, err.what()); \
+ }
+
+EXC_TRANSLATOR(balance_error)
+EXC_TRANSLATOR(interval_expr_error)
+EXC_TRANSLATOR(format_error)
+EXC_TRANSLATOR(parse_error)
+
+value_t py_transaction_amount(transaction_t * xact) {
+ return value_t(xact->amount);
+}
+
+transaction_t::state_t py_entry_state(entry_t * entry) {
+ transaction_t::state_t state;
+ if (entry->get_state(&state))
+ return state;
+ else
+ return transaction_t::UNCLEARED;
+}
+
+void export_journal()
+{
+ scope().attr("TRANSACTION_NORMAL") = TRANSACTION_NORMAL;
+ scope().attr("TRANSACTION_VIRTUAL") = TRANSACTION_VIRTUAL;
+ scope().attr("TRANSACTION_BALANCE") = TRANSACTION_BALANCE;
+ scope().attr("TRANSACTION_AUTO") = TRANSACTION_AUTO;
+ scope().attr("TRANSACTION_BULK_ALLOC") = TRANSACTION_BULK_ALLOC;
+ scope().attr("TRANSACTION_CALCULATED") = TRANSACTION_CALCULATED;
+
+ enum_< transaction_t::state_t > ("State")
+ .value("Uncleared", transaction_t::UNCLEARED)
+ .value("Cleared", transaction_t::CLEARED)
+ .value("Pending", transaction_t::PENDING)
+ ;
+
+ class_< transaction_t > ("Transaction")
+ .def(init<optional<account_t *> >())
+ .def(init<account_t *, amount_t, optional<unsigned int, const string&> >())
+
+ .def(self == self)
+ .def(self != self)
+
+ .add_property("entry",
+ make_getter(&transaction_t::entry,
+ return_value_policy<reference_existing_object>()))
+ .add_property("account",
+ make_getter(&transaction_t::account,
+ return_value_policy<reference_existing_object>()))
+
+ .add_property("amount", &py_transaction_amount)
+ .def_readonly("amount_expr", &transaction_t::amount_expr)
+ .add_property("cost",
+ make_getter(&transaction_t::cost,
+ return_internal_reference<1>()))
+ .def_readonly("cost_expr", &transaction_t::cost_expr)
+
+ .def_readwrite("state", &transaction_t::state)
+ .def_readwrite("flags", &transaction_t::flags)
+ .def_readwrite("note", &transaction_t::note)
+
+ .def_readonly("beg_pos", &transaction_t::beg_pos)
+ .def_readonly("beg_line", &transaction_t::beg_line)
+ .def_readonly("end_pos", &transaction_t::end_pos)
+ .def_readonly("end_line", &transaction_t::end_line)
+
+ .def("actual_date", &transaction_t::actual_date)
+ .def("effective_date", &transaction_t::effective_date)
+ .def("date", &transaction_t::date)
+
+ .def("use_effective_date", &transaction_t::use_effective_date)
+
+ .def("valid", &transaction_t::valid)
+ ;
+
+ class_< account_t >
+ ("Account", init<optional<account_t *, string, string> >()
+ [with_custodian_and_ward<1, 2>()])
+ .def(self == self)
+ .def(self != self)
+
+ .def(self_ns::str(self))
+
+ .def("__len__", accounts_len)
+ .def("__getitem__", accounts_getitem, return_internal_reference<1>())
+
+ .add_property("journal",
+ make_getter(&account_t::journal,
+ return_value_policy<reference_existing_object>()))
+ .add_property("parent",
+ make_getter(&account_t::parent,
+ return_value_policy<reference_existing_object>()))
+ .def_readwrite("name", &account_t::name)
+ .def_readwrite("note", &account_t::note)
+ .def_readonly("depth", &account_t::depth)
+ .add_property("data", py_account_get_data, py_account_set_data)
+ .def_readonly("ident", &account_t::ident)
+
+ .def("fullname", &account_t::fullname)
+
+ .def("add_account", &account_t::add_account)
+ .def("remove_account", &account_t::remove_account)
+
+ .def("find_account", &account_t::find_account,
+ return_value_policy<reference_existing_object>())
+
+ .def("valid", &account_t::valid)
+ ;
+
+ class_< journal_t > ("Journal")
+ .def(self == self)
+ .def(self != self)
+
+ .def("__len__", entries_len)
+ .def("__getitem__", entries_getitem, return_internal_reference<1>())
+
+ .add_property("master", make_getter(&journal_t::master,
+ return_internal_reference<1>()))
+ .add_property("basket", make_getter(&journal_t::basket,
+ return_internal_reference<1>()))
+
+ .def_readonly("sources", &journal_t::sources)
+
+ .def_readwrite("price_db", &journal_t::price_db)
+
+ .def("add_account", &journal_t::add_account)
+ .def("remove_account", &journal_t::remove_account)
+
+ .def("find_account", py_find_account_1, return_internal_reference<1>())
+ .def("find_account", py_find_account_2, return_internal_reference<1>())
+ .def("find_account_re", &journal_t::find_account_re,
+ return_internal_reference<1>())
+
+ .def("add_entry", py_add_entry)
+ .def("remove_entry", &journal_t::remove_entry)
+
+ .def("add_entry_finalizer", py_add_entry_finalizer)
+ .def("remove_entry_finalizer", py_remove_entry_finalizer)
+ .def("run_entry_finalizers", py_run_entry_finalizers)
+
+ .def("valid", &journal_t::valid)
+ ;
+
+ class_< entry_base_t, entry_base_wrap, boost::noncopyable > ("EntryBase")
+ .def("__len__", transactions_len)
+ .def("__getitem__", transactions_getitem,
+ return_internal_reference<1>())
+
+ .def_readonly("journal", &entry_base_t::journal)
+
+ .def_readonly("src_idx", &entry_base_t::src_idx)
+ .def_readonly("beg_pos", &entry_base_t::beg_pos)
+ .def_readonly("beg_line", &entry_base_t::beg_line)
+ .def_readonly("end_pos", &entry_base_t::end_pos)
+ .def_readonly("end_line", &entry_base_t::end_line)
+
+ .def("add_transaction", py_add_transaction)
+ .def("remove_transaction", &entry_base_t::remove_transaction)
+
+ .def(self == self)
+ .def(self != self)
+
+ .def("finalize", &entry_base_t::finalize)
+ .def("valid", &entry_base_t::valid)
+ ;
+
+ class_< entry_t, bases<entry_base_t> > ("Entry")
+ .add_property("date", &entry_t::date)
+ .add_property("effective_date", &entry_t::effective_date)
+ .add_property("actual_date", &entry_t::actual_date)
+
+ .def_readwrite("code", &entry_t::code)
+ .def_readwrite("payee", &entry_t::payee)
+
+ .add_property("state", &py_entry_state)
+
+ .def("valid", &entry_t::valid)
+ ;
+
+#define EXC_TRANSLATE(type) \
+ register_error_translator<type>(&exc_translate_ ## type);
+
+ EXC_TRANSLATE(balance_error);
+ EXC_TRANSLATE(interval_expr_error);
+ EXC_TRANSLATE(format_error);
+ EXC_TRANSLATE(parse_error);
+}
+#endif
diff --git a/python/py_parser.cc b/python/py_parser.cc
index 813b3f3c..fc881a0f 100644
--- a/python/py_parser.cc
+++ b/python/py_parser.cc
@@ -66,3 +66,48 @@ void export_parser()
}
} // namespace ledger
+
+#if 0
+#include "parser.h"
+
+using namespace boost::python;
+using namespace ledger;
+
+struct py_parser_t : public parser_t
+{
+ PyObject * self;
+ py_parser_t(PyObject * self_) : self(self_) {}
+
+ virtual bool test(std::istream& in) const {
+ return call_method<bool>(self, "test", in);
+ }
+
+ virtual repitem_t * parse(std::istream& in,
+ journal_t * journal,
+ account_t * master = NULL,
+ const string * original_file = NULL) {
+ return call_method<unsigned int>(self, "parse", in, journal, master,
+ original_file);
+ }
+};
+
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(parser_parse_overloads,
+ py_parser_t::parse, 2, 4)
+
+BOOST_PYTHON_FUNCTION_OVERLOADS(parse_journal_overloads, parse_journal, 2, 4)
+BOOST_PYTHON_FUNCTION_OVERLOADS(parse_journal_file_overloads,
+ parse_journal_file, 2, 4)
+
+void export_parser() {
+ class_< parser_t, py_parser_t, boost::noncopyable > ("Parser")
+ .def("test", &py_parser_t::test)
+ .def("parse", &py_parser_t::parse, parser_parse_overloads())
+ ;
+
+ def("register_parser", register_parser);
+ def("unregister_parser", unregister_parser);
+
+ def("parse_journal", parse_journal, parse_journal_overloads());
+ def("parse_journal_file", parse_journal_file, parse_journal_file_overloads());
+}
+#endif