-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
66 lines (52 loc) · 2.15 KB
/
query.py
File metadata and controls
66 lines (52 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2009 Don Kelly <karfai@gmail.com>
# This file is part of voyageur.
# voyageur is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# voyageur is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with voyageur. If not, see <http://www.gnu.org/licenses/>.
from schema import *
import re, sys
st = create_store()
def search_intersection(parts):
a = unicode('%' + parts[0].upper() + '%')
b = unicode('%' + parts[1].upper() + '%')
return Or(Like(Stop.name, '%s/%s' % (a, b)),
Like(Stop.name, '%s/%s' % (b, a)))
def search_number(parts):
return Stop.number == int(parts[0])
def search_label(parts):
return Stop.label == unicode(parts[0].upper())
def search_all(parts):
return Stop.name.like(unicode('%' + parts[0].upper() + '%'))
patterns = [
('(\w+) and (\w+)', search_intersection),
('(\w+)\s*/\s*(\w+)', search_intersection),
('([0-9]{4})', search_number),
('([a-zA-Z]{2}[0-9]{3})', search_label),
('(\w+)', search_all),
]
srch = None
for p in patterns:
mt = re.match(p[0], sys.argv[1])
if mt:
srch = p[1](mt.groups())
if srch:
break
n = secs_elapsed_today()
for r in st.find(Stop, srch):
print '%s (%i): %s' % (r.label, r.number, r.name)
for pu in r.upcoming_pickups(int(sys.argv[2])):
m = (pu.arrival - n) / 60
print "%s %s in %um (%s) (%i)" % (pu.trip.route.name, pu.trip.headsign, m, pu.arrival_s(), pu.sequence)
print '-------'
for tpu in pu.trip.next_pickups_from_pickup(pu, 5):
print '%s (%i)' % (tpu.stop.name, tpu.sequence)
print '-------'
for tpu in pu.trip.next_pickups_from_now(5):
print '%s (%i)' % (tpu.stop.name, tpu.sequence)