Added Python interface for commodity_t
This commit is contained in:
parent
4befcfa27d
commit
690e46117e
9 changed files with 123 additions and 39 deletions
|
|
@ -770,7 +770,7 @@ commodity_t& amount_t::commodity() const
|
||||||
|
|
||||||
bool amount_t::has_commodity() const
|
bool amount_t::has_commodity() const
|
||||||
{
|
{
|
||||||
return commodity_ && commodity_ != commodity_->parent().null_commodity;
|
return commodity_ && commodity_ != commodity_->pool().null_commodity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void amount_t::annotate(const annotation_t& details)
|
void amount_t::annotate(const annotation_t& details)
|
||||||
|
|
@ -795,7 +795,7 @@ void amount_t::annotate(const annotation_t& details)
|
||||||
<< *this << std::endl << details);
|
<< *this << std::endl << details);
|
||||||
|
|
||||||
if (commodity_t * ann_comm =
|
if (commodity_t * ann_comm =
|
||||||
this_base->parent().find_or_create(*this_base, details))
|
this_base->pool().find_or_create(*this_base, details))
|
||||||
set_commodity(*ann_comm);
|
set_commodity(*ann_comm);
|
||||||
#ifdef ASSERTS_ON
|
#ifdef ASSERTS_ON
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -185,12 +185,12 @@ annotated_commodity_t::strip_annotations(const keep_details_t& what_to_keep)
|
||||||
(keep_date && details.date) ||
|
(keep_date && details.date) ||
|
||||||
(keep_tag && details.tag))
|
(keep_tag && details.tag))
|
||||||
{
|
{
|
||||||
new_comm = parent().find_or_create
|
new_comm = pool().find_or_create
|
||||||
(referent(), annotation_t(keep_price ? details.price : none,
|
(referent(), annotation_t(keep_price ? details.price : none,
|
||||||
keep_date ? details.date : none,
|
keep_date ? details.date : none,
|
||||||
keep_tag ? details.tag : none));
|
keep_tag ? details.tag : none));
|
||||||
} else {
|
} else {
|
||||||
new_comm = parent().find_or_create(base_symbol());
|
new_comm = pool().find_or_create(base_symbol());
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(new_comm);
|
assert(new_comm);
|
||||||
|
|
@ -199,7 +199,7 @@ annotated_commodity_t::strip_annotations(const keep_details_t& what_to_keep)
|
||||||
|
|
||||||
void annotated_commodity_t::write_annotations(std::ostream& out) const
|
void annotated_commodity_t::write_annotations(std::ostream& out) const
|
||||||
{
|
{
|
||||||
details.print(out, parent().keep_base);
|
details.print(out, pool().keep_base);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
|
||||||
|
|
@ -375,7 +375,7 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
|
||||||
const optional<datetime_t>& moment,
|
const optional<datetime_t>& moment,
|
||||||
const optional<commodity_t&>& in_terms_of)
|
const optional<commodity_t&>& in_terms_of)
|
||||||
{
|
{
|
||||||
if (parent().get_quotes && ! has_flags(COMMODITY_NOMARKET)) {
|
if (pool().get_quotes && ! has_flags(COMMODITY_NOMARKET)) {
|
||||||
bool exceeds_leeway = true;
|
bool exceeds_leeway = true;
|
||||||
|
|
||||||
if (point) {
|
if (point) {
|
||||||
|
|
@ -389,8 +389,8 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
|
||||||
DEBUG("commodity.download", "slip.now = " << seconds_diff);
|
DEBUG("commodity.download", "slip.now = " << seconds_diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG("commodity.download", "leeway = " << parent().quote_leeway);
|
DEBUG("commodity.download", "leeway = " << pool().quote_leeway);
|
||||||
if (seconds_diff < parent().quote_leeway)
|
if (seconds_diff < pool().quote_leeway)
|
||||||
exceeds_leeway = false;
|
exceeds_leeway = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -398,7 +398,7 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
|
||||||
DEBUG("commodity.download",
|
DEBUG("commodity.download",
|
||||||
"attempting to download a more current quote...");
|
"attempting to download a more current quote...");
|
||||||
if (optional<price_point_t> quote =
|
if (optional<price_point_t> quote =
|
||||||
parent().get_commodity_quote(*this, in_terms_of)) {
|
pool().get_commodity_quote(*this, in_terms_of)) {
|
||||||
if (! in_terms_of ||
|
if (! in_terms_of ||
|
||||||
(quote->price.has_commodity() &&
|
(quote->price.has_commodity() &&
|
||||||
quote->price.commodity() == *in_terms_of))
|
quote->price.commodity() == *in_terms_of))
|
||||||
|
|
@ -411,7 +411,7 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
|
||||||
|
|
||||||
commodity_t::operator bool() const
|
commodity_t::operator bool() const
|
||||||
{
|
{
|
||||||
return this != parent().null_commodity;
|
return this != pool().null_commodity;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool commodity_t::symbol_needs_quotes(const string& symbol)
|
bool commodity_t::symbol_needs_quotes(const string& symbol)
|
||||||
|
|
@ -568,7 +568,7 @@ void commodity_t::parse_symbol(char *& p, string& symbol)
|
||||||
|
|
||||||
bool commodity_t::valid() const
|
bool commodity_t::valid() const
|
||||||
{
|
{
|
||||||
if (symbol().empty() && this != parent().null_commodity) {
|
if (symbol().empty() && this != pool().null_commodity) {
|
||||||
DEBUG("ledger.validate",
|
DEBUG("ledger.validate",
|
||||||
"commodity_t: symbol().empty() && this != null_commodity");
|
"commodity_t: symbol().empty() && this != null_commodity");
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -245,30 +245,31 @@ public:
|
||||||
|
|
||||||
operator bool() const;
|
operator bool() const;
|
||||||
|
|
||||||
static bool symbol_needs_quotes(const string& symbol);
|
|
||||||
|
|
||||||
bool is_annotated() const {
|
|
||||||
return annotated;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool operator==(const commodity_t& comm) const {
|
virtual bool operator==(const commodity_t& comm) const {
|
||||||
if (comm.annotated)
|
if (comm.annotated)
|
||||||
return comm == *this;
|
return comm == *this;
|
||||||
return base.get() == comm.base.get();
|
return base.get() == comm.base.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool symbol_needs_quotes(const string& symbol);
|
||||||
|
|
||||||
virtual commodity_t& referent() {
|
virtual commodity_t& referent() {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
virtual const commodity_t& referent() const {
|
virtual const commodity_t& referent() const {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_annotated() const {
|
||||||
|
return annotated;
|
||||||
|
}
|
||||||
|
|
||||||
virtual commodity_t& strip_annotations(const keep_details_t&) {
|
virtual commodity_t& strip_annotations(const keep_details_t&) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
virtual void write_annotations(std::ostream&) const {}
|
virtual void write_annotations(std::ostream&) const {}
|
||||||
|
|
||||||
commodity_pool_t& parent() const {
|
commodity_pool_t& pool() const {
|
||||||
return *parent_;
|
return *parent_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -329,7 +330,6 @@ public:
|
||||||
|
|
||||||
optional<history_t&> history(const optional<commodity_t&>& commodity);
|
optional<history_t&> history(const optional<commodity_t&>& commodity);
|
||||||
|
|
||||||
public:
|
|
||||||
// These methods provide a transparent pass-through to the underlying
|
// These methods provide a transparent pass-through to the underlying
|
||||||
// base->varied_history object.
|
// base->varied_history object.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ string commodity_pool_t::make_qualified_name(const commodity_t& comm,
|
||||||
|
|
||||||
std::ostringstream name;
|
std::ostringstream name;
|
||||||
comm.print(name);
|
comm.print(name);
|
||||||
details.print(name, comm.parent().keep_base);
|
details.print(name, comm.pool().keep_base);
|
||||||
|
|
||||||
DEBUG("amounts.commodities", "make_qualified_name for "
|
DEBUG("amounts.commodities", "make_qualified_name for "
|
||||||
<< *comm.qualified_symbol << std::endl << details);
|
<< *comm.qualified_symbol << std::endl << details);
|
||||||
|
|
|
||||||
|
|
@ -282,9 +282,6 @@ internal precision."))
|
||||||
.def("parse_conversion", &amount_t::parse_conversion)
|
.def("parse_conversion", &amount_t::parse_conversion)
|
||||||
.staticmethod("parse_conversion")
|
.staticmethod("parse_conversion")
|
||||||
|
|
||||||
.def("print_", py_print)
|
|
||||||
.def("dump", &amount_t::dump)
|
|
||||||
|
|
||||||
.def("valid", &amount_t::valid)
|
.def("valid", &amount_t::valid)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -211,9 +211,6 @@ void export_balance()
|
||||||
|
|
||||||
.def("strip_annotations", &balance_t::strip_annotations)
|
.def("strip_annotations", &balance_t::strip_annotations)
|
||||||
|
|
||||||
.def("print_", py_print)
|
|
||||||
.def("dump", &balance_t::dump)
|
|
||||||
|
|
||||||
.def("valid", &balance_t::valid)
|
.def("valid", &balance_t::valid)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,13 +40,6 @@ namespace ledger {
|
||||||
|
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
|
|
||||||
void py_add_price(commodity_t& commodity,
|
|
||||||
const datetime_t& date,
|
|
||||||
const amount_t& price)
|
|
||||||
{
|
|
||||||
commodity.add_price(date, price);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
commodity_t * py_create_1(commodity_pool_t& pool,
|
commodity_t * py_create_1(commodity_pool_t& pool,
|
||||||
|
|
@ -106,6 +99,16 @@ namespace {
|
||||||
return pool.exchange(amount, cost, is_per_unit, moment, tag);
|
return pool.exchange(amount, cost, is_per_unit, moment, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void py_add_price_2(commodity_t& commodity,
|
||||||
|
const datetime_t& date, const amount_t& price) {
|
||||||
|
commodity.add_price(date, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
void py_add_price_3(commodity_t& commodity, const datetime_t& date,
|
||||||
|
const amount_t& price, const bool reflexive) {
|
||||||
|
commodity.add_price(date, price, reflexive);
|
||||||
|
}
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
void export_commodity()
|
void export_commodity()
|
||||||
|
|
@ -170,18 +173,61 @@ void export_commodity()
|
||||||
scope().attr("COMMODITY_KNOWN") = COMMODITY_KNOWN;
|
scope().attr("COMMODITY_KNOWN") = COMMODITY_KNOWN;
|
||||||
scope().attr("COMMODITY_PRIMARY") = COMMODITY_PRIMARY;
|
scope().attr("COMMODITY_PRIMARY") = COMMODITY_PRIMARY;
|
||||||
|
|
||||||
class_< commodity_t, bases<>,
|
class_< commodity_t, boost::noncopyable > ("Commodity", no_init)
|
||||||
commodity_t, boost::noncopyable > ("Commodity", no_init)
|
#if 1
|
||||||
.def("symbol_needs_quotes", &commodity_t::symbol_needs_quotes)
|
.def("flags", &delegates_flags<uint_least16_t>::flags)
|
||||||
.staticmethod("symbol_needs_quotes")
|
.def("has_flags", &delegates_flags<uint_least16_t>::has_flags)
|
||||||
|
.def("set_flags", &delegates_flags<uint_least16_t>::set_flags)
|
||||||
|
.def("clear_flags", &delegates_flags<uint_least16_t>::clear_flags)
|
||||||
|
.def("add_flags", &delegates_flags<uint_least16_t>::add_flags)
|
||||||
|
.def("drop_flags", &delegates_flags<uint_least16_t>::drop_flags)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
.add_static_property("european_by_default",
|
||||||
|
make_getter(&commodity_t::european_by_default),
|
||||||
|
make_setter(&commodity_t::european_by_default))
|
||||||
|
|
||||||
|
.def("__nonzero__", &commodity_t::operator bool)
|
||||||
|
|
||||||
.def(self == self)
|
.def(self == self)
|
||||||
|
|
||||||
.def("drop_flags", &commodity_t::drop_flags)
|
.def("symbol_needs_quotes", &commodity_t::symbol_needs_quotes)
|
||||||
|
.staticmethod("symbol_needs_quotes")
|
||||||
|
|
||||||
.def("add_price", py_add_price)
|
#if 0
|
||||||
|
.def("referent", &commodity_t::referent,
|
||||||
|
return_value_policy<reference_existing_object>())
|
||||||
|
#endif
|
||||||
|
|
||||||
|
.def("is_annotated", &commodity_t::is_annotated)
|
||||||
|
.def("strip_annotations", &commodity_t::strip_annotations,
|
||||||
|
return_value_policy<reference_existing_object>())
|
||||||
|
.def("write_annotations", &commodity_t::write_annotations)
|
||||||
|
|
||||||
|
.def("pool", &commodity_t::pool,
|
||||||
|
return_value_policy<reference_existing_object>())
|
||||||
|
|
||||||
|
.def("base_symbol", &commodity_t::base_symbol)
|
||||||
|
.def("symbol", &commodity_t::symbol)
|
||||||
|
.def("mapping_key", &commodity_t::mapping_key)
|
||||||
|
|
||||||
|
.def("name", &commodity_t::name)
|
||||||
|
.def("set_name", &commodity_t::set_name)
|
||||||
|
.def("note", &commodity_t::note)
|
||||||
|
.def("set_note", &commodity_t::set_note)
|
||||||
.def("precision", &commodity_t::precision)
|
.def("precision", &commodity_t::precision)
|
||||||
|
.def("set_precision", &commodity_t::set_precision)
|
||||||
|
.def("smaller", &commodity_t::smaller)
|
||||||
|
.def("set_smaller", &commodity_t::set_smaller)
|
||||||
|
.def("larger", &commodity_t::larger)
|
||||||
|
.def("set_larger", &commodity_t::set_larger)
|
||||||
|
|
||||||
|
.def("add_price", py_add_price_2)
|
||||||
|
.def("add_price", py_add_price_3)
|
||||||
|
.def("remove_price", &commodity_t::remove_price,
|
||||||
|
with_custodian_and_ward<1, 3>())
|
||||||
|
.def("find_price", &commodity_t::find_price)
|
||||||
|
.def("check_for_updated_price", &commodity_t::check_for_updated_price)
|
||||||
;
|
;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,50 @@ typedef register_python_conversion<std::ostream, ostream_to_python, ostream_from
|
||||||
|
|
||||||
void export_utils()
|
void export_utils()
|
||||||
{
|
{
|
||||||
|
class_< supports_flags<uint_least8_t> > ("SupportFlags8")
|
||||||
|
.def(init<supports_flags<uint_least8_t> >())
|
||||||
|
.def(init<uint_least8_t>())
|
||||||
|
|
||||||
|
.def("flags", &supports_flags<uint_least8_t>::flags)
|
||||||
|
.def("has_flags", &supports_flags<uint_least8_t>::has_flags)
|
||||||
|
.def("set_flags", &supports_flags<uint_least8_t>::set_flags)
|
||||||
|
.def("clear_flags", &supports_flags<uint_least8_t>::clear_flags)
|
||||||
|
.def("add_flags", &supports_flags<uint_least8_t>::add_flags)
|
||||||
|
.def("drop_flags", &supports_flags<uint_least8_t>::drop_flags)
|
||||||
|
;
|
||||||
|
|
||||||
|
class_< supports_flags<uint_least16_t> > ("SupportFlags16")
|
||||||
|
.def(init<supports_flags<uint_least16_t> >())
|
||||||
|
.def(init<uint_least16_t>())
|
||||||
|
|
||||||
|
.def("flags", &supports_flags<uint_least16_t>::flags)
|
||||||
|
.def("has_flags", &supports_flags<uint_least16_t>::has_flags)
|
||||||
|
.def("set_flags", &supports_flags<uint_least16_t>::set_flags)
|
||||||
|
.def("clear_flags", &supports_flags<uint_least16_t>::clear_flags)
|
||||||
|
.def("add_flags", &supports_flags<uint_least16_t>::add_flags)
|
||||||
|
.def("drop_flags", &supports_flags<uint_least16_t>::drop_flags)
|
||||||
|
;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
class_< basic_flags_t<uint_least8_t>,
|
||||||
|
bases<supports_flags<uint_least8_t> > > ("BasicFlags8")
|
||||||
|
.def(init<uint_least8_t>())
|
||||||
|
|
||||||
|
.def("plus_flags", &basic_flags_t<uint_least8_t>::plus_flags)
|
||||||
|
.def("minus_flags", &basic_flags_t<uint_least8_t>::minus_flags)
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class_< delegates_flags<uint_least16_t>,
|
||||||
|
boost::noncopyable > ("DelegatesFlags16", no_init)
|
||||||
|
.def("flags", &delegates_flags<uint_least16_t>::flags)
|
||||||
|
.def("has_flags", &delegates_flags<uint_least16_t>::has_flags)
|
||||||
|
.def("set_flags", &delegates_flags<uint_least16_t>::set_flags)
|
||||||
|
.def("clear_flags", &delegates_flags<uint_least16_t>::clear_flags)
|
||||||
|
.def("add_flags", &delegates_flags<uint_least16_t>::add_flags)
|
||||||
|
.def("drop_flags", &delegates_flags<uint_least16_t>::drop_flags)
|
||||||
|
;
|
||||||
|
|
||||||
bool_python_conversion();
|
bool_python_conversion();
|
||||||
string_python_conversion();
|
string_python_conversion();
|
||||||
istream_python_conversion();
|
istream_python_conversion();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue