From 217920fddf0e0273c83359e8069a381fb889e621 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Fri, 10 Dec 2010 17:07:03 +0100 Subject: [PATCH] Fix handling of negative amounts on account list The '-' was ignored (only ',' and digits were accepted!) As a bonus, factorization of similar code. --- weboob/backends/cragr/pages/accounts_list.py | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/weboob/backends/cragr/pages/accounts_list.py b/weboob/backends/cragr/pages/accounts_list.py index d7473f59..2700c9a6 100644 --- a/weboob/backends/cragr/pages/accounts_list.py +++ b/weboob/backends/cragr/pages/accounts_list.py @@ -21,7 +21,18 @@ from weboob.capabilities.bank import Account from .base import CragrBasePage from weboob.capabilities.bank import Operation +def clean_amount(amount): + """ + Removes weird characters and converts to a float + >>> clean_amount(u'1 000,00 $') + 1000.0 + """ + data = amount.replace(',', '.').replace(' ', '').replace(u'\xa0', '') + matches = re.findall('^(-?[0-9]+\.[0-9]{2}).*$', data) + return float(matches[0]) if (matches) else 0.0 + class AccountsList(CragrBasePage): + def get_list(self): """ Returns the list of available bank accounts @@ -43,13 +54,7 @@ class AccountsList(CragrBasePage): account.link_id = div.find('a').get('href', '') account.id = div.findall('br')[1].tail.strip() s = div.find('div').find('span').find('b').text - balance = u'' - for c in s: - if c.isdigit(): - balance += c - if c == ',': - balance += '.' - account.balance = float(balance) + account.balance = clean_amount(s) l.append(account) return l @@ -208,9 +213,7 @@ class AccountsList(CragrBasePage): for body_elmt in interesting_divs: if (self.is_right_aligned_div(body_elmt)): # this is the second line of an operation entry, displaying the amount - data = self.extract_text(body_elmt).replace(',', '.').replace(' ', '').replace(u'\xa0', '') - matches = re.findall('^(-?[0-9]+\.[0-9]{2}).*$', data) - operation.amount = float(matches[0]) if (matches) else 0.0 + operation.amount = clean_amount(self.extract_text(body_elmt)) yield operation else: # this is the first line of an operation entry, displaying the date and label @@ -229,9 +232,7 @@ class AccountsList(CragrBasePage): operation = Operation(index) index += 1 # amount - data = self.extract_text(interesting_divs[(i*3)+1]).replace(',', '.').replace(' ', '').replace(u'\xa0', '') - matches = re.findall('^(-?[0-9]+\.[0-9]{2}).*$', data) - operation.amount = float(matches[0]) if (matches) else 0.0 + operation.amount = clean_amount(self.extract_text(interesting_divs[(i*3)+1])) # date data = self.extract_text(interesting_divs[i*3]) matches = re.findall('^([012][0-9]|3[01])/(0[1-9]|1[012])', data)