GnuCash 2.4.99
price_database_example.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Another test file for price database stuff
00003 # To update the price database call
00004 # $PATH/gnucash  --add-price-quotes $PATHTOFILE
00005 # before running this.
00006 # Adding to a calling bash script would be better
00007 # Although calling it from here would be even better!
00008 #  OR:  export PYTHONPATH=$HOME/progs/lib/python2.6/site-packages
00009 # Then: gnucash-env ipython
00010 # The account file is not saved but always use a disposable copy.
00011 # Thanks for contributions by Christoph Holtermann and Mark Jenkins
00012 
00013 ##  @file
00014 #   @brief Test file for price database stuff
00015 #   @author Mike Evans, Christoph Holtermann, Mark Jenkins
00016 #   @ingroup python_bindings_examples
00017 
00018 from gnucash import Session
00019 
00020 # -------------------------------------------
00021 # Configuration options can be changed here :
00022 
00023 cur_mnemonic="EUR"                      # Currency that prices are shown in. Possibilities include "EUR","GBP",...
00024 namespace_name = ""                     # If no namespace_name is set, all namespaces will be shown
00025 show_prices = True                      # If True, all prices for commodity are shown
00026 commodity_fullname = ""                 # If no name is given, all commoditys in namespace will be shown
00027 FILE = "PATH_TO_YOUR_TEST_FILE"         # File is not saved but use a copy anyway
00028 
00029 # Configuration end
00030 # -------------------------------------------
00031 
00032 session = Session(FILE, True, False, False)
00033 
00034 root = session.book.get_root_account()
00035 book = session.book
00036 pdb = book.get_price_db()
00037 comm_table = book.get_table()
00038 
00039 cur = comm_table.lookup("CURRENCY", cur_mnemonic)
00040 cur_name = cur.get_fullname()
00041 
00042 
00043 if namespace_name != "":                    # Show single namespace
00044   namespaces [ comm_table.find_namespace(namespace_name) ]
00045 
00046 else:                                 # Show all namespaces
00047   namespaces=comm_table.get_namespaces_list()
00048 
00049 for namespace in namespaces:
00050 
00051   namespace_name=namespace.get_name()
00052 
00053 
00054   # Get a list of all commodities in namespace
00055   commodities=comm_table.get_commodities(namespace_name)
00056 
00057 
00058   if len(commodities) == 0 :
00059 
00060     print "No commodity in namespace "+namespace_name+"."
00061   else:
00062     if commodity_fullname:
00063       print "Searching commodity '"+commodity_fullname+"' in namespace "+namespace_name
00064     else:
00065       print "Commoditys in namespace "+namespace_name+":"
00066 
00067 
00068     for i, c in enumerate(commodities):
00069 
00070       c_fullname = c.get_fullname()
00071 
00072       if not(commodity_fullname) or (commodity_fullname == c_fullname):
00073         print "["+str(i)+"] Full Name :", c.get_fullname()
00074         if show_prices:
00075           pl = pdb.get_prices(c,cur)
00076 
00077           if len(pl) > 0 :
00078             print "{0} {1:20}{2:>10} {3}".format("Time      ","Source","Price","Currency")
00079             for pr in pl:
00080 
00081                source = pr.get_source()
00082                time = pr.get_time()
00083                v=pr.get_value()
00084                price = float(v.num)/v.denom
00085 
00086                print "{0} {1:20}{2:10.4f} {3}".format(time,source,price,cur_name)
00087                # I didn't find out how to format the time option...
00088 
00089 session.end()
00090 session.destroy()
00091 quit()
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines