GnuCash  5.6-150-g038405b370+
price_database_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Another test file for price database stuff
3 # To update the price database call
4 # $PATH/gnucash-cli --quotes get $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=<path-to-gnucash-inst-dir>/lib/python3.7/site-packages:$PYTHONPATH
9 # You may have to adjust the above path to your local system (lib->lib64, python3.7->...)
10 # Then: ipython3
11 # The account file is not saved but always use a disposable copy.
12 # Thanks for contributions by Christoph Holtermann and Mark Jenkins
13 
14 
18 
19 from gnucash import Session
20 
21 # -------------------------------------------
22 # Configuration options can be changed here :
23 
24 cur_mnemonic="EUR" # Currency that prices are shown in. Possibilities include "EUR","GBP",...
25 namespace_name = "" # If no namespace_name is set, all namespaces will be shown
26 show_prices = True # If True, all prices for commodity are shown
27 commodity_fullname = "" # If no name is given, all commoditys in namespace will be shown
28 FILE = "PATH_TO_YOUR_TEST_FILE" # File is not saved but use a copy anyway
29 
30 # Configuration end
31 # -------------------------------------------
32 
33 session = Session(FILE, True, False, False)
34 
35 root = session.book.get_root_account()
36 book = session.book
37 pdb = book.get_price_db()
38 comm_table = book.get_table()
39 
40 cur = comm_table.lookup("CURRENCY", cur_mnemonic)
41 cur_name = cur.get_fullname()
42 
43 
44 if namespace_name != "": # Show single namespace
45  namespaces [ comm_table.find_namespace(namespace_name) ]
46 
47 else: # Show all namespaces
48  namespaces=comm_table.get_namespaces_list()
49 
50 for namespace in namespaces:
51 
52  namespace_name=namespace.get_name()
53 
54 
55  # Get a list of all commodities in namespace
56  commodities=comm_table.get_commodities(namespace_name)
57 
58 
59  if len(commodities) == 0 :
60 
61  print("No commodity in namespace "+namespace_name+".")
62  else:
63  if commodity_fullname:
64  print("Searching commodity '"+commodity_fullname+"' in namespace "+namespace_name)
65  else:
66  print("Commoditys in namespace "+namespace_name+":")
67 
68 
69  for i, c in enumerate(commodities):
70 
71  c_fullname = c.get_fullname()
72 
73  if not(commodity_fullname) or (commodity_fullname == c_fullname):
74  print("["+str(i)+"] Full Name :", c.get_fullname())
75  if show_prices:
76  pl = pdb.get_prices(c,cur)
77 
78  if len(pl) > 0 :
79  print("{0} {1:20}{2:>10} {3}".format("Time ","Source","Price","Currency"))
80  for pr in pl:
81 
82  source = pr.get_source()
83  time = pr.get_time64()
84  v=pr.get_value()
85  price = float(v.num)/v.denom
86 
87  print("{0} {1:20}{2:10.4f} {3}".format(time,source,price,cur_name))
88  # I didn't find out how to format the time option...
89 
90 session.end()
91 session.destroy()
92 quit()