From 1aa49402b3d81a2ab6ad978f37abf13998ff33d6 Mon Sep 17 00:00:00 2001 From: christopherlam Date: Sat, 4 Feb 2017 23:15:53 +0800 Subject: Update gnucashxml.py to improve compatibility with python3 (#1) * Update gnucashxml.py * Add support for 'double' slot type * Improve __repr__ to be more descriptive Improve Account, Slot, Split repr to become more descriptive Add fullname() support for account names (eg Expenses:Business:Capital) --- gnucashxml.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/gnucashxml.py b/gnucashxml.py index 9014d29..0d51300 100644 --- a/gnucashxml.py +++ b/gnucashxml.py @@ -88,8 +88,18 @@ class Account(object): self.splits = [] self.slots = slots or {} + def fullname(self): + if self.parent: + pfn = self.parent.fullname() + if pfn: + return '{}:{}'.format(pfn, self.name) + else: + return self.name + else: + return '' + def __repr__(self): - return "".format(self.guid) + return "".format(self.name, self.guid[:10]) def walk(self): """ @@ -137,7 +147,7 @@ class Transaction(object): self.slots = slots or {} def __repr__(self): - return u"".format(self.guid) + return "".format(self.date, self.description, self.guid[:6]) def __lt__(self, other): # For sorted() only @@ -167,7 +177,11 @@ class Split(object): self.slots = slots def __repr__(self): - return "".format(self.guid) + return "".format(self.transaction.date, + self.transaction.description, + self.transaction.currency, + self.value, + self.guid[:6]) def __lt__(self, other): # For sorted() only @@ -237,7 +251,7 @@ def _book_from_tree(tree): root_account = acc accountdict[acc.guid] = acc parentdict[acc.guid] = parent_guid - for acc in accountdict.values(): + for acc in list(accountdict.values()): if acc.parent is None and acc.actype != 'ROOT': parent = accountdict[parentdict[acc.guid]] acc.parent = parent @@ -411,8 +425,8 @@ def _slots_from_tree(tree): key = elt.find(slot + "key").text value = elt.find(slot + "value") type_ = value.get('type', 'string') - if type_ == 'integer': - slots[key] = long(value.text) + if type_ in ('integer', 'double'): + slots[key] = int(value.text) elif type_ == 'numeric': slots[key] = _parse_number(value.text) elif type_ in ('string', 'guid'): -- cgit v1.2.3