cleaner way to iterate

This commit is contained in:
smurail 2014-10-23 12:50:15 +02:00
commit 56e9d53adb

View file

@ -31,64 +31,64 @@ class AccountsList(Page):
pass pass
def get_list(self): def get_list(self):
for div in self.document.getiterator('div'): blocks = self.document.xpath('//div[@id="synthese-list"]//div[@class="block"]')
if div.attrib.get('id', '') == 'synthese-list': for div in blocks:
for tr in div.getiterator('tr'): for tr in div.getiterator('tr'):
account = Account() account = Account()
account.id = None account.id = None
account._link_id = None account._link_id = None
for td in tr.getiterator('td'): for td in tr.getiterator('td'):
if td.attrib.get('class', '') == 'account-cb': if td.attrib.get('class', '') == 'account-cb':
try: try:
a = td.xpath('./*/a[@class="gras"]')[0] a = td.xpath('./*/a[@class="gras"]')[0]
except IndexError: except IndexError:
# ignore account # ignore account
break break
account.type = Account.TYPE_CARD account.type = Account.TYPE_CARD
account.label = self.parser.tocleanstring(a) account.label = self.parser.tocleanstring(a)
try: try:
account._link_id = td.xpath('.//a')[0].attrib['href'] account._link_id = td.xpath('.//a')[0].attrib['href']
except KeyError: except KeyError:
pass pass
elif td.attrib.get('class', '') == 'account-name': elif td.attrib.get('class', '') == 'account-name':
try: try:
span = td.xpath('./span[@class="label"]')[0] span = td.xpath('./span[@class="label"]')[0]
except IndexError: except IndexError:
# ignore account # ignore account
break break
account.label = self.parser.tocleanstring(span) account.label = self.parser.tocleanstring(span)
try: try:
account._link_id = td.xpath('.//a')[0].attrib['href'] account._link_id = td.xpath('.//a')[0].attrib['href']
except KeyError: except KeyError:
pass pass
elif td.attrib.get('class', '') == 'account-more-actions': elif td.attrib.get('class', '') == 'account-more-actions':
for a in td.getiterator('a'): for a in td.getiterator('a'):
# For normal account, two "account-more-actions" # For normal account, two "account-more-actions"
# One for the account, one for the credit card. Take the good one # One for the account, one for the credit card. Take the good one
if "mouvements.phtml" in a.attrib['href'] and "/cartes/" not in a.attrib['href']: if "mouvements.phtml" in a.attrib['href'] and "/cartes/" not in a.attrib['href']:
account._link_id = a.attrib['href'] account._link_id = a.attrib['href']
elif td.attrib.get('class', '') == 'account-number': elif td.attrib.get('class', '') == 'account-number':
id = td.text id = td.text
id = id.strip(u' \n\t') id = id.strip(u' \n\t')
account.id = id account.id = id
elif td.attrib.get('class', '') == 'account-total': elif td.attrib.get('class', '') == 'account-total':
span = td.find('span') span = td.find('span')
if span is None: if span is None:
balance = td.text balance = td.text
else: else:
balance = span.text balance = span.text
account.currency = account.get_currency(balance) account.currency = account.get_currency(balance)
balance = FrenchTransaction.clean_amount(balance) balance = FrenchTransaction.clean_amount(balance)
if balance != "": if balance != "":
account.balance = Decimal(balance) account.balance = Decimal(balance)
else: else:
account.balance = Decimal(0) account.balance = Decimal(0)
else: else:
# because of some weird useless <tr> # because of some weird useless <tr>
if account.id is not None: if account.id is not None:
yield account yield account