From 74b99c4a8dfee843d8b7b21913ab868eaedb6e84 Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 2 Apr 2014 10:46:31 +0200 Subject: [PATCH] Filters on kwargs for find_object --- modules/dresdenwetter/backend.py | 2 +- modules/ing/backend.py | 10 +++++----- modules/poivy/backend.py | 2 +- modules/sachsen/backend.py | 2 +- weboob/capabilities/base.py | 13 ++++++++++--- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/modules/dresdenwetter/backend.py b/modules/dresdenwetter/backend.py index 9785c08f..4985912c 100644 --- a/modules/dresdenwetter/backend.py +++ b/modules/dresdenwetter/backend.py @@ -56,7 +56,7 @@ class DresdenWetterBackend(BaseBackend, ICapGauge): def iter_sensors(self, gauge, pattern=None): if not isinstance(gauge, Gauge): - gauge = find_object(self.iter_gauges(), gauge, error=SensorNotFound) + gauge = find_object(self.iter_gauges(), id=gauge, error=SensorNotFound) if pattern is None: for sensor in gauge.sensors: yield sensor diff --git a/modules/ing/backend.py b/modules/ing/backend.py index 92076d3c..31b16e10 100644 --- a/modules/ing/backend.py +++ b/modules/ing/backend.py @@ -68,7 +68,7 @@ class INGBackend(BaseBackend, ICapBank, ICapBill): return self.browser.get_accounts_list() def get_account(self, _id): - return find_object(self.browser.get_accounts_list(), _id, error=AccountNotFound) + return find_object(self.browser.get_accounts_list(), id=_id, error=AccountNotFound) def iter_history(self, account): if not isinstance(account, Account): @@ -100,11 +100,11 @@ class INGBackend(BaseBackend, ICapBank, ICapBill): return self.browser.get_subscriptions() def get_subscription(self, _id): - return find_object(self.browser.get_subscriptions(), _id, error=SubscriptionNotFound) + return find_object(self.browser.get_subscriptions(), id=_id, error=SubscriptionNotFound) - def get_bill(self, id): - subscription = self.get_subscription(id.split('-')[0]) - return find_object(self.browser.get_bills(subscription), id, error=BillNotFound) + def get_bill(self, _id): + subscription = self.get_subscription(_id.split('-')[0]) + return find_object(self.browser.get_bills(subscription), id=_id, error=BillNotFound) def iter_bills(self, subscription): if not isinstance(subscription, Subscription): diff --git a/modules/poivy/backend.py b/modules/poivy/backend.py index 87638ca8..4b971c6f 100644 --- a/modules/poivy/backend.py +++ b/modules/poivy/backend.py @@ -52,7 +52,7 @@ class PoivyBackend(BaseBackend, ICapBill): return self.browser.get_subscription_list() def get_subscription(self, _id): - return find_object(self.iter_subscription(), _id, error=SubscriptionNotFound) + return find_object(self.iter_subscription(), id=_id, error=SubscriptionNotFound) def iter_bills_history(self, subscription): # Try if we have a real subscription before to load the history diff --git a/modules/sachsen/backend.py b/modules/sachsen/backend.py index d2840609..7a63380d 100644 --- a/modules/sachsen/backend.py +++ b/modules/sachsen/backend.py @@ -56,7 +56,7 @@ class SachsenLevelBackend(BaseBackend, ICapGauge): def iter_sensors(self, gauge, pattern=None): if not isinstance(gauge, Gauge): - gauge = find_object(self.browser.get_rivers_list(), gauge, error=SensorNotFound) + gauge = find_object(self.browser.get_rivers_list(), id=gauge, error=SensorNotFound) if pattern is None: for sensor in gauge.sensors: yield sensor diff --git a/weboob/capabilities/base.py b/weboob/capabilities/base.py index edd145a6..6c6ae044 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -46,13 +46,20 @@ def empty(value): return True return False -def find_object(mylist, _id, error=None): +def find_object(mylist, error=None, **kwargs): """ - Very simple tools to return an object with the matching _id + Very simple tools to return an object with the matching parameters in + kwargs. """ for a in mylist: - if a.id == _id: + found = True + for key, value in kwargs.iteritems(): + if getattr(a, key) != value: + found = False + break + if found: return a + if error is not None: raise error() return None