Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 <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 "'------------------------------------------------------------------------------'"