summaryrefslogtreecommitdiff
path: root/src/py_xact.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-03 08:49:18 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-03 08:49:18 -0500
commitf86394d969d9495f08433e741c0857f6f5fdd772 (patch)
tree9c78dd179e24cdb9cb01e2fe40657aa631797502 /src/py_xact.cc
parent682518fcee944cce76ba8f0315f5b496a6ed4dcd (diff)
downloadfork-ledger-f86394d969d9495f08433e741c0857f6f5fdd772.tar.gz
fork-ledger-f86394d969d9495f08433e741c0857f6f5fdd772.tar.bz2
fork-ledger-f86394d969d9495f08433e741c0857f6f5fdd772.zip
Added Python interface for xact_t
Diffstat (limited to 'src/py_xact.cc')
-rw-r--r--src/py_xact.cc138
1 files changed, 117 insertions, 21 deletions
diff --git a/src/py_xact.cc b/src/py_xact.cc
index b152e272..d98d226c 100644
--- a/src/py_xact.cc
+++ b/src/py_xact.cc
@@ -32,45 +32,141 @@
#include <system.hh>
#include "pyinterp.h"
+#include "pyutils.h"
+#include "xact.h"
+#include "post.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 {
+
+ long posts_len(xact_base_t& xact)
+ {
+ return xact.posts.size();
}
-//EXC_TRANSLATOR(xact_error)
+ post_t& posts_getitem(xact_base_t& xact, long i)
+ {
+ static long last_index = 0;
+ static xact_base_t * last_xact = NULL;
+ static posts_list::iterator elem;
+
+ long len = xact.posts.size();
+
+ if (labs(i) >= len) {
+ PyErr_SetString(PyExc_IndexError, _("Index out of range"));
+ throw_error_already_set();
+ }
+
+ if (&xact == last_xact && i == last_index + 1) {
+ last_index = i;
+ return **++elem;
+ }
+
+ long x = i < 0 ? len + i : i;
+ elem = xact.posts.begin();
+ while (--x >= 0)
+ elem++;
+
+ last_xact = &xact;
+ last_index = i;
+
+ return **elem;
+ }
+
+} // unnamed namespace
+
+using namespace boost::python;
void export_xact()
{
-#if 0
- class_< xact_base_t > ("XactBase")
- ;
- class_< xact_t > ("Xact")
- ;
- struct_< xact_finalizer_t > ("XactFinalizer")
- ;
- class_< auto_xact_t > ("AutoXact")
+ class_< xact_base_t, bases<item_t> > ("TransactionBase")
+ .add_property("journal",
+ make_getter(&xact_base_t::journal,
+ return_value_policy<reference_existing_object>()),
+ make_setter(&xact_base_t::journal,
+ with_custodian_and_ward<1, 2>()))
+ .add_property("posts",
+ make_getter(&xact_base_t::posts),
+ make_setter(&xact_base_t::posts))
+
+ .def("__len__", posts_len)
+ .def("__getitem__", posts_getitem,
+ return_value_policy<reference_existing_object>())
+
+ .def("add_post", &xact_base_t::add_post, with_custodian_and_ward<1, 2>())
+ .def("remove_post", &xact_base_t::add_post)
+
+ .def("finalize", &xact_base_t::finalize)
+ .def("valid", &xact_base_t::valid)
;
- struct_< auto_xact_finalizer_t > ("AutoXactFinalizer")
+
+ class_< xact_t, bases<xact_base_t> > ("Transaction")
+ .add_property("code",
+ make_getter(&xact_t::code),
+ make_setter(&xact_t::code))
+ .add_property("payee",
+ make_getter(&xact_t::payee),
+ make_setter(&xact_t::payee))
+
+ .def("add_post", &xact_t::add_post, with_custodian_and_ward<1, 2>())
+
+ .def("magnitude", &xact_t::magnitude)
+ .def("idstring", &xact_t::idstring)
+ .def("id", &xact_t::id)
+
+ .def("lookup", &xact_t::lookup)
+
+ .def("valid", &xact_t::valid)
;
- class_< period_xact_t > ("PeriodXact")
+
+ class_< xact_finalizer_t, boost::noncopyable >
+ ("TransactionFinalizer", no_init)
+ .def("__call__", &xact_finalizer_t::operator())
;
- class_< func_finalizer_t > ("FuncFinalizer")
+
+ class_< auto_xact_t, bases<xact_base_t> > ("AutomatedTransaction")
+ .def(init<item_predicate>())
+
+ .add_property("predicate",
+ make_getter(&auto_xact_t::predicate),
+ make_setter(&auto_xact_t::predicate))
+
+ .def("extend_xact", &auto_xact_t::extend_xact)
;
-#endif
- //register_optional_to_python<amount_t>();
+ class_< auto_xact_finalizer_t, bases<xact_finalizer_t> >
+ ("AutomatedTransactionFinalizer")
+ .add_property("journal",
+ make_getter(&auto_xact_finalizer_t::journal,
+ return_value_policy<reference_existing_object>()),
+ make_setter(&auto_xact_finalizer_t::journal,
+ with_custodian_and_ward<1, 2>()))
+ .def("__call__", &auto_xact_finalizer_t::operator())
+ ;
- //implicitly_convertible<string, amount_t>();
+ class_< period_xact_t, bases<xact_base_t> > ("PeriodicTransaction")
+ .def(init<string>())
+
+ .add_property("period",
+ make_getter(&period_xact_t::period),
+ make_setter(&period_xact_t::period))
+ .add_property("period_string",
+ make_getter(&period_xact_t::period_string),
+ make_setter(&period_xact_t::period_string))
+ ;
-#define EXC_TRANSLATE(type) \
- register_exception_translator<type>(&exc_translate_ ## type);
+ class_< func_finalizer_t, bases<xact_finalizer_t> >
+ ("FunctionalFinalizer", init<func_finalizer_t::func_t>())
+ .add_property("func",
+ make_getter(&func_finalizer_t::func),
+ make_setter(&func_finalizer_t::func))
+ .def("__call__", &func_finalizer_t::operator())
+ ;
- //EXC_TRANSLATE(xact_error);
+ scope().attr("extend_xact_base") = &extend_xact_base;
}
} // namespace ledger