diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-11 07:22:41 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:43 -0400 |
commit | 1d065bf0dc7822a32632b474e14aa7c36146b427 (patch) | |
tree | d860538567856339fcbfb93d18dfbda6ea16132e /src | |
parent | 64219e948f78fff001a70151d213160cc1685a96 (diff) | |
download | fork-ledger-1d065bf0dc7822a32632b474e14aa7c36146b427.tar.gz fork-ledger-1d065bf0dc7822a32632b474e14aa7c36146b427.tar.bz2 fork-ledger-1d065bf0dc7822a32632b474e14aa7c36146b427.zip |
Copied over all of the C++ tests to Python again.
Diffstat (limited to 'src')
-rw-r--r-- | src/commodity.h | 4 | ||||
-rw-r--r-- | src/py_amount.cc | 71 | ||||
-rw-r--r-- | src/py_commodity.cc | 75 | ||||
-rw-r--r-- | src/py_utils.cc | 61 | ||||
-rw-r--r-- | src/pyledger.cc | 2 |
5 files changed, 125 insertions, 88 deletions
diff --git a/src/commodity.h b/src/commodity.h index 6551d3b5..a5a13aeb 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -49,7 +49,7 @@ class annotated_commodity_t; class commodity_t : public delegates_flags<>, - equality_comparable1<commodity_t, noncopyable> + public equality_comparable1<commodity_t, noncopyable> { friend class commodity_pool_t; @@ -255,7 +255,7 @@ inline std::ostream& operator<<(std::ostream& out, const annotation_t& details) class annotated_commodity_t : public commodity_t, - equality_comparable<annotated_commodity_t, + public equality_comparable<annotated_commodity_t, equality_comparable2<annotated_commodity_t, commodity_t, noncopyable> > { diff --git a/src/py_amount.cc b/src/py_amount.cc index 7fc667e7..88e2c993 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -40,6 +40,13 @@ namespace ledger { using namespace boost::python; +amount_t py_round_0(const amount_t& amount) { + return amount.round(); +} +amount_t py_round_1(const amount_t& amount, amount_t::precision_t prec) { + return amount.round(prec); +} + double py_to_double_0(amount_t& amount) { return amount.to_double(); } @@ -54,26 +61,46 @@ long py_to_long_1(amount_t& amount, bool no_check) { return amount.to_long(no_check); } -void py_parse_1(amount_t& amount, const string& str) { - amount.parse(str); +boost::optional<amount_t> py_value_0(const amount_t& amount) { + return amount.value(); } -void py_parse_2(amount_t& amount, const string& str, unsigned char flags) { - amount.parse(str, flags); +boost::optional<amount_t> py_value_1(const amount_t& amount, + const boost::optional<moment_t>& moment) { + return amount.value(moment); } -amount_t py_round_0(const amount_t& amount) { - return amount.round(); +void py_parse_2(amount_t& amount, object in, unsigned char flags) { + if (PyFile_Check(in.ptr())) { + pyifstream instr(reinterpret_cast<PyFileObject *>(in.ptr())); + amount.parse(instr, flags); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.parse(file) is not a file object"); + } } -amount_t py_round_1(const amount_t& amount, amount_t::precision_t prec) { - return amount.round(prec); +void py_parse_1(amount_t& amount, object in) { + py_parse_2(amount, in, 0); } -boost::optional<amount_t> py_value_0(const amount_t& amount) { - return amount.value(); +void py_parse_str_1(amount_t& amount, const string& str) { + amount.parse(str); } -boost::optional<amount_t> py_value_1(const amount_t& amount, - const boost::optional<moment_t>& moment) { - return amount.value(moment); +void py_parse_str_2(amount_t& amount, const string& str, unsigned char flags) { + amount.parse(str, flags); +} + +void py_read_1(amount_t& amount, object in) { + if (PyFile_Check(in.ptr())) { + pyifstream instr(reinterpret_cast<PyFileObject *>(in.ptr())); + amount.read(instr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.parse(file) is not a file object"); + } +} +void py_read_2(amount_t& amount, const std::string& str) { + const char * p = str.c_str(); + amount.read(p); } #define EXC_TRANSLATOR(type) \ @@ -112,7 +139,9 @@ void export_amount() .def(init<long>()) .def(init<std::string>()) - .def("exact", &amount_t::exact) + .def("exact", &amount_t::exact, args("value"), + "Construct an amount object whose display precision is always equal to its\n\ +internal precision.") .staticmethod("exact") .def(init<amount_t>()) @@ -197,7 +226,7 @@ void export_amount() .def(self / double()) .def(double() / self) - .def("precision", &amount_t::precision) + .add_property("precision", &amount_t::precision) .def("negate", &amount_t::negate) .def("in_place_negate", &amount_t::in_place_negate, @@ -243,7 +272,7 @@ void export_amount() .def("fits_in_double", &amount_t::fits_in_double) .def("fits_in_long", &amount_t::fits_in_long) - .def("quantity_string", &amount_t::quantity_string) + .add_property("quantity_string", &amount_t::quantity_string) .add_property("commodity", make_function(&amount_t::commodity, @@ -253,19 +282,25 @@ void export_amount() .def("has_commodity", &amount_t::has_commodity) .def("clear_commodity", &amount_t::clear_commodity) - .def("number", &amount_t::number) + .add_property("number", &amount_t::number) .def("annotate_commodity", &amount_t::annotate_commodity) .def("commodity_annotated", &amount_t::commodity_annotated) - .def("annotation_details", &amount_t::annotation_details) + .add_property("annotation_details", &amount_t::annotation_details) .def("strip_annotations", &amount_t::strip_annotations) .def("parse", py_parse_1) .def("parse", py_parse_2) + .def("parse", py_parse_str_1) + .def("parse", py_parse_str_2) .def("parse_conversion", &amount_t::parse_conversion) .staticmethod("parse_conversion") + .def("read", py_read_1) + .def("read", py_read_2) + .def("write", &amount_t::write) + .def("valid", &amount_t::valid) ; diff --git a/src/py_commodity.cc b/src/py_commodity.cc index dc065567..f857a448 100644 --- a/src/py_commodity.cc +++ b/src/py_commodity.cc @@ -30,47 +30,18 @@ */ #include "pyinterp.h" +#include "pyutils.h" #include "amount.h" #include <boost/python/exception_translator.hpp> +#include <boost/python/implicit.hpp> namespace ledger { using namespace boost::python; -struct commodity_updater_wrap : public commodity_base_t::updater_t -{ - PyObject * self; - commodity_updater_wrap(PyObject * self_) : self(self_) {} - - virtual void operator()(commodity_base_t& commodity, - const moment_t& moment, - const moment_t& date, - const moment_t& last, - amount_t& price) { - call_method<void>(self, "__call__", commodity, moment, date, last, price); - } -}; - -commodity_t * py_find_commodity(const string& symbol) -{ - return commodity_t::find(symbol); -} - -#define EXC_TRANSLATOR(type) \ - void exc_translate_ ## type(const type& err) { \ - PyErr_SetString(PyExc_ArithmeticError, err.what()); \ - } - -EXC_TRANSLATOR(commodity_error) - void export_commodity() { - class_< commodity_base_t::updater_t, commodity_updater_wrap, - boost::noncopyable > - ("updater") - ; - scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; scope().attr("COMMODITY_STYLE_SUFFIXED") = COMMODITY_STYLE_SUFFIXED; scope().attr("COMMODITY_STYLE_SEPARATED") = COMMODITY_STYLE_SEPARATED; @@ -79,46 +50,14 @@ void export_commodity() scope().attr("COMMODITY_STYLE_NOMARKET") = COMMODITY_STYLE_NOMARKET; scope().attr("COMMODITY_STYLE_BUILTIN") = COMMODITY_STYLE_BUILTIN; - class_< commodity_t > ("commodity") - .add_property("symbol", &commodity_t::symbol) - - .add_property("name", &commodity_t::name, &commodity_t::set_name) - .add_property("note", &commodity_t::note, &commodity_t::set_note) - .add_property("precision", &commodity_t::precision, - &commodity_t::set_precision) - .add_property("flags", &commodity_t::flags, &commodity_t::set_flags) - .add_property("add_flags", &commodity_t::add_flags) - .add_property("drop_flags", &commodity_t::drop_flags) - //.add_property("updater", &commodity_t::updater) - - .add_property("smaller", - make_getter(&commodity_t::smaller, - return_value_policy<reference_existing_object>()), - make_setter(&commodity_t::smaller, - return_value_policy<reference_existing_object>())) - .add_property("larger", - make_getter(&commodity_t::larger, - return_value_policy<reference_existing_object>()), - make_setter(&commodity_t::larger, - return_value_policy<reference_existing_object>())) + class_< commodity_t, bases<>, + commodity_t, boost::noncopyable > ("commodity", no_init) + .def(self == self) - .def(self_ns::str(self)) + .def("drop_flags", &commodity_t::drop_flags) - .def("find", py_find_commodity, - return_value_policy<reference_existing_object>()) - .staticmethod("find") - - .def("add_price", &commodity_t::add_price) - .def("remove_price", &commodity_t::remove_price) - .def("value", &commodity_t::value) - - .def("valid", &commodity_t::valid) + .add_property("precision", &commodity_t::precision) ; - -#define EXC_TRANSLATE(type) \ - register_exception_translator<type>(&exc_translate_ ## type); - - EXC_TRANSLATE(commodity_error); } } // namespace ledger diff --git a/src/py_utils.cc b/src/py_utils.cc index ea439297..09fb9740 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -31,6 +31,7 @@ #include "pyinterp.h" #include "pyutils.h" +#include "pyfstream.h" #include <boost/python/module.hpp> #include <boost/python/def.hpp> @@ -74,6 +75,7 @@ struct bool_from_python typedef register_python_conversion<bool, bool_to_python, bool_from_python> bool_python_conversion; + struct string_to_python { static PyObject* convert(const string& str) @@ -103,10 +105,69 @@ struct string_from_python typedef register_python_conversion<string, string_to_python, string_from_python> string_python_conversion; + +struct istream_to_python +{ + static PyObject* convert(const std::istream& str) + { + return incref(boost::python::detail::none()); + } +}; + +struct istream_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyFile_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage<pyifstream>*) data)->storage.bytes; + new (storage) pyifstream(reinterpret_cast<PyFileObject *>(obj_ptr)); + data->convertible = storage; + } +}; + +typedef register_python_conversion<std::istream, istream_to_python, istream_from_python> + istream_python_conversion; + + +struct ostream_to_python +{ + static PyObject* convert(const std::ostream& str) + { + return incref(boost::python::detail::none()); + } +}; + +struct ostream_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyFile_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage<pyofstream>*) data)->storage.bytes; + new (storage) pyofstream(reinterpret_cast<PyFileObject *>(obj_ptr)); + data->convertible = storage; + } +}; + +typedef register_python_conversion<std::ostream, ostream_to_python, ostream_from_python> + ostream_python_conversion; + + void export_utils() { bool_python_conversion(); string_python_conversion(); + istream_python_conversion(); + ostream_python_conversion(); } } // namespace ledger diff --git a/src/pyledger.cc b/src/pyledger.cc index f4adae53..4c2cd96e 100644 --- a/src/pyledger.cc +++ b/src/pyledger.cc @@ -38,6 +38,7 @@ namespace ledger { void export_utils(); void export_times(); void export_amount(); +void export_commodity(); #if 0 void export_balance(); void export_value(); @@ -55,6 +56,7 @@ void initialize_for_python() export_utils(); export_times(); export_amount(); + export_commodity(); #if 0 export_balance(); export_value(); |