From 7d698192f3693e2057217618df4de42fd4a83bdb Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Tue, 11 May 2010 16:59:12 +0200 Subject: [PATCH] adapt frontends to new Results API --- weboob/frontends/boobank/boobank.py | 40 ++++--------------- weboob/frontends/travel/application.py | 36 ++++------------- weboob/frontends/weboorrents/application.py | 43 ++++++--------------- weboob/frontends/wetboobs/application.py | 41 +++----------------- 4 files changed, 34 insertions(+), 126 deletions(-) diff --git a/weboob/frontends/boobank/boobank.py b/weboob/frontends/boobank/boobank.py index e190eba9..932bc293 100644 --- a/weboob/frontends/boobank/boobank.py +++ b/weboob/frontends/boobank/boobank.py @@ -3,7 +3,7 @@ # vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai """ -Copyright(C) 2009-2010 Romain Bignon +Copyright(C) 2009-2010 Romain Bignon, Christophe Benz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from __future__ import with_statement +import logging import sys import weboob @@ -43,27 +44,18 @@ class Boobank(ConsoleApplication): @ConsoleApplication.command('List every available accounts') def command_list(self): - results = {'HEADER': ('ID', 'label', 'balance', 'coming')} try: for backend, account in self.weboob.do('iter_accounts'): - row = [account.id, account.label, account.balance, account.coming] - try: - results[backend.name].append(row) - except KeyError: - results[backend.name] = [row] + print self.format(account) except weboob.CallErrors, e: for backend, error in e.errors: if isinstance(error, weboob.tools.browser.BrowserIncorrectPassword): - print >>sys.stderr, 'Error: Incorrect password for backend %s' % backend.name + logging.error(u'Error: Incorrect password for backend %s' % backend.name) else: - print >>sys.stderr, 'Error[%s]: %s' % (backend.name, error) - - return results + logging.error(u'Error[%s]: %s' % (backend.name, error)) @ConsoleApplication.command('Display all future operations') def command_coming(self, id): - operations = [] - found = 0 total = 0.0 def do(backend): @@ -72,27 +64,11 @@ class Boobank(ConsoleApplication): try: for backend, operation in self.weboob.do(do): - found = 1 - operations.append(' %8s %-50s %11.2f' % (operation.date, operation.label, operation.amount)) + print self.format(operation) total += operation.amount except weboob.CallErrors, e: for backend, error in e.errors: if isinstance(error, AccountNotFound): - if not found: - found = -1 + logging.error(u'Error: account %s not found' % id) else: - print >>sys.stderr, 'Error[%s]: %s' % (backend.name, error) - - if found < 0: - print >>sys.stderr, "Error: account %s not found" % id - return 1 - else: - if operations: - print ' Date Label Amount ' - print '+----------+----------------------------------------------------+-------------+' - print '\n'.join(operations) - print '+----------+----------------------------------------------------+-------------+' - print ' %8s %-50s %11.2f' % ('', 'Total:', total) - print '+----------+----------------------------------------------------+-------------+' - else: - print 'No coming operations for ID=%s' % id + logging.error(u'Error[%s]: %s' % (backend.name, error)) diff --git a/weboob/frontends/travel/application.py b/weboob/frontends/travel/application.py index 76e08246..2891c888 100644 --- a/weboob/frontends/travel/application.py +++ b/weboob/frontends/travel/application.py @@ -21,6 +21,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from weboob.capabilities.travel import ICapTravel from weboob.tools.application import ConsoleApplication + +__all__ = ['Travel'] + + class Travel(ConsoleApplication): APPNAME = 'travel' VERSION = '1.0' @@ -33,34 +37,10 @@ class Travel(ConsoleApplication): @ConsoleApplication.command('Search stations') def command_stations(self, pattern): - print ".--------------------------------.---------------------------------------------." - print '| ID | Name |' - print '+--------------------------------+---------------------------------------------+' - count = 0 - for backend in self.weboob.iter_backends(): - for station in backend.iter_station_search(pattern): - print '| %-31s| %-44s|' % (station.id, station.name) - count += 1 - print "+--------------------------------'---------------------------------------------+" - print "| %3d stations listed |" % count - print "'------------------------------------------------------------------------------'" + for backend, station in self.weboob.do('iter_station_search', pattern): + print self.format(station) @ConsoleApplication.command('List all departures on a special station') def command_departures(self, station, arrival=None): - print ".-----.-----------.-------.-----------------------.-------.--------------------.------------" - print "| ID | Type | Time | Arrival | Late | Info | Plateform |" - print "+-----+-----------+-------+-----------------------+-------+--------------------+-----------+" - count = 0 - for backend in self.weboob.iter_backends(): - for departure in backend.iter_station_departures(station, arrival): - print u"|%4d | %-10s|%6s | %-22s|%6s | %-19s| %-10s|" % (departure.id, - departure.type, - departure.time.strftime("%H:%M"), - departure.arrival_station, - departure.late and departure.late.strftime("%H:%M") or '', - departure.information, - departure.plateform) - count += 1 - print "+-----'-----------'-------'-----------------------'-------'--------------------'-----------+" - print "| %3d departures listed |" % count - print "'------------------------------------------------------------------------------------------'" + for backend, departure in self.weboob.do('iter_station_departures', station, arrival): + print self.format(departure) diff --git a/weboob/frontends/weboorrents/application.py b/weboob/frontends/weboorrents/application.py index e4182b59..f83cb108 100644 --- a/weboob/frontends/weboorrents/application.py +++ b/weboob/frontends/weboorrents/application.py @@ -20,11 +20,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from __future__ import with_statement +import logging import sys from weboob.capabilities.torrent import ICapTorrent from weboob.tools.application import ConsoleApplication + +__all__ = ['Weboorrents'] + + class Weboorrents(ConsoleApplication): APPNAME = 'weboorrents' VERSION = '1.0' @@ -37,13 +42,13 @@ class Weboorrents(ConsoleApplication): def split_id(self, id): if not '.' in id: - print >>sys.stderr, 'ID must be in form .' + logging.error('ID must be in form .') return None, None backend_name, id = id.split('.', 1) backend = self.weboob.backends.get(backend_name, None) if not backend: - print >>sys.stderr, 'Backends "%s" not found' % backend_name + logging.error('Backends "%s" not found' % backend_name) return None, None return backend, id @@ -53,25 +58,12 @@ class Weboorrents(ConsoleApplication): backend, id = self.split_id(id) if not backend: return 1 - with backend: torrent = backend.get_torrent(id) - if not torrent: - print >>sys.stderr, 'Torrent "%s" not found' % id + logging.error('Torrent "%s" not found' % id) return 1 - - rows = [] - rows.append(('ID', torrent.id)) - rows.append(('Name', torrent.name)) - rows.append(('Size', torrent.size)) - rows.append(('URL', torrent.url)) - rows.append(('Seeders', torrent.seeders)) - rows.append(('Leechers', torrent.leechers)) - rows.append(('Description', torrent.description)) - if torrent.files: - rows.append(('Files', '\n'.join(torrent.files))) - return {backend.name: rows} + print self.format(torrent) @ConsoleApplication.command('Get the torrent file') def command_getfile(self, id, dest): @@ -82,7 +74,7 @@ class Weboorrents(ConsoleApplication): with backend: s = backend.get_torrent_file(id) if not s: - print >>sys.stderr, 'Torrent "%s" not found' % id + logging.error('Torrent "%s" not found' % id) return 1 if dest == '-': @@ -93,17 +85,6 @@ class Weboorrents(ConsoleApplication): @ConsoleApplication.command('Search torrents') def command_search(self, pattern=None): - results = {} - if pattern: - results['BEFORE'] = u'Search pattern: %s' % pattern - else: - results['BEFORE'] = u'Last videos' - results['HEADER'] = ('ID', 'Name', 'Size') - + print u'Search pattern: %s' % pattern if pattern else u'Last torrents' for backend, torrent in self.weboob.do('iter_torrents', pattern=pattern): - row = ('%s.%s' % (backend.name,torrent.id), torrent.name, torrent.size) - try: - results[backend.name].append(row) - except KeyError: - results[backend.name] = [row] - return results + print self.format(torrent) diff --git a/weboob/frontends/wetboobs/application.py b/weboob/frontends/wetboobs/application.py index f9a55ca2..a4f86013 100644 --- a/weboob/frontends/wetboobs/application.py +++ b/weboob/frontends/wetboobs/application.py @@ -34,58 +34,29 @@ class WetBoobs(ConsoleApplication): @ConsoleApplication.command('search cities') def command_search(self, pattern): - print ".--------------------------------.---------------------------------------------." - print '| ID | Name |' - print '+--------------------------------+---------------------------------------------+' - count = 0 for backend, city in self.weboob.do('iter_city_search', pattern): - print u'| %-31s| %-44s|' % (city.city_id, city.name) - count += 1 - print "+--------------------------------'---------------------------------------------+" - print "| %3d cities listed |" % count - print "'------------------------------------------------------------------------------'" + print self.format(city) @ConsoleApplication.command('get current weather') def command_current(self, city): - print ".-------------.----------------------------------------------------------------." - print '| Temperature | Text |' - print '+-------------+----------------------------------------------------------------+' - found = 0 try: for backend, current in self.weboob.do('get_current', city): - print u'| %-12s| %-63s|' % (u'%d °%s' % (current.temp, current.unit), current.text) - found = 1 + print self.format(current) except CallErrors, e: for error in e: if isinstance(error, CityNotFound): - if not found: - found = -1 + logging.error('City "%s" not found' % city) else: raise error - if found < 0: - print "| -- | City not found |" - print "+-------------'----------------------------------------------------------------+" @ConsoleApplication.command('get forecasts') def command_forecasts(self, city): - print ".-------------.------.------.--------------------------------------------------." - print '| Date | Min | Max | Text |' - print '+-------------+-------+-------+------------------------------------------------+' - found = 0 try: - for backend, f in self.weboob.do('iter_forecast', city): - found = 1 - print u'| %-12s|%6s |%6s | %-47s|' % (f.date, - u'%d °%s' % (f.low, f.unit), - u'%d °%s' % (f.high, f.unit), - f.text) + for backend, forecast in self.weboob.do('iter_forecast', city): + print self.format(forecast) except CallErrors, e: for error in e: if isinstance(error, CityNotFound): - if not found: - found = -1 + logging.error('City "%s" not found' % city) else: raise error - if found < 0: - print "| -- --- ---- | -- | -- | City not found |" - print "+-------------'-------'-------'------------------------------------------------+"