2 # -*- coding: utf-8 -*-
6 from gotovienna.utils import *
7 from gotovienna.realtime import *
9 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
10 parser.add_argument('line', nargs='?', help='line name (e.g. 59A)')
11 parser.add_argument('station', nargs='?', help='station name (e.g. Karlsplatz)')
13 args = parser.parse_args()
18 # Convert line name to uppercase (e.g. 'u4' -> 'U4')
19 args.line = args.line.upper()
22 args.station = args.station.decode('utf-8')
24 if args.line in itip.lines:
28 # FIXME: change get_stations() to return (headers, stations) directly
29 stations = sorted(itip.get_stations(args.line).items())
30 headers, stations = zip(*stations)
32 maxlength = max(len(stops) for stops in stations)
33 for stops in stations:
34 # Pad station list with empty items for printing, so that
35 # different-sized lists aren't truncated (with zip below)
36 stops.extend([('', '')]*(maxlength-len(stops)))
38 stations_table = zip(*stations)
39 fmt = '%%-%ds' % ITEM_WIDTH
40 spacer = ' ' * ITEM_SPACING
43 print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
45 print spacer, spacer.join('-'*ITEM_WIDTH for name in headers)
47 def match_station(query, station):
48 return query and station and (query.lower() in station.lower())
50 for row in stations_table:
51 print spacer, spacer.join(ingreen(fmt%name)
52 if match_station(args.station, name)
57 # Get matching stations
58 stations = zip(headers, stations)
59 details = [(direction, name, url) for direction, stops in stations
60 for name, url in stops if match_station(args.station, name)]
62 # User entered a station, but no matches were found
63 if args.station and not details:
64 print inred('No station matched your query.')
67 # Format a departure time (in minutes from now) for display
68 def format_departure(minutes):
72 return inblue('1') + ' min'
74 return inblue('%d' % minutes) + ' mins'
76 # Print the departure times for all matched stations
77 for direction, name, url in details:
78 print ingreen(name), '->', inblue(direction)
80 departures = itip.get_departures(url)
82 print ' Next departures:', ', '.join(format_departure(x)
85 print ' No departure information.'
91 stations = itip.get_stations(l)
92 for key in stations.keys():
95 for station in stations[key]:
97 if s.startswith(station[0]) or station[0].startswith(s):
99 # skip station if destination
102 print '* %s\n %s .....' % (key, station[0]), itip.get_departures(station[1])
104 print ' %s' % station[0]
108 LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
112 print inred('The given line was not found. Valid lines:')
115 for label, remaining in categorize_lines(itip.lines):
116 prefix, fill, postfix = '|== ', '=', '==- -'
117 before, after = prefix+label+' ', postfix
118 padding = LINE_WIDTH - len(before+after)
119 before = before.replace(label, inblue(label))
120 print ''.join((before, fill*padding, after))
123 this_row = [remaining.pop(0) for _ in
124 range(min(len(remaining), ITEMS_PER_LINE))]
125 print ' '.join(('%%%ds' % ITEM_WIDTH) % x for x in this_row)