From 2e8598450deda964e999b7b92ce5c51d5d8a8c1b Mon Sep 17 00:00:00 2001 From: Vincent A Date: Wed, 30 Oct 2013 15:27:29 +0100 Subject: [PATCH] velib: add a 'city' config option to act as default filter --- modules/velib/backend.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/modules/velib/backend.py b/modules/velib/backend.py index 8cb7f1ce..c3d79aca 100644 --- a/modules/velib/backend.py +++ b/modules/velib/backend.py @@ -18,8 +18,9 @@ # along with weboob. If not, see . -from weboob.tools.backend import BaseBackend +from weboob.tools.backend import BaseBackend, BackendConfig from weboob.capabilities.gauge import ICapGauge, GaugeSensor, Gauge, GaugeMeasure, SensorNotFound +from weboob.tools.value import Value from .browser import VelibBrowser @@ -28,6 +29,12 @@ __all__ = ['VelibBackend'] SENSOR_TYPES = {u'available_bike_stands': u'Free stands', u'available_bikes': u'Available bikes', u'bike_stands': u'Total stands'} +CITIES = ("Paris", "Rouen", "Toulouse", "Luxembourg", "Valence", "Stockholm", + "Goteborg", "Santander", "Amiens", "Lillestrom", "Mulhouse", "Lyon", + "Ljubljana", "Seville", "Namur", "Nancy", "Creteil", "Bruxelles-Capitale", + "Cergy-Pontoise", "Vilnius", "Toyama", "Kazan", "Marseille", "Nantes", + "Besancon") + class BikeMeasure(GaugeMeasure): def __repr__(self): @@ -45,6 +52,9 @@ class VelibBackend(BaseBackend, ICapGauge): BROWSER = VelibBrowser STORAGE = {'boards': {}} + CONFIG = BackendConfig(Value('city', label='City', default='Paris', + choices=CITIES + ("ALL",))) + def __init__(self, *a, **kw): super(VelibBackend, self).__init__(*a, **kw) self.cities = None @@ -83,16 +93,19 @@ class VelibBackend(BaseBackend, ICapGauge): return gauge + def _contract(self): + contract = self.config.get('city').get() + if contract.lower() == 'all': + contract = None + return contract + def iter_gauges(self, pattern=None): if pattern is None: - for jgauge in self.browser.get_station_list(): + for jgauge in self.browser.get_station_list(contract=self._contract()): yield self._parse_gauge(jgauge) else: - self._fetch_cities() lowpattern = pattern.lower() - - contract = self.cities.get(lowpattern) - for jgauge in self.browser.get_station_list(contract=contract): + for jgauge in self.browser.get_station_list(contract=self._contract()): gauge = self._parse_gauge(jgauge) if lowpattern in gauge.name.lower() or lowpattern in gauge.city.lower(): yield gauge