# -*- coding: utf-8 -*-

# Copyright(C) 2009-2010  Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import re

from weboob.capabilities.bank import Account
from weboob.tools.browser import BasePage

class AccountsList(BasePage):
    LINKID_REGEXP = re.compile(".*ch4=(\w+).*")

    def on_loaded(self):
        pass

    def get_list(self):
        l = []
        for tr in self.document.getiterator('tr'):
            if tr.attrib.get('class', '') == 'comptes':
                account = Account()
                for td in tr.getiterator('td'):
                    if td.attrib.get('headers', '').startswith('Numero_'):
                        id = td.text
                        account.id = ''.join(id.split(' ')).strip()
                    elif td.attrib.get('headers', '').startswith('Libelle_'):
                        a = td.findall('a')
                        label = unicode(a[0].text)
                        account.label = label
                        m = self.LINKID_REGEXP.match(a[0].attrib.get('href', ''))
                        if m:
                            account.link_id = m.group(1)
                    elif td.attrib.get('headers', '').startswith('Solde'):
                        a = td.findall('a')
                        balance = a[0].text
                        balance = balance.replace('.','').replace(',','.')
                        account.balance = float(balance)
                    elif td.attrib.get('headers', '').startswith('Avenir'):
                        a = td.findall('a')
                        coming = a[0].text
                        coming = coming.replace('.','').replace(',','.')
                        account.coming = float(coming)

                l.append(account)
        return l

@@ -2097,24 +2097,220 @@
 ('Avenir'):%0A
+                        # Some accounts don't have a %22coming%22%0A                        if td.text.strip() == '-':%0A                            account.coming = 0.0%0A                        else:%0A    
             
@@ -2333,32 +2333,36 @@
 td.findall('a')%0A
+    
                 
@@ -2380,32 +2380,36 @@
 ing = a%5B0%5D.text%0A
+    
                 
@@ -2457,32 +2457,36 @@
 eplace(',','.')%0A
+    
                 

Support for accounts without "coming" in bnporc