Added Python interface for xact_t

This commit is contained in:
John Wiegley 2009-11-03 08:49:18 -05:00
parent 682518fcee
commit f86394d969

View file

@ -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")
;
struct_< auto_xact_finalizer_t > ("AutoXactFinalizer")
;
class_< period_xact_t > ("PeriodXact")
;
class_< func_finalizer_t > ("FuncFinalizer")
;
#endif
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))
//register_optional_to_python<amount_t>();
.def("__len__", posts_len)
.def("__getitem__", posts_getitem,
return_value_policy<reference_existing_object>())
//implicitly_convertible<string, amount_t>();
.def("add_post", &xact_base_t::add_post, with_custodian_and_ward<1, 2>())
.def("remove_post", &xact_base_t::add_post)
#define EXC_TRANSLATE(type) \
register_exception_translator<type>(&exc_translate_ ## type);
.def("finalize", &xact_base_t::finalize)
.def("valid", &xact_base_t::valid)
;
//EXC_TRANSLATE(xact_error);
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_< xact_finalizer_t, boost::noncopyable >
("TransactionFinalizer", no_init)
.def("__call__", &xact_finalizer_t::operator())
;
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)
;
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())
;
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))
;
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())
;
scope().attr("extend_xact_base") = &extend_xact_base;
}
} // namespace ledger