summaryrefslogtreecommitdiff
path: root/src/py_commodity.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-02 21:07:19 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-02 21:55:47 -0500
commit4befcfa27d1e4b162512f6161f9f897862ccdf57 (patch)
tree4903d223c3df16bb14ce95180b2db67b38c91f4d /src/py_commodity.cc
parent46e46dd5a337c009391583a679d6172cf4f5aa42 (diff)
downloadfork-ledger-4befcfa27d1e4b162512f6161f9f897862ccdf57.tar.gz
fork-ledger-4befcfa27d1e4b162512f6161f9f897862ccdf57.tar.bz2
fork-ledger-4befcfa27d1e4b162512f6161f9f897862ccdf57.zip
Added Python interface for commodity_pool_t
Diffstat (limited to 'src/py_commodity.cc')
-rw-r--r--src/py_commodity.cc117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/py_commodity.cc b/src/py_commodity.cc
index 4932df97..a39d1408 100644
--- a/src/py_commodity.cc
+++ b/src/py_commodity.cc
@@ -33,6 +33,8 @@
#include "pyinterp.h"
#include "commodity.h"
+#include "annotate.h"
+#include "pool.h"
namespace ledger {
@@ -45,8 +47,118 @@ void py_add_price(commodity_t& commodity,
commodity.add_price(date, price);
}
+namespace {
+
+ commodity_t * py_create_1(commodity_pool_t& pool,
+ const string& symbol)
+ {
+ return pool.create(symbol);
+ }
+ commodity_t * py_create_2(commodity_pool_t& pool,
+ const string& symbol,
+ const annotation_t& details)
+ {
+ return pool.create(symbol, details);
+ }
+
+ commodity_t * py_find_or_create_1(commodity_pool_t& pool,
+ const string& symbol)
+ {
+ return pool.find_or_create(symbol);
+ }
+ commodity_t * py_find_or_create_2(commodity_pool_t& pool,
+ const string& symbol,
+ const annotation_t& details)
+ {
+ return pool.find_or_create(symbol, details);
+ }
+
+ commodity_t * py_find_1(commodity_pool_t& pool,
+ const string& name)
+ {
+ return pool.find(name);
+ }
+
+ commodity_t * py_find_2(commodity_pool_t& pool,
+ const string& symbol,
+ const annotation_t& details)
+ {
+ return pool.find(symbol, details);
+ }
+
+ // Exchange one commodity for another, while recording the factored price.
+
+ void py_exchange_3(commodity_pool_t& pool,
+ commodity_t& commodity,
+ const amount_t& per_unit_cost,
+ const datetime_t& moment)
+ {
+ pool.exchange(commodity, per_unit_cost, moment);
+ }
+
+ cost_breakdown_t py_exchange_5(commodity_pool_t& pool,
+ const amount_t& amount,
+ const amount_t& cost,
+ const bool is_per_unit,
+ const boost::optional<datetime_t>& moment,
+ const boost::optional<string>& tag)
+ {
+ return pool.exchange(amount, cost, is_per_unit, moment, tag);
+ }
+
+} // unnamed namespace
+
void export_commodity()
{
+ class_< commodity_pool_t, boost::noncopyable > ("CommodityPool", no_init)
+ .add_property("null_commodity",
+ make_getter(&commodity_pool_t::null_commodity,
+ return_value_policy<reference_existing_object>()),
+ make_setter(&commodity_pool_t::null_commodity,
+ with_custodian_and_ward<1, 2>()))
+ .add_property("default_commodity",
+ make_getter(&commodity_pool_t::default_commodity,
+ return_value_policy<reference_existing_object>()),
+ make_setter(&commodity_pool_t::default_commodity,
+ with_custodian_and_ward<1, 2>()))
+
+ .add_property("keep_base",
+ make_getter(&commodity_pool_t::keep_base),
+ make_setter(&commodity_pool_t::keep_base))
+ .add_property("price_db",
+ make_getter(&commodity_pool_t::price_db),
+ make_setter(&commodity_pool_t::price_db))
+ .add_property("quote_leeway",
+ make_getter(&commodity_pool_t::quote_leeway),
+ make_setter(&commodity_pool_t::quote_leeway))
+ .add_property("get_quotes",
+ make_getter(&commodity_pool_t::get_quotes),
+ make_setter(&commodity_pool_t::get_quotes))
+ .add_property("get_commodity_quote",
+ make_getter(&commodity_pool_t::get_commodity_quote),
+ make_setter(&commodity_pool_t::get_commodity_quote))
+
+ .def("make_qualified_name", &commodity_pool_t::make_qualified_name)
+
+ .def("create", py_create_1, return_value_policy<reference_existing_object>())
+ .def("create", py_create_2, return_value_policy<reference_existing_object>())
+
+ .def("find_or_create", py_find_or_create_1,
+ return_value_policy<reference_existing_object>())
+ .def("find_or_create", py_find_or_create_2,
+ return_value_policy<reference_existing_object>())
+
+ .def("find", py_find_1, return_value_policy<reference_existing_object>())
+ .def("find", py_find_2, return_value_policy<reference_existing_object>())
+
+ .def("exchange", py_exchange_3, with_custodian_and_ward<1, 2>())
+ .def("exchange", py_exchange_5)
+
+ .def("parse_price_directive", &commodity_pool_t::parse_price_directive)
+ .def("parse_price_expression", &commodity_pool_t::parse_price_expression,
+ return_value_policy<reference_existing_object>())
+ ;
+
scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS;
scope().attr("COMMODITY_STYLE_SUFFIXED") = COMMODITY_STYLE_SUFFIXED;
scope().attr("COMMODITY_STYLE_SEPARATED") = COMMODITY_STYLE_SEPARATED;
@@ -55,9 +167,14 @@ void export_commodity()
scope().attr("COMMODITY_NOMARKET") = COMMODITY_NOMARKET;
scope().attr("COMMODITY_BUILTIN") = COMMODITY_BUILTIN;
scope().attr("COMMODITY_WALKED") = COMMODITY_WALKED;
+ scope().attr("COMMODITY_KNOWN") = COMMODITY_KNOWN;
+ scope().attr("COMMODITY_PRIMARY") = COMMODITY_PRIMARY;
class_< commodity_t, bases<>,
commodity_t, boost::noncopyable > ("Commodity", no_init)
+ .def("symbol_needs_quotes", &commodity_t::symbol_needs_quotes)
+ .staticmethod("symbol_needs_quotes")
+
.def(self == self)
.def("drop_flags", &commodity_t::drop_flags)