r19954 - gnucash/trunk/src/optional/python-bindings/example_scripts - Changes to example Script to include added namespace functionality

Mike Evans mikee at code.gnucash.org
Fri Dec 17 08:39:49 EST 2010


Author: mikee
Date: 2010-12-17 08:39:49 -0500 (Fri, 17 Dec 2010)
New Revision: 19954
Trac: http://svn.gnucash.org/trac/changeset/19954

Modified:
   gnucash/trunk/src/optional/python-bindings/example_scripts/price_database_example.py
Log:
Changes to example Script to include added namespace functionality
This patch changes the example script to reflect the capability of reading all
namespaces.
Patch provided by Christoph Holtermann.

Modified: gnucash/trunk/src/optional/python-bindings/example_scripts/price_database_example.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/example_scripts/price_database_example.py	2010-12-17 13:28:54 UTC (rev 19953)
+++ gnucash/trunk/src/optional/python-bindings/example_scripts/price_database_example.py	2010-12-17 13:39:49 UTC (rev 19954)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-# Test file for price database stuff
+# Another test file for price database stuff
 # To update the price database call
 # $PATH/gnucash  --add-price-quotes $PATHTOFILE
 # before running this.
@@ -8,7 +8,6 @@
 #  OR:  export PYTHONPATH=$HOME/progs/lib/python2.6/site-packages
 # Then: gnucash-env ipython
 # The account file is not saved but always use a disposable copy.
-# Change, FILE, CURRENCY and STOCK to those defined in your test account.
 # Thanks for contributions by Christoph Holtermann
 
 import gnucash
@@ -18,10 +17,10 @@
 # -------------------------------------------
 # Configuration options can be changed here :
 
-cur_mnemonic="EUR"                      # Possibilities include "EUR","GBP"
-namespace = "EUREX"                     # Predefined namespaces are "CURRENCY","EUREX","AMEX","template"
-show_prices = True
-commodity_fullname = "ThyssenKrupp AG"  # If no name is given, all commoditys in namespace will be printed out
+cur_mnemonic="EUR"                      # Currency that prices are shown in. Possibilities include "EUR","GBP",...
+namespace_name = ""                     # If no namespace_name is set, all namespaces will be shown
+show_prices = True                      # If True, all prices for commodity are shown
+commodity_fullname = ""                 # If no name is given, all commoditys in namespace will be shown
 FILE = "PATH_TO_YOUR_TEST_FILE"         # File is not saved but use a copy anyway
 url = "xml://"+FILE
 
@@ -38,40 +37,48 @@
 cur = comm_table.lookup("CURRENCY", cur_mnemonic)
 cur_name = cur.get_fullname()
 
-# Get a list of all commodities in namespace
-commodities=comm_table.get_commodities(namespace)
+if namespace_name:                    # Show single namespace
+  namespaces=[]
+  namespace=gnucash.gnucash_core_c.gnc_commodity_table_find_namespace(comm_table.instance,"EUREX")
+else:                                 # Show all namespaces
+  namespaces=comm_table.get_namespaces_list()
 
-if not commodities:
-  print "No commodity in namespace "+namespace+" !"
-  quit()
+for namespace in namespaces:
+  namespace_name=gnucash.gnucash_core_c.gnc_commodity_namespace_get_name(namespace)
 
-if commodity_fullname:
-  print "Searching commodity '"+commodity_fullname+"' in namespace "+namespace
-else:
-  print "Commoditys in namespace "+namespace+":"
+  # Get a list of all commodities in namespace
+  commodities=comm_table.get_commodities(namespace_name)
 
-for i in range(len(commodities)):
-  c=gnucash.GncCommodity(instance=commodities[i])
-  commodities[i]=c
+  if not commodities:
+    print "No commodity in namespace "+namespace_name+"."
+  else:
+    if commodity_fullname:
+      print "Searching commodity '"+commodity_fullname+"' in namespace "+namespace_name
+    else:
+      print "Commoditys in namespace "+namespace_name+":"
 
-  c_fullname = c.get_fullname()
+    for i in range(len(commodities)):
+      c=gnucash.GncCommodity(instance=commodities[i])
+      commodities[i]=c
 
-  if not(commodity_fullname) or (commodity_fullname == c_fullname):
-    print "["+str(i)+"] Full Name :", c.get_fullname()
-    if show_prices:
-      pl = pdb.get_prices(c,cur)
-      if pl:
-        print "{0} {1:20}{2:10} {3}".format("Time      ","Source","Price","Currency")
-        for i in pl:
-           pr = GncPrice(instance=i)
-           source = pr.get_source()
-           time = pr.get_time()
-           v=pr.get_value()
-           price = float(v.num)/v.denom
+      c_fullname = c.get_fullname()
 
-           print "{0} {1:20}{2:10.4f} {3}".format(time,source,price,cur_name)
-           # I didn't find out how to format the time option...
+      if not(commodity_fullname) or (commodity_fullname == c_fullname):
+        print "["+str(i)+"] Full Name :", c.get_fullname()
+        if show_prices:
+          pl = pdb.get_prices(c,cur)
+          if pl:
+            print "{0} {1:20}{2:>10} {3}".format("Time      ","Source","Price","Currency")
+            for i in pl:
+               pr = GncPrice(instance=i)
+               source = pr.get_source()
+               time = pr.get_time()
+               v=pr.get_value()
+               price = float(v.num)/v.denom
 
+               print "{0} {1:20}{2:10.4f} {3}".format(time,source,price,cur_name)
+               # I didn't find out how to format the time option...
+
 session.end()
 session.destroy()
 quit()



More information about the gnucash-changes mailing list