velib: add a 'city' config option to act as default filter

This commit is contained in:
Vincent A 2013-10-30 15:27:29 +01:00
commit 2e8598450d

View file

@ -18,8 +18,9 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
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.capabilities.gauge import ICapGauge, GaugeSensor, Gauge, GaugeMeasure, SensorNotFound
from weboob.tools.value import Value
from .browser import VelibBrowser 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'} 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): class BikeMeasure(GaugeMeasure):
def __repr__(self): def __repr__(self):
@ -45,6 +52,9 @@ class VelibBackend(BaseBackend, ICapGauge):
BROWSER = VelibBrowser BROWSER = VelibBrowser
STORAGE = {'boards': {}} STORAGE = {'boards': {}}
CONFIG = BackendConfig(Value('city', label='City', default='Paris',
choices=CITIES + ("ALL",)))
def __init__(self, *a, **kw): def __init__(self, *a, **kw):
super(VelibBackend, self).__init__(*a, **kw) super(VelibBackend, self).__init__(*a, **kw)
self.cities = None self.cities = None
@ -83,16 +93,19 @@ class VelibBackend(BaseBackend, ICapGauge):
return gauge 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): def iter_gauges(self, pattern=None):
if pattern is 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) yield self._parse_gauge(jgauge)
else: else:
self._fetch_cities()
lowpattern = pattern.lower() lowpattern = pattern.lower()
for jgauge in self.browser.get_station_list(contract=self._contract()):
contract = self.cities.get(lowpattern)
for jgauge in self.browser.get_station_list(contract=contract):
gauge = self._parse_gauge(jgauge) gauge = self._parse_gauge(jgauge)
if lowpattern in gauge.name.lower() or lowpattern in gauge.city.lower(): if lowpattern in gauge.name.lower() or lowpattern in gauge.city.lower():
yield gauge yield gauge