itip, scotty: Add help texts for arguments
[pywienerlinien] / itip
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import argparse
5
6 from gotovienna.realtime import *
7
8 def inred(x):
9     return '\033[91m' + x + '\033[0m'
10
11 def ingreen(x):
12     return '\033[92m' + x + '\033[0m'
13
14 def inblue(x):
15     return '\033[94m' + x + '\033[0m'
16
17 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
18 parser.add_argument('line', nargs='?', help='line name (e.g. 59A)')
19 parser.add_argument('station', nargs='?', help='station name (e.g. Karlsplatz)')
20
21 args = parser.parse_args()
22
23 itip = ITipParser()
24
25 if args.line:
26     # Convert line name to uppercase (e.g. 'u4' -> 'U4')
27     args.line = args.line.upper()
28
29 if args.station:
30     args.station = args.station.decode('utf-8')
31
32 if args.line in itip.lines:
33     ITEM_WIDTH = 33
34     ITEM_SPACING = 4
35
36     # FIXME: change get_stations() to return (headers, stations) directly
37     stations = sorted(itip.get_stations(args.line).items())
38     headers, stations = zip(*stations)
39
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)))
45
46     stations_table = zip(*stations)
47     fmt = '%%-%ds' % ITEM_WIDTH
48     spacer = ' ' * ITEM_SPACING
49
50     print
51     print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
52             for name in headers)
53     print spacer, spacer.join('-'*ITEM_WIDTH for name in headers)
54
55     def match_station(query, station):
56         return query and station and (query.lower() in station.lower())
57
58     for row in stations_table:
59         print spacer, spacer.join(ingreen(fmt%name)
60                 if match_station(args.station, name)
61                 else fmt%name
62                 for name, url in row)
63     print
64
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)]
69
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.')
73         print
74
75     # Format a departure time (in minutes from now) for display
76     def format_departure(minutes):
77         if minutes == 0:
78             return inred('now')
79         elif minutes == 1:
80             return inblue('1') + ' min'
81         else:
82             return inblue('%d' % minutes) + ' mins'
83
84     # Print the departure times for all matched stations
85     for direction, name, url in details:
86         print ingreen(name), '->', inblue(direction)
87
88         departures = itip.get_departures(url)
89         if departures:
90             print '  Next departures:', ', '.join(format_departure(x)
91                     for x in departures)
92         else:
93             print '  No departure information.'
94         print
95 else:
96     ITEMS_PER_LINE = 12
97     ITEM_WIDTH = 5
98     LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
99
100     if args.line:
101         print
102         print inred('The given line was not found. Valid lines:')
103
104     print
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))
111
112         while remaining:
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)
116
117         print