Skip to content
application.py 4.73 KiB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
#!/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 <command> [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 <pattern>       Search stations'
        print '      departures <station>     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 "'------------------------------------------------------------------------------'"