just small format changes
[pywienerlinien] / itip
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from gotovienna import argparse
5 from gotovienna.utils import *
6 from gotovienna.realtime import *
7
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)')
11
12 args = parser.parse_args()
13
14 itip = ITipParser()
15
16 if args.line:
17     # Convert line name to uppercase (e.g. 'u4' -> 'U4')
18     args.line = args.line.upper()
19
20 if args.station:
21     args.station = args.station.decode('utf-8')
22
23 if args.line in itip.lines:
24     ITEM_WIDTH = 33
25     ITEM_SPACING = 4
26
27     # FIXME: change get_stations() to return (headers, stations) directly
28     stations = sorted(itip.get_stations(args.line).items())
29     headers, stations = zip(*stations)
30
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)))
36
37     stations_table = zip(*stations)
38     fmt = '%%-%ds' % ITEM_WIDTH
39     spacer = ' ' * ITEM_SPACING
40
41     print
42     print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
43             for name in headers)
44     print spacer, spacer.join('-'*ITEM_WIDTH for name in headers)
45
46     def match_station(query, station):
47         return query and station and (query.lower() in station.lower())
48
49     for row in stations_table:
50         print spacer, spacer.join(ingreen(fmt%name)
51                 if match_station(args.station, name)
52                 else fmt%name
53                 for name, url in row)
54     print
55
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)]
60
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.')
64         print
65
66     # Format a departure time (in minutes from now) for display
67     def format_departure(minutes):
68         if minutes == 0:
69             return inred('now')
70         elif minutes == 1:
71             return inblue('1') + ' min'
72         else:
73             return inblue('%d' % minutes) + ' mins'
74
75     # Print the departure times for all matched stations
76     for direction, name, url in details:
77         print ingreen(name), '->', inblue(direction)
78
79         departures = itip.get_departures(url)
80         if departures:
81             print '  Next departures:', ', '.join(format_departure(x)
82                     for x in departures)
83         else:
84             print '  No departure information.'
85         print
86 else:
87     ITEMS_PER_LINE = 12
88     ITEM_WIDTH = 5
89     LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
90
91     if args.line:
92         print
93         print inred('The given line was not found. Valid lines:')
94
95     print
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))
102
103         while remaining:
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)
107
108         print