diff options
| -rw-r--r-- | gnucashxml.py | 63 | 
1 files changed, 60 insertions, 3 deletions
| diff --git a/gnucashxml.py b/gnucashxml.py index 83be8c2..76ba930 100644 --- a/gnucashxml.py +++ b/gnucashxml.py @@ -31,11 +31,12 @@ class Book(object):      A book is the main container for GNU Cash data.      It doesn't really do anything at all by itself, except to have -    a reference to the accounts, transactions, and commodities. +    a reference to the accounts, transactions, prices, and commodities.      """ -    def __init__(self, guid, transactions=None, root_account=None, +    def __init__(self, guid, prices=None, transactions=None, root_account=None,                   accounts=None, commodities=None, slots=None):          self.guid = guid +        self.prices = prices          self.transactions = transactions or []          self.root_account = root_account          self.accounts = accounts or [] @@ -217,6 +218,26 @@ class Split(object):              False +class Price(object): +    """ +    A price is GNUCASH record of the price of a commodity against a currency +    Consists of date, currency, commodity,  value +    """ +    def __init__(self, guid=None, commodity=None, currency=None, +                 date=None, value=None): +        self.guid = guid +        self.commodity = commodity +        self.currency = currency +        self.date = date +        self.value = value + +    def __repr__(self): +        return "<Price {}... {:%d/%m/%Y}: 1 {} = {} {} >".format(self.guid[:6], +            self.date, +            self.commodity, +            self.value, +            self.currency) +  ##################################################################  # XML file parsing @@ -268,6 +289,12 @@ def _book_from_tree(tree):          commodities.append(comm)          commoditydict[(comm.space, comm.name)] = comm +    prices = [] +    t = tree.find('{http://www.gnucash.org/XML/gnc}pricedb') +    for child in t.findall('price'): +        price = _price_from_tree(child, commoditydict) +        prices.append(price) +          root_account = None      accounts = []      accountdict = {} @@ -296,6 +323,7 @@ def _book_from_tree(tree):      slots = _slots_from_tree(          tree.find('{http://www.gnucash.org/XML/book}slots'))      return Book(guid=guid, +                prices=prices,                  transactions=transactions,                  root_account=root_account,                  accounts=accounts, @@ -319,7 +347,36 @@ def _commodity_from_tree(tree):      space = tree.find('{http://www.gnucash.org/XML/cmdty}space').text      return Commodity(name=name, space=space) - +# Implemented: +# - price +# - price:guid +# - price:commodity +# - price:currency +# - price:date +# - price:value +def _price_from_tree(tree, commoditydict): +    price = '{http://www.gnucash.org/XML/price}' +    cmdty = '{http://www.gnucash.org/XML/cmdty}' +    ts = "{http://www.gnucash.org/XML/ts}" +     +    guid = tree.find(price + 'id').text +    value = _parse_number(tree.find(price + 'value').text) +    date = parse_date(tree.find(price + 'time/' + ts + 'date').text) +     +    currency_space = tree.find(price + "currency/" + cmdty + "space").text +    currency_name = tree.find(price + "currency/" + cmdty + "id").text +    currency = commoditydict[(currency_space, currency_name)] +     +    commodity_space = tree.find(price + "commodity/" + cmdty + "space").text +    commodity_name = tree.find(price + "commodity/" + cmdty + "id").text +    commodity = commoditydict[(commodity_space, commodity_name)] +     +    return Price(guid = guid,  +                 commodity = commodity,  +                 date = date,  +                 value = value,  +                 currency = currency) +                   # Implemented:  # - act:name  # - act:id | 
