1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 Handle renumbering the ids in an SABX file.
23 """
24
25 from sabx10.templating import SabxProcessor
26
28 """
29 Keep track of the ids for the NumProcessor class.
30
31 @ivar unique: unique ids?
32 @type ivar: C{boolean}
33 @ivar start_val: starting id value
34 @type start_val: C{int}
35 @ivar val: current id value
36 @type val: C{int}
37 """
38 - def __init__(self, start_val=0, unique=False):
39 """
40 Setup the instance variables.
41 """
42 self.unique = unique
43 self.start_val = self.val = start_val
44
46 """
47 Start over counting.
48 """
49 if not self.unique:
50 self.val = self.start_val
51
53 """
54 Get the next id.
55 """
56 ret_val = self.val
57 self.val += 1
58 return str(ret_val)
59
61 """
62 Get a list of ids.
63 """
64 self.reset()
65 start = self.val
66 self.val += length
67 return [str(index) for index in range(start, self.val)]
68
70 """
71 Sequentially renumber the ids of all the items in the SABX 1.0 file. This
72 includes parking places, stops, pois, segments, turns, and rides.
73
74 @ivar index: next index to use
75 @type index: C{int}
76 @ivar park_translate: dictionary of old to new parking id mappings
77 @type park_translate: C{dict}
78 @ivar turn_translate: dictionary of old to new turn id mappings
79 @type turn_translate: C{dict}
80 @ivar seg_translate: dictionary of old to new segment id mappings
81 @type seg_translate: C{dict}
82 @ivar stop_translate: dictionary of old to new stop id mappings
83 @type stop_translate: C{dict}
84 @ivar poi_translate: dictionary of old to new poi id mappings
85 @type poi_translate: C{dict}
86 @ivar ride_translate: dictionary of old to new ride id mappings
87 @type ride_translate: C{dict}
88 """
89
90 - def __init__(self, template_file=None, man=None):
91 """
92 Adds the "start" and "unique" options to the command-line options.
93
94 @param template_file: (optional) file name of template file
95 @type template_file: C{string}
96 @param man: (optional) extended program help
97 @type man: C{string}
98 """
99 SabxProcessor.__init__(self, template_file, man)
100
101 self.parser.add_option("-s", "--start", dest="start",
102 help="number to start at",
103 type="int", default=1, metavar="START")
104 self.parser.add_option("-u", "--unique", dest="unique",
105 action="store_true",
106 help="all unique ids?",
107 default=False, metavar="UNIQUE")
108
110 """
111 Create the translation dictionary for an item list.
112 """
113 keys = [item.id for item in item_list]
114 vals = self.id.range(len(item_list))
115 trans = {}
116 for key, val in zip(keys, vals):
117 trans[key] = val
118 return trans
119
137
139 """
140 Renumber all the items in the list.
141 """
142 for item in item_list:
143 item.id = translater[item.id]
144
146 """
147 Renumber the stops and pois for a segment.
148 """
149 for pt in seg.waypoints:
150 pt.id = self.id.next()
151
152 if pt.stop:
153 pt.stop = (' ').join([self.stop_translate[stop_item]
154 for stop_item in pt.stop.split()])
155
156 if pt.poi:
157 pt.poi = (' ').join([self.poi_translate[poi_item]
158 for poi_item in pt.poi.split()])
159
161 """
162 Renumber the parking, turns, and segments for a ride.
163 """
164 ride.parking = self.park_translate[ride.parking]
165 ride.turns = [self.turn_translate[turn] for turn in ride.turns]
166 ride.segs = [self.seg_translate[seg] for seg in ride.segs]
167
169 """
170 Go through all the parking places, turns, segments, stops, pois, and
171 rides and renumber them all.
172 """
173 self._renumber_items(self.template_data['park_list'],
174 self.park_translate)
175 self._renumber_items(self.template_data['turn_list'],
176 self.turn_translate)
177 self._renumber_items(self.template_data['seg_list'],
178 self.seg_translate)
179 self._renumber_items(self.template_data['stop_list'],
180 self.stop_translate)
181 self._renumber_items(self.template_data['poi_list'],
182 self.poi_translate)
183 self._renumber_items(self.template_data['ride_list'],
184 self.ride_translate)
185
186 self.id.reset()
187 for seg in self.template_data['seg_list']:
188 self._renumber_seg_pts_stops_pois(seg)
189
190 for ride in self.template_data['ride_list']:
191 self._renumber_ride_park_turns_segs(ride)
192
201