diff options
author | John Wiegley <johnw@newartisans.com> | 2009-11-02 19:17:43 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-11-02 19:17:43 -0500 |
commit | 46e46dd5a337c009391583a679d6172cf4f5aa42 (patch) | |
tree | 67009e7d8681685afec8b09dcb7930570492e36f /src/py_balance.cc | |
parent | a048afc8a34d3a1c1a6372ef6b7cc373779bcac0 (diff) | |
parent | d6790072eff9cb2a938ee9ed204263ee277a6874 (diff) | |
download | fork-ledger-46e46dd5a337c009391583a679d6172cf4f5aa42.tar.gz fork-ledger-46e46dd5a337c009391583a679d6172cf4f5aa42.tar.bz2 fork-ledger-46e46dd5a337c009391583a679d6172cf4f5aa42.zip |
Merge branch 'next'
Diffstat (limited to 'src/py_balance.cc')
-rw-r--r-- | src/py_balance.cc | 280 |
1 files changed, 120 insertions, 160 deletions
diff --git a/src/py_balance.cc b/src/py_balance.cc index 086b7b12..2d940876 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -32,69 +32,90 @@ #include <system.hh> #include "pyinterp.h" +#include "pyutils.h" +#include "pyfstream.h" +#include "commodity.h" +#include "annotate.h" +#include "balance.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 { + + boost::optional<balance_t> py_value_0(const balance_t& balance) { + return balance.value(); + } + boost::optional<balance_t> py_value_1(const balance_t& balance, + const bool primary_only) { + return balance.value(primary_only); } -EXC_TRANSLATOR(balance_error) + boost::optional<balance_t> + py_value_2(const balance_t& balance, + const bool primary_only, + const boost::optional<datetime_t>& moment) { + return balance.value(primary_only, moment); + } -void export_balance() -{ -#if 0 - class_< balance_t > ("Balance") - ; -#endif + boost::optional<balance_t> + py_value_3(const balance_t& balance, + const bool primary_only, + const boost::optional<datetime_t>& moment, + const boost::optional<commodity_t&>& in_terms_of) { + return balance.value(primary_only, moment, in_terms_of); + } - //register_optional_to_python<amount_t>(); + boost::optional<amount_t> + py_commodity_amount_0(const balance_t& balance) { + return balance.commodity_amount(); + } - //implicitly_convertible<string, amount_t>(); + boost::optional<amount_t> + py_commodity_amount_1(const balance_t& balance, + const boost::optional<const commodity_t&>& commodity) { + return balance.commodity_amount(commodity); + } -#define EXC_TRANSLATE(type) \ - register_exception_translator<type>(&exc_translate_ ## type); + void py_print(balance_t& balance, object out) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast<PyFileObject *>(out.ptr())); + balance.print(outstr); + } else { + PyErr_SetString(PyExc_IOError, + _("Argument to balance.print_(file) is not a file object")); + } + } - //EXC_TRANSLATE(balance_error); -} + long balance_len(balance_t& bal) { + return bal.amounts.size(); + } -} // namespace ledger + amount_t balance_getitem(balance_t& bal, long i) { + long len = bal.amounts.size(); -#if 0 -unsigned int balance_len(balance_t& bal) -{ - return bal.amounts.size(); -} + if (labs(i) >= len) { + PyErr_SetString(PyExc_IndexError, _("Index out of range")); + throw_error_already_set(); + } -amount_t balance_getitem(balance_t& bal, int i) -{ - std::size_t len = bal.amounts.size(); + long x = i < 0 ? len + i : i; + balance_t::amounts_map::iterator elem = bal.amounts.begin(); + while (--x >= 0) + elem++; - if (abs(i) >= len) { - PyErr_SetString(PyExc_IndexError, _("Index out of range")); - throw_error_already_set(); + return (*elem).second; } - int x = i < 0 ? len + i : i; - balance_t::amounts_map::iterator elem = bal.amounts.begin(); - while (--x >= 0) - elem++; +} // unnamed namespace - return (*elem).second; -} - -unsigned int balance_pair_len(balance_pair_t& bal_pair) -{ - return balance_len(bal_pair.quantity); -} +#define EXC_TRANSLATOR(type) \ + void exc_translate_ ## type(const type& err) { \ + PyErr_SetString(PyExc_ArithmeticError, err.what()); \ + } -amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i) -{ - return balance_getitem(bal_pair.quantity, i); -} +EXC_TRANSLATOR(balance_error) void export_balance() { @@ -102,8 +123,7 @@ void export_balance() .def(init<balance_t>()) .def(init<amount_t>()) .def(init<long>()) - .def(init<unsigned long>()) - .def(init<double>()) + .def(init<string>()) .def(self += self) .def(self += other<amount_t>()) @@ -117,32 +137,16 @@ void export_balance() .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()) @@ -153,114 +157,70 @@ void export_balance() .def(self_ns::str(self)) + .def("negated", &balance_t::negated) + .def("in_place_negate", &balance_t::in_place_negate, + return_value_policy<reference_existing_object>()) + .def(- self) + + .def("abs", &balance_t::abs) .def("__abs__", &balance_t::abs) + .def("__len__", balance_len) .def("__getitem__", balance_getitem) - .def("valid", &balance_t::valid) + .def("rounded", &balance_t::rounded) + .def("in_place_round", &balance_t::in_place_round, + return_value_policy<reference_existing_object>()) - .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) - ; + .def("truncated", &balance_t::truncated) + .def("in_place_truncate", &balance_t::in_place_truncate, + return_value_policy<reference_existing_object>()) - 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("unrounded", &balance_t::unrounded) + .def("in_place_unround", &balance_t::in_place_unround, + return_value_policy<reference_existing_object>()) - .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("reduced", &balance_t::reduced) + .def("in_place_reduce", &balance_t::in_place_reduce, + return_value_policy<reference_existing_object>()) - .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("unreduced", &balance_t::unreduced) + .def("in_place_unreduce", &balance_t::in_place_unreduce, + return_value_policy<reference_existing_object>()) - .def(self_ns::str(self)) + .def("value", py_value_0) + .def("value", py_value_1, args("primary_only")) + .def("value", py_value_2, args("primary_only", "moment")) + .def("value", py_value_3, args("primary_only", "moment", "in_terms_of")) + + .def("price", &balance_t::price) + + .def("__nonzero__", &balance_t::is_nonzero) + .def("is_nonzero", &balance_t::is_nonzero) + .def("is_zero", &balance_t::is_zero) + .def("is_realzero", &balance_t::is_realzero) + + .def("is_empty", &balance_t::is_empty) + .def("single_amount", &balance_t::single_amount) + + .def("to_amount", &balance_t::to_amount) - .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>())) + .def("commodity_count", &balance_t::commodity_count) + .def("commodity_amount", py_commodity_amount_0) + .def("commodity_amount", py_commodity_amount_1) + + .def("strip_annotations", &balance_t::strip_annotations) + + .def("print_", py_print) + .def("dump", &balance_t::dump) + + .def("valid", &balance_t::valid) ; + +#define EXC_TRANSLATE(type) \ + register_exception_translator<type>(&exc_translate_ ## type); + + EXC_TRANSLATE(balance_error); } -#endif + +} // namespace ledger |