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

View file

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

View file

@ -19,6 +19,7 @@
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.value import ValueBackendPassword
@ -48,17 +49,15 @@ class PoivyBackend(BaseBackend, ICapBill):
self.config['password'].get())
def iter_subscription(self):
for subscription in self.browser.get_subscription_list():
yield subscription
return self.browser.get_subscription_list()
def get_subscription(self, _id):
subscription = self.browser.get_subscription(_id)
if subscription:
return subscription
else:
raise SubscriptionNotFound()
return find_id_list(self.iter_subscription(), _id, error=SubscriptionNotFound)
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()
# No details on the website

View file

@ -48,25 +48,7 @@ class PoivyBrowser(LoginBrowser):
def get_subscription_list(self):
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
def get_history(self):
self.history.stay_or_go()
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 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):
"""