Support for GncCommodityNamespace, better support for GncCommodity, and example

enhancements.
Patch supplied my Mark Jenkins.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19955 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Mike Evans
2010-12-17 14:00:52 +00:00
parent 94c55fef95
commit 81a4a8108c
4 changed files with 58 additions and 23 deletions

View File

@@ -11,9 +11,7 @@
# Change, FILE, CURRENCY and STOCK to those defined in your test account.
import gnucash
from gnucash.gnucash_core_c import gnc_pricedb_get_db, gnc_pricedb_get_prices, gnc_pricedb_lookup_latest,gnc_pricedb_print_contents
from gnucash.gnucash_core import *
from gnucash import Session
FILE = "PATH_TO_YOUR_TEST_FILE" ## Fail is no saved but use a copy anyway
session = Session("xml://%s" % FILE, True, False, False)
@@ -27,16 +25,16 @@ arm = comm_table.lookup("NASDAQ", "SOME_STOCK")
latest = pdb.lookup_latest(arm,gbp) # from the table, NOT live data
value = latest.get_value()
pl = pdb.get_prices(arm,gbp)
for i in pl:
pr = GncPrice(instance=i)
for pr in pl:
source = pr.get_source()
time = pr.get_time()
v=pr.get_value()
price = float(v.num)/v.denom
print time, source, price
print arm.get_fullname(), float(v0.num) / float(v0.denom )
if len(pl) > 0:
v0 = pl[0].get_value()
print arm.get_fullname(), float(v0.num) / float(v0.denom )
session.end()
session.destroy()

View File

@@ -10,9 +10,7 @@
# The account file is not saved but always use a disposable copy.
# Thanks for contributions by Christoph Holtermann
import gnucash
from gnucash.gnucash_core_c import gnc_pricedb_get_db, gnc_pricedb_get_prices, gnc_pricedb_lookup_latest,gnc_pricedb_print_contents
from gnucash.gnucash_core import *
from gnucash import Session
# -------------------------------------------
# Configuration options can be changed here :
@@ -37,19 +35,24 @@ comm_table = book.get_table()
cur = comm_table.lookup("CURRENCY", cur_mnemonic)
cur_name = cur.get_fullname()
if namespace_name: # Show single namespace
namespaces=[]
namespace=gnucash.gnucash_core_c.gnc_commodity_table_find_namespace(comm_table.instance,"EUREX")
if namespace_name != "": # Show single namespace
namespaces [ comm_table.find_namespace(namespace_name) ]
else: # Show all namespaces
namespaces=comm_table.get_namespaces_list()
for namespace in namespaces:
namespace_name=gnucash.gnucash_core_c.gnc_commodity_namespace_get_name(namespace)
namespace_name=namespace.get_name()
# Get a list of all commodities in namespace
commodities=comm_table.get_commodities(namespace_name)
if not commodities:
if len(commodities) == 0 :
print "No commodity in namespace "+namespace_name+"."
else:
if commodity_fullname:
@@ -57,9 +60,8 @@ for namespace in namespaces:
else:
print "Commoditys in namespace "+namespace_name+":"
for i in range(len(commodities)):
c=gnucash.GncCommodity(instance=commodities[i])
commodities[i]=c
for i, c in enumerate(commodities):
c_fullname = c.get_fullname()
@@ -67,10 +69,11 @@ for namespace in namespaces:
print "["+str(i)+"] Full Name :", c.get_fullname()
if show_prices:
pl = pdb.get_prices(c,cur)
if pl:
if len(pl) > 0 :
print "{0} {1:20}{2:>10} {3}".format("Time ","Source","Price","Currency")
for i in pl:
pr = GncPrice(instance=i)
for pr in pl:
source = pr.get_source()
time = pr.get_time()
v=pr.get_value()

View File

@@ -124,6 +124,18 @@ def method_function_returns_instance(method_function, cls):
return new_function
def method_function_returns_instance_list(method_function, cls):
def new_function(*args):
return [ cls( **{INSTANCE_ARGUMENT: item} )
for item in method_function(*args) ]
return new_function
def methods_return_instance_lists(cls, function_dict):
for func_name, instance_name in function_dict.iteritems():
setattr(cls, func_name,
method_function_returns_instance_list(
getattr(cls, func_name), instance_name))
def default_arguments_decorator(function, *args):
"""Decorates a function to give it default, positional arguments

View File

@@ -26,7 +26,8 @@ import gnucash_core_c
from function_class import \
ClassFromFunctions, extract_attributes_with_prefix, \
default_arguments_decorator, method_function_returns_instance, \
methods_return_instance, process_list_convert_to_instance
methods_return_instance, process_list_convert_to_instance, \
method_function_returns_instance_list, methods_return_instance_lists
from gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn, \
gncInvoiceGetInvoiceFromLot, gncEntryLookup, gncInvoiceLookup, \
@@ -297,6 +298,8 @@ PriceDB_dict = {
'convert_balance_latest_before' : GncNumeric,
}
methods_return_instance(GncPriceDB,PriceDB_dict)
GncPriceDB.get_prices = method_function_returns_instance_list(
GncPriceDB.get_prices, GncPrice )
class GncCommodity(GnuCashCoreClass): pass
@@ -315,6 +318,9 @@ class GncCommodityTable(GnuCashCoreClass):
pass
class GncCommodityNamespace(GnuCashCoreClass):
pass
class GncLot(GnuCashCoreClass):
def GetInvoiceFromLot(self):
from gnucash_business import Invoice
@@ -486,10 +492,26 @@ commoditytable_dict = {
'lookup' : GncCommodity,
'lookup_unique' : GncCommodity,
'find_full' : GncCommodity,
'insert' : GncCommodity
'insert' : GncCommodity,
'add_namespace': GncCommodityNamespace,
'find_namespace': GncCommodityNamespace,
}
methods_return_instance(GncCommodityTable, commoditytable_dict)
methods_return_instance_lists(
GncCommodityTable, { 'get_namespaces': GncCommodityNamespace,
'get_namespaces_list': GncCommodityNamespace,
'get_commodities': GncCommodity,
'get_quotable_commodities': GncCommodity,
} )
# GncCommodityNamespace
GncCommodityNamespace.add_methods_with_prefix('gnc_commodity_namespace_')
GncCommodityNamespace.get_commodity_list = \
method_function_returns_instance_list(
GncCommodityNamespace.get_commodity_list, GncCommodity )
# GncLot
GncLot.add_constructor_and_methods_with_prefix('gnc_lot_', 'new')