From 3f6d62f34ac0c28ad019b5698a92ea0f67071f56 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Tue, 23 Mar 2010 13:59:14 +0100 Subject: [PATCH] add the 'travel' frontend --- weboob/frontends/travel/__init__.py | 23 ++++++ weboob/frontends/travel/application.py | 109 +++++++++++++++++++++++++ weboob/frontends/travel/scripts/travel | 28 +++++++ 3 files changed, 160 insertions(+) create mode 100644 weboob/frontends/travel/__init__.py create mode 100644 weboob/frontends/travel/application.py create mode 100755 weboob/frontends/travel/scripts/travel diff --git a/weboob/frontends/travel/__init__.py b/weboob/frontends/travel/__init__.py new file mode 100644 index 0000000000..ff98e5ab76 --- /dev/null +++ b/weboob/frontends/travel/__init__.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai + +""" +Copyright(C) 2010 Romain Bignon + +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 +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +from .application import Application diff --git a/weboob/frontends/travel/application.py b/weboob/frontends/travel/application.py new file mode 100644 index 0000000000..7161d671cf --- /dev/null +++ b/weboob/frontends/travel/application.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai + +""" +Copyright(C) 2010 Romain Bignon + +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 +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +import sys +from types import MethodType + +from weboob import Weboob +from weboob.capabilities.travel import ICapTravel +from weboob.tools.application import BaseApplication + +class Application(BaseApplication): + APPNAME = 'travel' + CONFIG = {} + + def main(self, argv): + #if not self.config: + # print >>sys.stderr, "Error: %s is not configured yet. Please call 'weboob.travel -c'" % argv[0] + # print >>sys.stderr, "Also, you need to use 'weboobcfg' to set backend configs" + # return -1 + + self.weboob.load_modules(ICapTravel) + + if len(argv) == 1: + print >>sys.stderr, "Usage: %s [args ...]" % argv[0] + return -1 + + return self.command(argv[1], *argv[2:]) + + def getMethods(self, prefix): + services = {} + for attrname in dir(self): + if not attrname.startswith(prefix): + continue + attr = getattr(self, attrname) + if not isinstance(attr, MethodType): + continue + name = attrname[len(prefix):] + services[name] = attr + return services + + def command(self, command, *args): + commands = self.getMethods('command_') + if not command in commands: + print >>sys.stderr, "No such command: %s" % command + self.command_help() + return 1 + try: + return commands[command](*args) + except TypeError, e: + try: + print >>sys.stderr, "Command %s takes %s arguments" % (command, int(str(e).split(' ')[3]) - 1) + except: + print >>sys.stderr, '%s' % e + return 1 + + def command_help(self): + print 'Available commands are:' + print ' stations Search stations' + print ' departures List all departures on a special station' + + def command_stations(self, pattern): + print ".-----------------.----------------------------------------." + print '| ID | Name |' + print '+-----------------+----------------------------------------+' + count = 0 + for name, backend, in self.weboob.iter_backends(): + for station in backend.iter_station_search(pattern): + print '| %-15s | %-38s |' % (station.id, station.name) + count += 1 + print "+-----------------'----------------------------------------+" + print "| %3d stations listed |" % count + print "'----------------------------------------------------------'" + + def command_departures(self, station): + print ".-----.-----------.-------.-----------------------.-------.--------------------." + print "| ID | Type | Time | Arrival | Late | Info |" + print "+-----+-----------+-------+-----------------------+-------+--------------------+" + count = 0 + for name, backend, in self.weboob.iter_backends(): + for departure in backend.iter_station_departures(station): + print u"| %3d | %-9s | %5s | %-21s | %5s | %-18s |" % (departure.id, + departure.type, + departure.time.strftime("%H:%M"), + departure.arrival_station, + departure.late and departure.late.strftime("%H:%M") or '', + departure.information.replace('\n', '').strip()) + count += 1 + print "+-----'-----------'-------'-----------------------'-------'--------------------+" + print "| %3d departures listed |" % count + print "'------------------------------------------------------------------------------'" diff --git a/weboob/frontends/travel/scripts/travel b/weboob/frontends/travel/scripts/travel new file mode 100755 index 0000000000..d9ed3c0d12 --- /dev/null +++ b/weboob/frontends/travel/scripts/travel @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai + +""" +Copyright(C) 2010 Romain Bignon + +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 +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +import sys +from weboob.frontends.travel import Application + +if __name__ == '__main__': + app = Application() + sys.exit(app.main(sys.argv)) -- GitLab