2 # -*- coding: utf-8 -*-
4 from gotovienna import argparse
5 from gotovienna.utils import *
6 from gotovienna.realtime import *
8 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
9 parser.add_argument('line', nargs='?', help='line name (e.g. 59A)')
10 parser.add_argument('station', nargs='?', help='station name (e.g. Karlsplatz)')
12 args = parser.parse_args()
17 # Convert line name to uppercase (e.g. 'u4' -> 'U4')
18 args.line = args.line.upper()
21 args.station = args.station.decode('utf-8')
23 if args.line in itip.lines:
27 # FIXME: change get_stations() to return (headers, stations) directly
28 stations = sorted(itip.get_stations(args.line).items())
29 headers, stations = zip(*stations)
31 maxlength = max(len(stops) for stops in stations)
32 for stops in stations:
33 # Pad station list with empty items for printing, so that
34 # different-sized lists aren't truncated (with zip below)
35 stops.extend([('', '')]*(maxlength-len(stops)))
37 stations_table = zip(*stations)
38 fmt = '%%-%ds' % ITEM_WIDTH
39 spacer = ' ' * ITEM_SPACING
42 print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
44 print spacer, spacer.join('-'*ITEM_WIDTH for name in headers)
46 def match_station(query, station):
47 return query and station and (query.lower() in station.lower())
49 for row in stations_table:
50 print spacer, spacer.join(ingreen(fmt%name)
51 if match_station(args.station, name)
56 # Get matching stations
57 stations = zip(headers, stations)
58 details = [(direction, name, url) for direction, stops in stations
59 for name, url in stops if match_station(args.station, name)]
61 # User entered a station, but no matches were found
62 if args.station and not details:
63 print inred('No station matched your query.')
66 # Format a departure time (in minutes from now) for display
67 def format_departure(minutes):
71 return inblue('1') + ' min'
73 return inblue('%d' % minutes) + ' mins'
75 # Print the departure times for all matched stations
76 for direction, name, url in details:
77 print ingreen(name), '->', inblue(direction)
79 departures = itip.get_departures(url)
81 print ' Next departures:', ', '.join(format_departure(x)
84 print ' No departure information.'
89 LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
93 print inred('The given line was not found. Valid lines:')
96 for label, remaining in categorize_lines(itip.lines):
97 prefix, fill, postfix = '|== ', '=', '==- -'
98 before, after = prefix+label+' ', postfix
99 padding = LINE_WIDTH - len(before+after)
100 before = before.replace(label, inblue(label))
101 print ''.join((before, fill*padding, after))
104 this_row = [remaining.pop(0) for _ in
105 range(min(len(remaining), ITEMS_PER_LINE))]
106 print ' '.join(('%%%ds' % ITEM_WIDTH) % x for x in this_row)