just comments, fixme
[pywienerlinien] / scotty
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3
4 import argparse
5 import sys
6
7 from gotovienna.routing import *
8
9 parser = argparse.ArgumentParser(description='Get public transport route for Vienna')
10 parser.add_argument('-ot', metavar='type', type=str, help='origin type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES)
11 parser.add_argument('-dt', metavar='type', type=str, help='destination type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES)
12 parser.add_argument('origin', nargs='?')
13 parser.add_argument('destination', nargs='?')
14
15 args = parser.parse_args()
16
17 if not args.origin:
18     args.origin = raw_input('Origin: ')
19
20 if not args.destination:
21     args.destination = raw_input('Destination: ')
22
23 print >>sys.stderr, 'Searching...',
24 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
25 print >>sys.stderr, 'done.'
26
27 finished = False
28 while not finished:
29
30     html = search((args.origin, args.ot), (args.destination, args.dt)).read()
31     
32     parser = sParser(html)
33     state = parser.check_page()
34
35     if state == PageType.CORRECTION:
36         try:
37             cor = parser.get_correction()
38             origin, origin_place = split_station(args.origin)
39             destination, destination_place = split_station(args.destination)
40             
41             # FIXME refactoring
42             
43             if cor.has_key('origin'):
44                 print
45                 print '* Origin ambiguous:'
46                 l = None
47                 while not l or not l.isdigit() or int(l) > len(cor['origin']):
48                     i = 1
49                     for c in cor['origin']:
50                         print '%d. %s' % (i, c)
51                         i += 1
52                     l = sys.stdin.readline().strip()
53     
54                 origin = cor['origin'][int(l) - 1]
55     
56             if cor.has_key('destination'):
57                 print
58                 print '* Destination ambiguous:'
59                 l = None
60                 while not l or not l.isdigit() or int(l) > len(cor['destination']):
61                     i = 1
62                     for c in cor['destination']:
63                         print '%d. %s' % (i, c)
64                         i += 1
65                     l = sys.stdin.readline().strip()
66     
67                 destination = cor['destination'][int(l) - 1]
68                 
69             if cor.has_key('origin_place'):
70                 print
71                 print '* Origin place ambiguous:'
72                 l = None
73                 while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
74                     i = 1
75                     for c in cor['origin_place']:
76                         print '%d. %s' % (i, c)
77                         i += 1
78                     l = sys.stdin.readline().strip()
79     
80                 origin_place = cor['origin_place'][int(l) - 1]
81     
82             if cor.has_key('destination_place'):
83                 print
84                 print '* Destination place ambiguous:'
85                 l = None
86                 while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
87                     i = 1
88                     for c in cor['destination_place']:
89                         print '%d. %s' % (i, c)
90                         i += 1
91                     l = sys.stdin.readline().strip()
92     
93                 destination_place = cor['destination_place'][int(l) - 1]
94                 
95             args.origin = '%s, %s' % (origin, origin_place)
96             args.destination = '%s, %s' %(destination, destination_place)
97             
98         except ParserError:
99             print 'PANIC at correction page'
100             finished = True
101     
102     elif state == PageType.RESULT:
103         parser = rParser(html)
104         try:
105             overviews = parser.overview
106             details = parser.details
107             l = ''
108             while not l == 'q':
109                 for idx, overview in enumerate(overviews):
110                     if not overview['date'] or not overview['time']:
111                         # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
112                         continue
113     
114                     print '%d. [%s] %s-%s (%s)' % (idx + 1,
115                             overview['date'],
116                             overview['time'][0],
117                             overview['time'][1],
118                             overview['duration'])
119                 print 'q. Quit'
120                 l = sys.stdin.readline().strip()
121                 print
122                 print '~' * 80
123     
124                 if l.isdigit() and int(l) <= len(details):
125                     for detail in details[int(l) - 1]:
126                         if detail['time'] and detail['station']:
127                             time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
128                             print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
129                         else:
130                             print '\n'.join(detail['info'])
131                         print '-' * 80
132                 print
133         
134             finished = True
135         
136         except ParserError:
137             print 'parsererror'
138     
139     elif state == PageType.UNKNOWN:
140         print 'PANIC unknown result'