ConsoleApplication provides methods to handle commands

This commit is contained in:
Romain Bignon 2010-04-06 21:53:54 +02:00
commit 27af8988b1
3 changed files with 72 additions and 76 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
"""
Copyright(C) 2009-2010 Romain Bignon
@ -33,44 +34,9 @@ class Boobank(ConsoleApplication):
def main(self, argv):
self.weboob.load_backends(ICapBank)
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 ' list List every available accounts'
print ' coming <ID> Display all future operations'
return self.process_command(*argv[1:])
@ConsoleApplication.command('List every available accounts')
def command_list(self):
print ' ID Account Balance Coming '
print '+-----------------+---------------------+--------------+-------------+'
@ -82,6 +48,7 @@ class Boobank(ConsoleApplication):
account.balance,
account.coming)
@ConsoleApplication.command('Display all future operations')
def command_coming(self, id):
found = 0
for name, backend in self.weboob.iter_backends():

View file

@ -30,44 +30,9 @@ class Travel(ConsoleApplication):
def main(self, argv):
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> [arrival] List all departures on a special station'
return self.process_command(*argv[1:])
@ConsoleApplication.command('Search stations')
def command_stations(self, pattern):
print ".--------------------------------.---------------------------------------------."
print '| ID | Name |'
@ -81,13 +46,14 @@ class Travel(ConsoleApplication):
print "| %3d stations listed |" % count
print "'------------------------------------------------------------------------------'"
def command_departures(self, station, arrival_station=None):
@ConsoleApplication.command('List all departures on a special station')
def command_departures(self, station, arrival=None):
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, arrival_station):
for departure in backend.iter_station_departures(station, arrival):
print u"| %3d | %-9s | %5s | %-21s | %5s | %-18s |" % (departure.id,
departure.type,
departure.time.strftime("%H:%M"),