From ed0e01812a1d604002f3ff23d3cbdffe60bd9297 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 3 Nov 2009 02:09:38 -0500 Subject: Added Python interface for item_t --- src/py_item.cc | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 7 deletions(-) (limited to 'src/py_item.cc') diff --git a/src/py_item.cc b/src/py_item.cc index 0e1fe0f9..dbf5b6f7 100644 --- a/src/py_item.cc +++ b/src/py_item.cc @@ -32,11 +32,39 @@ #include #include "pyinterp.h" +#include "mask.h" +#include "item.h" namespace ledger { using namespace boost::python; +namespace { + + bool py_has_tag_1s(item_t& item, const string& tag) { + return item.has_tag(tag); + } + bool py_has_tag_1m(item_t& item, const mask_t& tag_mask) { + return item.has_tag(tag_mask); + } + bool py_has_tag_2m(item_t& item, const mask_t& tag_mask, + const boost::optional& value_mask) { + return item.has_tag(tag_mask, value_mask); + } + + boost::optional py_get_tag_1s(item_t& item, const string& tag) { + return item.get_tag(tag); + } + boost::optional py_get_tag_1m(item_t& item, const mask_t& tag_mask) { + return item.get_tag(tag_mask); + } + boost::optional py_get_tag_2m(item_t& item, const mask_t& tag_mask, + const boost::optional& value_mask) { + return item.get_tag(tag_mask, value_mask); + } + +} // unnamed namespace + #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ @@ -46,19 +74,79 @@ using namespace boost::python; void export_item() { -#if 0 - class_< item_t > ("Item") + class_< position_t > ("Position") + .add_property("pathname", + make_getter(&position_t::pathname), + make_setter(&position_t::pathname)) + .add_property("beg_pos", + make_getter(&position_t::beg_pos), + make_setter(&position_t::beg_pos)) + .add_property("beg_line", + make_getter(&position_t::beg_line), + make_setter(&position_t::beg_line)) + .add_property("end_pos", + make_getter(&position_t::end_pos), + make_setter(&position_t::end_pos)) + .add_property("end_line", + make_getter(&position_t::end_line), + make_setter(&position_t::end_line)) ; + + scope().attr("ITEM_NORMAL") = ITEM_NORMAL; + scope().attr("ITEM_GENERATED") = ITEM_GENERATED; + scope().attr("ITEM_TEMP") = ITEM_TEMP; + + class_< item_t > ("Item", init()) +#if 1 + .def("flags", &supports_flags<>::flags) + .def("has_flags", &supports_flags<>::has_flags) + .def("set_flags", &supports_flags<>::set_flags) + .def("clear_flags", &supports_flags<>::clear_flags) + .def("add_flags", &supports_flags<>::add_flags) + .def("drop_flags", &supports_flags<>::drop_flags) #endif - //register_optional_to_python(); + .add_property("note", + make_getter(&item_t::note), + make_setter(&item_t::note)) + .add_property("pos", + make_getter(&item_t::pos), + make_setter(&item_t::pos)) + .add_property("metadata", + make_getter(&item_t::metadata), + make_setter(&item_t::metadata)) + + .def("copy_details", &item_t::copy_details) + + .def(self == self) + .def(self != self) + + .def("has_tag", py_has_tag_1s) + .def("has_tag", py_has_tag_1m) + .def("has_tag", py_has_tag_2m) + .def("get_tag", py_get_tag_1s) + .def("get_tag", py_get_tag_1m) + .def("get_tag", py_get_tag_2m) - //implicitly_convertible(); + .def("set_tag", &item_t::set_tag) -#define EXC_TRANSLATE(type) \ - register_exception_translator(&exc_translate_ ## type); + .def("parse_tags", &item_t::parse_tags) + .def("append_note", &item_t::append_note) - //EXC_TRANSLATE(item_error); + .add_static_property("use_effective_date", + make_getter(&item_t::use_effective_date), + make_setter(&item_t::use_effective_date)) + + .def("date", &item_t::date) + .def("effective_date", &item_t::effective_date) + + .def("set_state", &item_t::set_state) + .def("state", &item_t::state) + + .def("lookup", &item_t::lookup) + + .def("valid", &item_t::valid) + ; } } // namespace ledger -- cgit v1.2.3 From a462b93e30f91eed9f2a4699726cbc60b7d67d9a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 3 Nov 2009 02:25:25 -0500 Subject: Added Python interface for post_t --- src/py_item.cc | 2 +- src/py_post.cc | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 130 insertions(+), 13 deletions(-) (limited to 'src/py_item.cc') diff --git a/src/py_item.cc b/src/py_item.cc index dbf5b6f7..91f7b8ac 100644 --- a/src/py_item.cc +++ b/src/py_item.cc @@ -96,7 +96,7 @@ void export_item() scope().attr("ITEM_GENERATED") = ITEM_GENERATED; scope().attr("ITEM_TEMP") = ITEM_TEMP; - class_< item_t > ("Item", init()) + class_< item_t > ("JournalItem", init()) #if 1 .def("flags", &supports_flags<>::flags) .def("has_flags", &supports_flags<>::has_flags) diff --git a/src/py_post.cc b/src/py_post.cc index b1d62464..b4849c3d 100644 --- a/src/py_post.cc +++ b/src/py_post.cc @@ -32,33 +32,150 @@ #include #include "pyinterp.h" +#include "post.h" +#include "xact.h" namespace ledger { using namespace boost::python; -#define EXC_TRANSLATOR(type) \ - void exc_translate_ ## type(const type& err) { \ - PyErr_SetString(PyExc_ArithmeticError, err.what()); \ +namespace { + + bool py_has_tag_1s(post_t& post, const string& tag) { + return post.has_tag(tag); + } + bool py_has_tag_1m(post_t& post, const mask_t& tag_mask) { + return post.has_tag(tag_mask); + } + bool py_has_tag_2m(post_t& post, const mask_t& tag_mask, + const boost::optional& value_mask) { + return post.has_tag(tag_mask, value_mask); + } + + boost::optional py_get_tag_1s(post_t& post, const string& tag) { + return post.get_tag(tag); + } + boost::optional py_get_tag_1m(post_t& post, const mask_t& tag_mask) { + return post.get_tag(tag_mask); + } + boost::optional py_get_tag_2m(post_t& post, const mask_t& tag_mask, + const boost::optional& value_mask) { + return post.get_tag(tag_mask, value_mask); + } + + post_t::xdata_t& py_xdata(post_t& post) { + return post.xdata(); } -//EXC_TRANSLATOR(post_error) + account_t * py_reported_account(post_t& post) { + return post.reported_account(); + } + +} // unnamed namespace void export_post() { -#if 0 - class_< post_t > ("Post") - ; + scope().attr("POST_EXT_RECEIVED") = POST_EXT_RECEIVED; + scope().attr("POST_EXT_HANDLED") = POST_EXT_HANDLED; + scope().attr("POST_EXT_DISPLAYED") = POST_EXT_DISPLAYED; + scope().attr("POST_EXT_DIRECT_AMT") = POST_EXT_DIRECT_AMT; + scope().attr("POST_EXT_SORT_CALC") = POST_EXT_SORT_CALC; + scope().attr("POST_EXT_COMPOUND") = POST_EXT_COMPOUND; + scope().attr("POST_EXT_VISITED") = POST_EXT_VISITED; + scope().attr("POST_EXT_MATCHES") = POST_EXT_MATCHES; + scope().attr("POST_EXT_CONSIDERED") = POST_EXT_CONSIDERED; + + class_< post_t::xdata_t > ("PostingXData") +#if 1 + .def("flags", &supports_flags::flags) + .def("has_flags", &supports_flags::has_flags) + .def("set_flags", &supports_flags::set_flags) + .def("clear_flags", &supports_flags::clear_flags) + .def("add_flags", &supports_flags::add_flags) + .def("drop_flags", &supports_flags::drop_flags) #endif - //register_optional_to_python(); + .add_property("visited_value", + make_getter(&post_t::xdata_t::visited_value), + make_setter(&post_t::xdata_t::visited_value)) + .add_property("compound_value", + make_getter(&post_t::xdata_t::compound_value), + make_setter(&post_t::xdata_t::compound_value)) + .add_property("total", + make_getter(&post_t::xdata_t::total), + make_setter(&post_t::xdata_t::total)) + .add_property("count", + make_getter(&post_t::xdata_t::count), + make_setter(&post_t::xdata_t::count)) + .add_property("date", + make_getter(&post_t::xdata_t::date), + make_setter(&post_t::xdata_t::date)) + .add_property("datetime", + make_getter(&post_t::xdata_t::datetime), + make_setter(&post_t::xdata_t::datetime)) + .add_property("account", + make_getter(&post_t::xdata_t::account), + make_setter(&post_t::xdata_t::account)) + .add_property("sort_values", + make_getter(&post_t::xdata_t::sort_values), + make_setter(&post_t::xdata_t::sort_values)) + ; + + scope().attr("POST_VIRTUAL") = POST_VIRTUAL; + scope().attr("POST_MUST_BALANCE") = POST_MUST_BALANCE; + scope().attr("POST_CALCULATED") = POST_CALCULATED; + scope().attr("POST_COST_CALCULATED") = POST_COST_CALCULATED; + + class_< post_t, bases > ("Posting") + //.def(init()) + + .add_property("xact", + make_getter(&post_t::xact, + return_value_policy()), + make_setter(&post_t::xact, + with_custodian_and_ward<1, 2>())) + .add_property("account", + make_getter(&post_t::account, + return_value_policy()), + make_setter(&post_t::account, + with_custodian_and_ward<1, 2>())) + .add_property("amount", + make_getter(&post_t::amount), + make_setter(&post_t::amount)) + .add_property("cost", + make_getter(&post_t::cost), + make_setter(&post_t::cost)) + .add_property("assigned_amount", + make_getter(&post_t::assigned_amount), + make_setter(&post_t::assigned_amount)) + + .def("has_tag", py_has_tag_1s) + .def("has_tag", py_has_tag_1m) + .def("has_tag", py_has_tag_2m) + .def("get_tag", py_get_tag_1s) + .def("get_tag", py_get_tag_1m) + .def("get_tag", py_get_tag_2m) - //implicitly_convertible(); + .def("date", &post_t::date) + .def("effective_date", &post_t::effective_date) -#define EXC_TRANSLATE(type) \ - register_exception_translator(&exc_translate_ ## type); + .def("must_balance", &post_t::must_balance) - //EXC_TRANSLATE(post_error); + .def("lookup", &post_t::lookup) + + .def("valid", &post_t::valid) + + .def("has_xdata", &post_t::has_xdata) + .def("clear_xdata", &post_t::clear_xdata) + .def("xdata", py_xdata, + return_value_policy()) + + .def("add_to_value", &post_t::add_to_value) + .def("set_reported_account", &post_t::set_reported_account) + + .def("reported_account", py_reported_account, + return_value_policy()) + ; } } // namespace ledger -- cgit v1.2.3 From 3d3d21150e8e7f34dede814bdbe71bf019791436 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 3 Nov 2009 08:50:41 -0500 Subject: Added Python interface for an item_t enum --- src/py_item.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/py_item.cc') diff --git a/src/py_item.cc b/src/py_item.cc index 91f7b8ac..ac544c6a 100644 --- a/src/py_item.cc +++ b/src/py_item.cc @@ -32,6 +32,7 @@ #include #include "pyinterp.h" +#include "scope.h" #include "mask.h" #include "item.h" @@ -96,7 +97,17 @@ void export_item() scope().attr("ITEM_GENERATED") = ITEM_GENERATED; scope().attr("ITEM_TEMP") = ITEM_TEMP; + enum_< item_t::state_t > ("State") + .value("Uncleared", item_t::UNCLEARED) + .value("Cleared", item_t::CLEARED) + .value("Pending", item_t::PENDING) + ; + +#if 0 + class_< item_t, bases > ("JournalItem", init()) +#else class_< item_t > ("JournalItem", init()) +#endif #if 1 .def("flags", &supports_flags<>::flags) .def("has_flags", &supports_flags<>::has_flags) -- cgit v1.2.3