Introduce find_id_list

And code simplification in backends/browsers
This commit is contained in:
Florent 2014-04-01 17:40:38 +02:00
commit 080c4aff81
5 changed files with 27 additions and 60 deletions

View file

@ -22,7 +22,7 @@ from weboob.capabilities.bank import ICapBank, AccountNotFound,\
Account, Recipient Account, Recipient
from weboob.capabilities.bill import ICapBill, Bill, Subscription,\ from weboob.capabilities.bill import ICapBill, Bill, Subscription,\
SubscriptionNotFound, BillNotFound SubscriptionNotFound, BillNotFound
from weboob.capabilities.base import UserError from weboob.capabilities.base import UserError, find_id_list
from weboob.tools.backend import BaseBackend, BackendConfig from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.value import ValueBackendPassword from weboob.tools.value import ValueBackendPassword
@ -65,25 +65,20 @@ class INGBackend(BaseBackend, ICapBank, ICapBill):
return self.iter_subscription() return self.iter_subscription()
def iter_accounts(self): def iter_accounts(self):
for account in self.browser.get_accounts_list(): return self.browser.get_accounts_list()
yield account
def get_account(self, _id): def get_account(self, _id):
account = self.browser.get_account(_id) return find_id_list(self.browser.get_accounts_list(), _id, error=AccountNotFound)
if account:
return account
else:
raise AccountNotFound()
def iter_history(self, account): def iter_history(self, account):
for history in self.browser.get_history(account.id): if not isinstance(account, Account):
yield history account = self.get_account(account)
return self.browser.get_history(account)
def iter_transfer_recipients(self, account): def iter_transfer_recipients(self, account):
if not isinstance(account, Account): if not isinstance(account, Account):
account = self.get_account(account) account = self.get_account(account)
for recipient in self.browser.get_recipients(account): return self.browser.get_recipients(account)
yield recipient
def transfer(self, account, recipient, amount, reason): def transfer(self, account, recipient, amount, reason):
if not reason: if not reason:
@ -102,21 +97,14 @@ class INGBackend(BaseBackend, ICapBank, ICapBill):
return self.browser.get_investments(account) return self.browser.get_investments(account)
def iter_subscription(self): def iter_subscription(self):
for subscription in self.browser.get_subscriptions(): return self.browser.get_subscriptions()
yield subscription
def get_subscription(self, _id): def get_subscription(self, _id):
for subscription in self.browser.get_subscriptions(): return find_id_list(self.browser.get_subscriptions(), _id, error=SubscriptionNotFound)
if subscription.id == _id:
return subscription
raise SubscriptionNotFound()
def get_bill(self, id): def get_bill(self, id):
subscription = self.get_subscription(id.split('-')[0]) subscription = self.get_subscription(id.split('-')[0])
for bill in self.browser.get_bills(subscription): return find_id_list(self.browser.get_bills(subscription), id, error=BillNotFound)
if bill.id == id:
return bill
raise BillNotFound()
def iter_bills(self, subscription): def iter_bills(self, subscription):
if not isinstance(subscription, Subscription): if not isinstance(subscription, Subscription):

View file

@ -80,20 +80,8 @@ class IngBrowser(LoginBrowser):
self.where = "start" self.where = "start"
return self.page.get_list() return self.page.get_list()
def get_account(self, id):
assert isinstance(id, basestring)
l = self.get_accounts_list()
for a in l:
if a.id == id:
return a
return None
@need_login @need_login
def get_history(self, account): def get_history(self, account):
if not isinstance(account, Account):
account = self.get_account(account)
if account.type == Account.TYPE_MARKET: if account.type == Account.TYPE_MARKET:
for result in self.get_history_titre(account): for result in self.get_history_titre(account):
yield result yield result
@ -152,7 +140,7 @@ class IngBrowser(LoginBrowser):
else: else:
# It is hard to check the box and to get the real list. # It is hard to check the box and to get the real list.
# We try an alternative way like normal users # We try an alternative way like normal users
self.get_history(account.id).next() self.get_history(account).next()
self.transferpage.stay_or_go() self.transferpage.stay_or_go()
return self.page.get_recipients() return self.page.get_recipients()

View file

@ -19,6 +19,7 @@
from weboob.capabilities.bill import ICapBill, Subscription, SubscriptionNotFound, Detail from weboob.capabilities.bill import ICapBill, Subscription, SubscriptionNotFound, Detail
from weboob.capabilities.base import find_id_list
from weboob.tools.backend import BaseBackend, BackendConfig from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.value import ValueBackendPassword from weboob.tools.value import ValueBackendPassword
@ -48,17 +49,15 @@ class PoivyBackend(BaseBackend, ICapBill):
self.config['password'].get()) self.config['password'].get())
def iter_subscription(self): def iter_subscription(self):
for subscription in self.browser.get_subscription_list(): return self.browser.get_subscription_list()
yield subscription
def get_subscription(self, _id): def get_subscription(self, _id):
subscription = self.browser.get_subscription(_id) return find_id_list(self.iter_subscription(), _id, error=SubscriptionNotFound)
if subscription:
return subscription
else:
raise SubscriptionNotFound()
def iter_bills_history(self, subscription): def iter_bills_history(self, subscription):
# Try if we have a real subscription before to load the history
if not isinstance(subscription, Subscription):
subscription = self.get_subscription(subscription)
return self.browser.get_history() return self.browser.get_history()
# No details on the website # No details on the website

View file

@ -48,25 +48,7 @@ class PoivyBrowser(LoginBrowser):
def get_subscription_list(self): def get_subscription_list(self):
return self.homepage.stay_or_go().get_list() return self.homepage.stay_or_go().get_list()
def _find_id_list(self, mylist, _id):
for a in mylist:
if a.id == _id:
return a
return None
@need_login
def get_subscription(self, _id):
return self._find_id_list(self.get_subscription_list(), _id)
@need_login @need_login
def get_history(self): def get_history(self):
self.history.stay_or_go() self.history.stay_or_go()
return self.pagination(lambda: self.page.get_calls()) return self.pagination(lambda: self.page.get_calls())
@need_login
def iter_bills(self, parentid):
return self.bills.stay_or_go().get_bills()
@need_login
def get_bill(self, _id):
return self._find_id_list(self.iter_bills(), _id)

View file

@ -46,6 +46,16 @@ def empty(value):
return True return True
return False return False
def find_id_list(mylist, _id, error=None):
"""
Very simple tools to return an object with the matching _id
"""
for a in mylist:
if a.id == _id:
return a
if error is not None:
raise error()
return None
class UserError(Exception): class UserError(Exception):
""" """