Full location support with partial match smartitty
[ottawa-travel-planner.git] / ShortFormatter.py
blobcc3c72614617ce9e1fc5b06b1cd643e63d3be667
2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 """Formats an Itinerary for terse display."""
6 import sre
8 import Itinerary
10 class ShortFormatter:
11 def __init__(self, itinentries):
12 self.entries = itinentries
14 # A list of text strings to display.
15 self.lines = []
17 self.format()
19 def format(self):
20 for ie in self.entries:
21 if ie.type == Itinerary.TYPE_WALK_TO_STOP:
22 self.formatWalkToStop(ie)
23 elif ie.type == Itinerary.TYPE_TAKE_BUS:
24 self.formatTakeBus(ie)
25 elif ie.type == Itinerary.TYPE_WALK_TO_TRANSFER:
26 self.formatWalkToTransfer(ie)
27 elif ie.type == Itinerary.TYPE_WALK_TO_DEST:
28 self.formatWalkToDest(ie)
30 def formatWalkToStop(self, ie):
31 line = "-%s walk %s (%s)" \
32 % (self.canonTime(ie.startTime),
33 self.canonDest(ie.destination), ie.busStop)
34 self.lines.append(line)
36 def formatTakeBus(self, ie):
37 line = "-%s %s %s to %s at %s" \
38 % (self.canonTime(ie.startTime), ie.route,
39 self.canonDirection(ie.direction),
40 self.canonDest(ie.destination),
41 self.canonTime(ie.endTime))
42 self.lines.append(line)
44 def formatWalkToTransfer(self, ie):
45 line = "-walk %s (%s)" % (self.canonDest(ie.destination), ie.busStop)
46 self.lines.append(line)
48 def formatWalkToDest(self, ie):
49 line = "-walk %s %s" \
50 % (self.canonDest(ie.destination), self.canonTime(ie.endTime))
51 self.lines.append(line)
53 def canonDest(self, d):
54 """MERIVALE / COLONNADE -> Merivale/Colonnade"""
55 match = _canon_dest_rx.match(d)
56 if match:
57 d = "/".join((match.group(1), match.group(2)))
58 return d.title()
60 def canonTime(self, t):
61 """11:05 AM -> 11:05"""
62 return t.split()[0]
64 def canonDirection(self, d):
65 """St Laurent -> St Lau. Take the first 3 letters of all words
66 after the first."""
67 words = d.split()
68 canonwords = [words[0]]
69 for w in words[1:]:
70 canonwords.append(w[0:3]) # ok even if only 2 letters in w
71 return " ".join(canonwords)
73 def __str__(self):
74 return "\n".join(self.lines)
76 def __repr__(self):
77 print self.__str__()
79 _canon_dest_re = "^(.*)\s/\s+(.*)$"
80 _canon_dest_rx = sre.compile(_canon_dest_re)