Added Python interface for commodity_pool_t

This commit is contained in:
John Wiegley 2009-11-02 21:07:19 -05:00
parent 46e46dd5a3
commit 4befcfa27d
2 changed files with 119 additions and 2 deletions

View file

@ -222,8 +222,6 @@ protected:
#endif // HAVE_BOOST_SERIALIZATION
};
static bool symbol_needs_quotes(const string& symbol);
shared_ptr<base_t> base;
commodity_pool_t * parent_;
@ -247,6 +245,8 @@ public:
operator bool() const;
static bool symbol_needs_quotes(const string& symbol);
bool is_annotated() const {
return annotated;
}

View file

@ -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)