Package sabx10 :: Package utils :: Module sabx_number
[hide private]
[frames] | no frames]

Source Code for Module sabx10.utils.sabx_number

  1  ############################################################################### 
  2  # 
  3  # sabx10 - an SABX file manipulation library 
  4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  5  # 
  6  # This file is part of sabx10. 
  7  # 
  8  # sabx10 is free software: you can redistribute it and/or modify it under the 
  9  # terms of the GNU General Public License as published by the Free Software 
 10  # Foundation, either version 3 of the License, or (at your option) any later 
 11  # version. 
 12  # 
 13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along with 
 18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
 19  # 
 20  ############################################################################### 
 21  """ 
 22  Handle renumbering the ids in an SABX file. 
 23  """ 
 24   
 25  from sabx10.templating import SabxProcessor 
 26   
27 -class Id(object):
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
45 - def reset(self):
46 """ 47 Start over counting. 48 """ 49 if not self.unique: 50 self.val = self.start_val
51
52 - def next(self):
53 """ 54 Get the next id. 55 """ 56 ret_val = self.val 57 self.val += 1 58 return str(ret_val)
59
60 - def range(self, length):
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
69 -class NumProcessor(SabxProcessor):
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
109 - def _create_item_translation(self, item_list):
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
120 - def _create_translations(self):
121 """ 122 Read all the ids for parking places, turns, segments, stops, and pois 123 and generate new ids for them, saving the mappings. 124 """ 125 self.park_translate = self._create_item_translation( 126 self.template_data['park_list']) 127 self.turn_translate = self._create_item_translation( 128 self.template_data['turn_list']) 129 self.seg_translate = self._create_item_translation( 130 self.template_data['seg_list']) 131 self.stop_translate = self._create_item_translation( 132 self.template_data['stop_list']) 133 self.poi_translate = self._create_item_translation( 134 self.template_data['poi_list']) 135 self.ride_translate = self._create_item_translation( 136 self.template_data['ride_list'])
137
138 - def _renumber_items(self, item_list, translater):
139 """ 140 Renumber all the items in the list. 141 """ 142 for item in item_list: 143 item.id = translater[item.id]
144
145 - def _renumber_seg_pts_stops_pois(self, seg):
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
160 - def _renumber_ride_park_turns_segs(self, ride):
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
168 - def _renumber_ids(self):
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
193 - def get_template_data(self):
194 """ 195 Renumber all the ids for all the SABX 1.0 data. 196 """ 197 SabxProcessor.get_template_data(self) 198 self.id = Id(self.options.start, self.options.unique) 199 self._create_translations() 200 self._renumber_ids()
201