Added Python interface for journal_t
This commit is contained in:
parent
7c79a745d3
commit
682518fcee
1 changed files with 149 additions and 347 deletions
|
|
@ -32,334 +32,176 @@
|
|||
#include <system.hh>
|
||||
|
||||
#include "pyinterp.h"
|
||||
#include "pyutils.h"
|
||||
#include "hooks.h"
|
||||
#include "journal.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 {
|
||||
|
||||
account_t& py_account_master(journal_t& journal) {
|
||||
return *journal.master;
|
||||
}
|
||||
|
||||
//EXC_TRANSLATOR(journal_error)
|
||||
|
||||
void export_journal()
|
||||
{
|
||||
#if 0
|
||||
class_< journal_t > ("Journal")
|
||||
;
|
||||
#endif
|
||||
|
||||
//register_optional_to_python<amount_t>();
|
||||
|
||||
//implicitly_convertible<string, amount_t>();
|
||||
|
||||
#define EXC_TRANSLATE(type) \
|
||||
register_exception_translator<type>(&exc_translate_ ## type);
|
||||
|
||||
//EXC_TRANSLATE(journal_error);
|
||||
}
|
||||
|
||||
} // namespace ledger
|
||||
|
||||
#if 0
|
||||
xact_t& post_xact(const post_t& post)
|
||||
{
|
||||
return *post.xact;
|
||||
}
|
||||
|
||||
unsigned int posts_len(xact_base_t& xact)
|
||||
{
|
||||
return xact.posts.size();
|
||||
}
|
||||
|
||||
post_t& posts_getitem(xact_base_t& xact, int i)
|
||||
{
|
||||
static int last_index = 0;
|
||||
static xact_base_t * last_xact = NULL;
|
||||
static posts_list::iterator elem;
|
||||
|
||||
std::size_t len = xact.posts.size();
|
||||
|
||||
if (abs(i) >= len) {
|
||||
PyErr_SetString(PyExc_IndexError, _("Index out of range"));
|
||||
throw_error_already_set();
|
||||
commodity_pool_t& py_commodity_pool(journal_t& journal) {
|
||||
return *journal.commodity_pool;
|
||||
}
|
||||
|
||||
if (&xact == last_xact && i == last_index + 1) {
|
||||
last_index = i;
|
||||
return **++elem;
|
||||
long xacts_len(journal_t& journal)
|
||||
{
|
||||
return journal.xacts.size();
|
||||
}
|
||||
|
||||
int x = i < 0 ? len + i : i;
|
||||
elem = xact.posts.begin();
|
||||
while (--x >= 0)
|
||||
elem++;
|
||||
xact_t& xacts_getitem(journal_t& journal, long i)
|
||||
{
|
||||
static long last_index = 0;
|
||||
static journal_t * last_journal = NULL;
|
||||
static xacts_list::iterator elem;
|
||||
|
||||
last_xact = &xact;
|
||||
last_index = i;
|
||||
long len = journal.xacts.size();
|
||||
|
||||
return **elem;
|
||||
}
|
||||
|
||||
unsigned int xacts_len(journal_t& journal)
|
||||
{
|
||||
return journal.xacts.size();
|
||||
}
|
||||
|
||||
xact_t& xacts_getitem(journal_t& journal, int i)
|
||||
{
|
||||
static int last_index = 0;
|
||||
static journal_t * last_journal = NULL;
|
||||
static xacts_list::iterator elem;
|
||||
|
||||
std::size_t len = journal.xacts.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.xacts.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;
|
||||
}
|
||||
|
||||
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_xact(journal_t& journal, xact_t * xact) {
|
||||
return journal.add_xact(new xact_t(*xact));
|
||||
}
|
||||
|
||||
void py_add_post(xact_base_t& xact, post_t * post) {
|
||||
return xact.add_post(new post_t(*post));
|
||||
}
|
||||
|
||||
struct xact_base_wrap : public xact_base_t
|
||||
{
|
||||
PyObject * self;
|
||||
xact_base_wrap(PyObject * self_) : self(self_) {}
|
||||
|
||||
virtual bool valid() const {
|
||||
return call_method<bool>(self, "valid");
|
||||
}
|
||||
};
|
||||
|
||||
struct py_xact_finalizer_t : public xact_finalizer_t {
|
||||
object pyobj;
|
||||
py_xact_finalizer_t() {}
|
||||
py_xact_finalizer_t(object obj) : pyobj(obj) {}
|
||||
py_xact_finalizer_t(const py_xact_finalizer_t& other)
|
||||
: pyobj(other.pyobj) {}
|
||||
virtual bool operator()(xact_t& xact, bool post) {
|
||||
return call<bool>(pyobj.ptr(), xact, post);
|
||||
}
|
||||
};
|
||||
|
||||
std::list<py_xact_finalizer_t> py_finalizers;
|
||||
|
||||
void py_add_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
py_finalizers.push_back(py_xact_finalizer_t(x));
|
||||
journal.add_xact_finalizer(&py_finalizers.back());
|
||||
}
|
||||
|
||||
void py_remove_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
for (std::list<py_xact_finalizer_t>::iterator i = py_finalizers.begin();
|
||||
i != py_finalizers.end();
|
||||
i++)
|
||||
if ((*i).pyobj == x) {
|
||||
journal.remove_xact_finalizer(&(*i));
|
||||
py_finalizers.erase(i);
|
||||
return;
|
||||
if (labs(i) >= len) {
|
||||
PyErr_SetString(PyExc_IndexError, _("Index out of range"));
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
|
||||
void py_run_xact_finalizers(journal_t& journal, xact_t& xact, bool post)
|
||||
{
|
||||
run_hooks(journal.xact_finalize_hooks, xact, post);
|
||||
}
|
||||
if (&journal == last_journal && i == last_index + 1) {
|
||||
last_index = i;
|
||||
return **++elem;
|
||||
}
|
||||
|
||||
#define EXC_TRANSLATOR(type) \
|
||||
void exc_translate_ ## type(const type& err) { \
|
||||
PyErr_SetString(PyExc_RuntimeError, err.what()); \
|
||||
long x = i < 0 ? len + i : i;
|
||||
elem = journal.xacts.begin();
|
||||
while (--x >= 0)
|
||||
elem++;
|
||||
|
||||
last_journal = &journal;
|
||||
last_index = i;
|
||||
|
||||
return **elem;
|
||||
}
|
||||
|
||||
EXC_TRANSLATOR(balance_error)
|
||||
EXC_TRANSLATOR(interval_expr_error)
|
||||
EXC_TRANSLATOR(format_error)
|
||||
EXC_TRANSLATOR(parse_error)
|
||||
long accounts_len(account_t& account)
|
||||
{
|
||||
return account.accounts.size();
|
||||
}
|
||||
|
||||
value_t py_post_amount(post_t * post) {
|
||||
return value_t(post->amount);
|
||||
}
|
||||
account_t& accounts_getitem(account_t& account, long i)
|
||||
{
|
||||
static long last_index = 0;
|
||||
static account_t * last_account = NULL;
|
||||
static accounts_map::iterator elem;
|
||||
|
||||
post_t::state_t py_xact_state(xact_t * xact) {
|
||||
post_t::state_t state;
|
||||
if (xact->get_state(&state))
|
||||
return state;
|
||||
else
|
||||
return post_t::UNCLEARED;
|
||||
}
|
||||
long len = account.accounts.size();
|
||||
|
||||
if (labs(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;
|
||||
}
|
||||
|
||||
long x = i < 0 ? len + i : i;
|
||||
elem = account.accounts.begin();
|
||||
while (--x >= 0)
|
||||
elem++;
|
||||
|
||||
last_account = &account;
|
||||
last_index = i;
|
||||
|
||||
return *(*elem).second;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
struct py_xact_finalizer_t : public xact_finalizer_t {
|
||||
object pyobj;
|
||||
py_xact_finalizer_t() {}
|
||||
py_xact_finalizer_t(object obj) : pyobj(obj) {}
|
||||
py_xact_finalizer_t(const py_xact_finalizer_t& other)
|
||||
: pyobj(other.pyobj) {}
|
||||
virtual bool operator()(xact_t& xact, bool post) {
|
||||
return call<bool>(pyobj.ptr(), xact, post);
|
||||
}
|
||||
};
|
||||
|
||||
std::list<py_xact_finalizer_t> py_finalizers;
|
||||
|
||||
void py_add_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
py_finalizers.push_back(py_xact_finalizer_t(x));
|
||||
journal.add_xact_finalizer(&py_finalizers.back());
|
||||
}
|
||||
|
||||
void py_remove_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
for (std::list<py_xact_finalizer_t>::iterator i = py_finalizers.begin();
|
||||
i != py_finalizers.end();
|
||||
i++)
|
||||
if ((*i).pyobj == x) {
|
||||
journal.remove_xact_finalizer(&(*i));
|
||||
py_finalizers.erase(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void py_run_xact_finalizers(journal_t& journal, xact_t& xact, bool post)
|
||||
{
|
||||
journal.xact_finalize_hooks.run_hooks(xact, post);
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
void export_journal()
|
||||
{
|
||||
scope().attr("POST_NORMAL") = POST_NORMAL;
|
||||
scope().attr("POST_VIRTUAL") = POST_VIRTUAL;
|
||||
scope().attr("POST_BALANCE") = POST_BALANCE;
|
||||
scope().attr("POST_AUTO") = POST_AUTO;
|
||||
scope().attr("POST_BULK_ALLOC") = POST_BULK_ALLOC;
|
||||
scope().attr("POST_CALCULATED") = POST_CALCULATED;
|
||||
|
||||
enum_< post_t::state_t > ("State")
|
||||
.value("Uncleared", post_t::UNCLEARED)
|
||||
.value("Cleared", post_t::CLEARED)
|
||||
.value("Pending", post_t::PENDING)
|
||||
class_< journal_t::fileinfo_t > ("FileInfo")
|
||||
.add_property("filename",
|
||||
make_getter(&journal_t::fileinfo_t::filename),
|
||||
make_setter(&journal_t::fileinfo_t::filename))
|
||||
.add_property("size",
|
||||
make_getter(&journal_t::fileinfo_t::size),
|
||||
make_setter(&journal_t::fileinfo_t::size))
|
||||
.add_property("modtime",
|
||||
make_getter(&journal_t::fileinfo_t::modtime),
|
||||
make_setter(&journal_t::fileinfo_t::modtime))
|
||||
.add_property("from_stream",
|
||||
make_getter(&journal_t::fileinfo_t::from_stream),
|
||||
make_setter(&journal_t::fileinfo_t::from_stream))
|
||||
;
|
||||
|
||||
class_< post_t > ("Post")
|
||||
.def(init<optional<account_t *> >())
|
||||
.def(init<account_t *, amount_t, optional<unsigned int, const string&> >())
|
||||
|
||||
.def(self == self)
|
||||
.def(self != self)
|
||||
|
||||
.add_property("xact",
|
||||
make_getter(&post_t::xact,
|
||||
return_value_policy<reference_existing_object>()))
|
||||
.add_property("account",
|
||||
make_getter(&post_t::account,
|
||||
return_value_policy<reference_existing_object>()))
|
||||
|
||||
.add_property("amount", &py_post_amount)
|
||||
.def_readonly("amount_expr", &post_t::amount_expr)
|
||||
.add_property("cost",
|
||||
make_getter(&post_t::cost,
|
||||
return_internal_reference<1>()))
|
||||
.def_readonly("cost_expr", &post_t::cost_expr)
|
||||
|
||||
.def_readwrite("state", &post_t::state)
|
||||
.def_readwrite("flags", &post_t::flags)
|
||||
.def_readwrite("note", &post_t::note)
|
||||
|
||||
.def_readonly("beg_pos", &post_t::beg_pos)
|
||||
.def_readonly("beg_line", &post_t::beg_line)
|
||||
.def_readonly("end_pos", &post_t::end_pos)
|
||||
.def_readonly("end_line", &post_t::end_line)
|
||||
|
||||
.def("actual_date", &post_t::actual_date)
|
||||
.def("effective_date", &post_t::effective_date)
|
||||
.def("date", &post_t::date)
|
||||
|
||||
.def("use_effective_date", &post_t::use_effective_date)
|
||||
|
||||
.def("valid", &post_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)
|
||||
.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__", xacts_len)
|
||||
.def("__getitem__", xacts_getitem, return_internal_reference<1>())
|
||||
|
||||
class_< journal_t, boost::noncopyable > ("Journal")
|
||||
.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)
|
||||
.add_property("basket",
|
||||
make_getter(&journal_t::basket,
|
||||
return_internal_reference<1>()),
|
||||
make_setter(&journal_t::basket))
|
||||
.add_property("sources", make_getter(&journal_t::sources))
|
||||
.add_property("was_loaded", make_getter(&journal_t::was_loaded))
|
||||
.add_property("commodity_pool",
|
||||
make_getter(&journal_t::commodity_pool,
|
||||
return_internal_reference<1>()))
|
||||
#if 0
|
||||
.add_property("xact_finalize_hooks",
|
||||
make_getter(&journal_t::xact_finalize_hooks),
|
||||
make_setter(&journal_t::xact_finalize_hooks))
|
||||
#endif
|
||||
|
||||
.def("add_account", &journal_t::add_account)
|
||||
.def("remove_account", &journal_t::remove_account)
|
||||
|
|
@ -369,58 +211,18 @@ void export_journal()
|
|||
.def("find_account_re", &journal_t::find_account_re,
|
||||
return_internal_reference<1>())
|
||||
|
||||
.def("add_xact", py_add_xact)
|
||||
.def("add_xact", &journal_t::add_xact)
|
||||
.def("remove_xact", &journal_t::remove_xact)
|
||||
|
||||
.def("add_xact_finalizer", py_add_xact_finalizer)
|
||||
.def("remove_xact_finalizer", py_remove_xact_finalizer)
|
||||
.def("run_xact_finalizers", py_run_xact_finalizers)
|
||||
|
||||
.def("__len__", xacts_len)
|
||||
.def("__getitem__", xacts_getitem, return_internal_reference<1>())
|
||||
|
||||
.def("valid", &journal_t::valid)
|
||||
;
|
||||
|
||||
class_< xact_base_t, xact_base_wrap, boost::noncopyable > ("XactBase")
|
||||
.def("__len__", posts_len)
|
||||
.def("__getitem__", posts_getitem,
|
||||
return_internal_reference<1>())
|
||||
|
||||
.def_readonly("journal", &xact_base_t::journal)
|
||||
|
||||
.def_readonly("src_idx", &xact_base_t::src_idx)
|
||||
.def_readonly("beg_pos", &xact_base_t::beg_pos)
|
||||
.def_readonly("beg_line", &xact_base_t::beg_line)
|
||||
.def_readonly("end_pos", &xact_base_t::end_pos)
|
||||
.def_readonly("end_line", &xact_base_t::end_line)
|
||||
|
||||
.def("add_post", py_add_post)
|
||||
.def("remove_post", &xact_base_t::remove_post)
|
||||
|
||||
.def(self == self)
|
||||
.def(self != self)
|
||||
|
||||
.def("finalize", &xact_base_t::finalize)
|
||||
.def("valid", &xact_base_t::valid)
|
||||
;
|
||||
|
||||
class_< xact_t, bases<xact_base_t> > ("Xact")
|
||||
.add_property("date", &xact_t::date)
|
||||
.add_property("effective_date", &xact_t::effective_date)
|
||||
.add_property("actual_date", &xact_t::actual_date)
|
||||
|
||||
.def_readwrite("code", &xact_t::code)
|
||||
.def_readwrite("payee", &xact_t::payee)
|
||||
|
||||
.add_property("state", &py_xact_state)
|
||||
|
||||
.def("valid", &xact_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
|
||||
|
||||
} // namespace ledger
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue