2 # -*- coding: utf-8 -*-
6 from gotovienna.realtime import *
9 return '\033[91m' + x + '\033[0m'
12 return '\033[92m' + x + '\033[0m'
15 return '\033[94m' + x + '\033[0m'
17 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
18 parser.add_argument('line', nargs='?')
19 parser.add_argument('station', nargs='?')
21 args = parser.parse_args()
26 # Convert line name to uppercase (e.g. 'u4' -> 'U4')
27 args.line = args.line.upper()
30 args.station = args.station.decode('utf-8')
32 if args.line in itip.lines:
36 # FIXME: change get_stations() to return (headers, stations) directly
37 stations = sorted(itip.get_stations(args.line).items())
38 headers, stations = zip(*stations)
40 maxlength = max(len(stops) for stops in stations)
41 for stops in stations:
42 # Pad station list with empty items for printing, so that
43 # different-sized lists aren't truncated (with zip below)
44 stops.extend([('', '')]*(maxlength-len(stops)))
46 stations_table = zip(*stations)
47 fmt = '%%-%ds' % ITEM_WIDTH
48 spacer = ' ' * ITEM_SPACING
51 print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
53 print spacer, spacer.join('-'*ITEM_WIDTH for name in headers)
55 def match_station(query, station):
56 return query and station and (query.lower() in station.lower())
58 for row in stations_table:
59 print spacer, spacer.join(ingreen(fmt%name)
60 if match_station(args.station, name)
65 # Get matching stations
66 stations = zip(headers, stations)
67 details = [(direction, name, url) for direction, stops in stations
68 for name, url in stops if match_station(args.station, name)]
70 # User entered a station, but no matches were found
71 if args.station and not details:
72 print inred('No station matched your query.')
75 # Format a departure time (in minutes from now) for display
76 def format_departure(minutes):
80 return inblue('1') + ' min'
82 return inblue('%d' % minutes) + ' mins'
84 # Print the departure times for all matched stations
85 for direction, name, url in details:
86 print ingreen(name), '->', inblue(direction)
88 departures = itip.get_departures(url)
90 print ' Next departures:', ', '.join(format_departure(x)
93 print ' No departure information.'
98 LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
102 print inred('The given line was not found. Valid lines:')
105 for label, remaining in categorize_lines(itip.lines):
106 prefix, fill, postfix = '|== ', '=', '==- -'
107 before, after = prefix+label+' ', postfix
108 padding = LINE_WIDTH - len(before+after)
109 before = before.replace(label, inblue(label))
110 print ''.join((before, fill*padding, after))
113 this_row = [remaining.pop(0) for _ in
114 range(min(len(remaining), ITEMS_PER_LINE))]
115 print ' '.join(('%%%ds' % ITEM_WIDTH) % x for x in this_row)