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

Source Code for Module sabx10.utils.sabx_cleanup

  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 cleaning-up SABX files. 
 23  """ 
 24   
 25  from sabx10.templating import SabxProcessor 
 26   
27 -class CleanupProcessor(SabxProcessor):
28 """ 29 Process an SABX 1.0 file and remove all unused parking places, turns, 30 segments, stops, and pois. 31 32 @ivar used_parking_ids: list of parking place ids used in rides 33 @type used_parking_ids: C{list} of C{string} 34 @ivar used_turn_ids: list of turn ids used in rides 35 @type used_turn_ids: C{list} of C{string} 36 @ivar used_seg_ids: list of segment ids used in rides 37 @type used_seg_ids: C{list} of C{string} 38 @ivar used_stop_ids: list of stop ids used in rides 39 @type used_stop_ids: C{list} of C{string} 40 @ivar used_poi_ids: list of poi ids used in rides 41 @type used_poi_ids: C{list} of C{string} 42 """ 43
44 - def _get_used_item_ids(self):
45 """ 46 Go through all the rides in the rideset and make lists of all parking 47 places, turns, segments, stops, and pois that are used. 48 """ 49 self.used_parking_ids = [] 50 self.used_turn_ids = [] 51 self.used_seg_ids = [] 52 self.used_stop_ids = [] 53 self.used_poi_ids = [] 54 55 for ride in self.template_data['ride_list']: 56 self.used_parking_ids.append(ride.parking) 57 self.used_turn_ids.extend(ride.turns) 58 self.used_seg_ids.extend(ride.segs) 59 60 for seg_index in self.used_seg_ids: 61 for pt in self.template_data['seg_dict'][seg_index].waypoints: 62 if pt.stop is not None: 63 stop_list = pt.stop.split(',') 64 self.used_stop_ids.extend(stop_list) 65 66 if pt.poi is not None: 67 poi_list = pt.poi.split(',') 68 self.used_poi_ids.extend(poi_list)
69
70 - def _get_used_items(self, old_list, used_ids):
71 """ 72 Go through items in the old_list and make a new list and dict 73 containing the items whose ids are in used_ids. 74 75 @param old_list: list of items to check 76 @type old_list: C{list} of objects 77 @param used_ids: list of ids to check against 78 @type used_ids: C{list} of C{string} 79 80 @return: C{list} and C{dict} of used items 81 @rtype: (C{list}, C{dict}) of used objects 82 """ 83 used_list = [] 84 used_dict = {} 85 for item in old_list: 86 if item.id in used_ids: 87 used_list.append(item) 88 used_dict[item.id] = item 89 return used_list, used_dict
90
91 - def _cull_unused_items(self):
92 """ 93 Go through the lists of parking places, turns, segments, stops, and 94 pois and only keeps items that are used in rides. 95 """ 96 self.template_data['park_list'], self.template_data['park_dict'] = \ 97 self._get_used_items( 98 self.template_data['park_list'], self.used_parking_ids) 99 self.template_data['turn_list'], self.template_data['turn_dict'] = \ 100 self._get_used_items( 101 self.template_data['turn_list'], self.used_turn_ids) 102 self.template_data['seg_list'], self.template_data['seg_dict'] = \ 103 self._get_used_items( 104 self.template_data['seg_list'], self.used_seg_ids) 105 self.template_data['stop_list'], self.template_data['stop_dict'] = \ 106 self._get_used_items( 107 self.template_data['stop_list'], self.used_stop_ids) 108 self.template_data['poi_list'], self.template_data['poi_dict'] = \ 109 self._get_used_items( 110 self.template_data['poi_list'], self.used_poi_ids)
111
112 - def get_template_data(self):
113 """ 114 Culls unused items from the SABX 1.0 data. 115 """ 116 SabxProcessor.get_template_data(self) 117 self._get_used_item_ids() 118 self._cull_unused_items()
119