GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
price_database_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Another test file for price database stuff
3 # To update the price database call
4 # $PATH/gnucash --add-price-quotes $PATHTOFILE
5 # before running this.
6 # Adding to a calling bash script would be better
7 # Although calling it from here would be even better!
8 # OR: export PYTHONPATH=$HOME/progs/lib/python2.6/site-packages
9 # Then: gnucash-env ipython
10 # The account file is not saved but always use a disposable copy.
11 # Thanks for contributions by Christoph Holtermann and Mark Jenkins
12 
13 ## @file
14 # @brief Test file for price database stuff
15 # @author Mike Evans, Christoph Holtermann, Mark Jenkins
16 # @ingroup python_bindings_examples
17 
18 from gnucash import Session
19 
20 # -------------------------------------------
21 # Configuration options can be changed here :
22 
23 cur_mnemonic="EUR" # Currency that prices are shown in. Possibilities include "EUR","GBP",...
24 namespace_name = "" # If no namespace_name is set, all namespaces will be shown
25 show_prices = True # If True, all prices for commodity are shown
26 commodity_fullname = "" # If no name is given, all commoditys in namespace will be shown
27 FILE = "PATH_TO_YOUR_TEST_FILE" # File is not saved but use a copy anyway
28 
29 # Configuration end
30 # -------------------------------------------
31 
32 session = Session(FILE, True, False, False)
33 
34 root = session.book.get_root_account()
35 book = session.book
36 pdb = book.get_price_db()
37 comm_table = book.get_table()
38 
39 cur = comm_table.lookup("CURRENCY", cur_mnemonic)
40 cur_name = cur.get_fullname()
41 
42 
43 if namespace_name != "": # Show single namespace
44  namespaces [ comm_table.find_namespace(namespace_name) ]
45 
46 else: # Show all namespaces
47  namespaces=comm_table.get_namespaces_list()
48 
49 for namespace in namespaces:
50 
51  namespace_name=namespace.get_name()
52 
53 
54  # Get a list of all commodities in namespace
55  commodities=comm_table.get_commodities(namespace_name)
56 
57 
58  if len(commodities) == 0 :
59 
60  print "No commodity in namespace "+namespace_name+"."
61  else:
62  if commodity_fullname:
63  print "Searching commodity '"+commodity_fullname+"' in namespace "+namespace_name
64  else:
65  print "Commoditys in namespace "+namespace_name+":"
66 
67 
68  for i, c in enumerate(commodities):
69 
70  c_fullname = c.get_fullname()
71 
72  if not(commodity_fullname) or (commodity_fullname == c_fullname):
73  print "["+str(i)+"] Full Name :", c.get_fullname()
74  if show_prices:
75  pl = pdb.get_prices(c,cur)
76 
77  if len(pl) > 0 :
78  print "{0} {1:20}{2:>10} {3}".format("Time ","Source","Price","Currency")
79  for pr in pl:
80 
81  source = pr.get_source()
82  time = pr.get_time()
83  v=pr.get_value()
84  price = float(v.num)/v.denom
85 
86  print "{0} {1:20}{2:10.4f} {3}".format(time,source,price,cur_name)
87  # I didn't find out how to format the time option...
88 
89 session.end()
90 session.destroy()
91 quit()