Package sabx10 :: Package oxm :: Module parse
[hide private]
[frames] | no frames]

Source Code for Module sabx10.oxm.parse

  1  ############################################################################### 
  2  # 
  3  # sabx10.oxm - an SABX file manipulation library 
  4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  5  # 
  6  # This file is part of sabx10.oxm. 
  7  # 
  8  # sabx10.oxm is free software: you can redistribute it and/or modify it under 
  9  # the terms of the GNU Lesser General Public License as published by the Free 
 10  # Software Foundation, either version 3 of the License, or (at your option) any 
 11  # later version. 
 12  # 
 13  # sabx10.oxm 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 Lesser General Public License for more 
 16  # details. 
 17  # 
 18  # You should have received a copy of the GNU Lesser General Public License 
 19  # along with sabx10.oxm.  If not, see <http://www.gnu.org/licenses/>. 
 20  # 
 21  ############################################################################### 
 22  """ 
 23  Take an SABX XML tree and turn it into objects. 
 24   
 25  L{parse_tree} is the main entry point for parsing XML.  When you pass it an XML 
 26  tree of SABX data, it returns a dictionary of information corresponding to the 
 27  structure of an SABX file.  It's returned as a C{dict} because I generally use 
 28  this to read an SABX file, modify it, then run it through a Jinja2 template for 
 29  output.  Jinja2 takes a dictionary to pass to the template as it's template 
 30  data. 
 31   
 32  Here's a somewhat graphical representation of the data that you get back from 
 33  L{parse_tree}. 
 34   
 35   - SABX - C{dict} 
 36    - uuid 
 37    - version 
 38    - zip_prefix 
 39    - desc 
 40    - ride_type 
 41    - title 
 42    - description 
 43    - terrain 
 44    - photos - L{PhotoSet} 
 45      - C{list} of L{Photo} 
 46    - park_list and park_dict - C{list} and C{dict} 
 47      - contains L{Parking} 
 48    - turn_list and turn_dict - C{list} and C{dict} 
 49      - contains L{Turn} 
 50    - seg_list and seg_dict - C{list} and C{dict} 
 51      - contains L{Segment} 
 52        - C{list} of L{Waypoint} 
 53          - stop ids 
 54          - poi ids 
 55    - stop_list and stop_dict - C{list} and C{dict} 
 56      - contains L{Stop} 
 57    - poi_list and poi_dict - C{list} and C{dict} 
 58      - contains L{Poi} 
 59    - ride_list and ride_dict - C{list} and C{dict} 
 60      - contains L{Ride} 
 61        - id 
 62        - description 
 63        - parking id 
 64        - turns - C{list} 
 65          - contains turn ids 
 66        - segments - C{list} 
 67          - contains segment ids 
 68    - history - C{list} 
 69      - contains L{HistoryUpdate} 
 70  """ 
 71   
 72  import xml.etree.ElementTree as et 
 73  from utils import check_version 
 74  from parking import parse_parking 
 75  from turn import parse_turns 
 76  from segment import parse_segments 
 77  from stop import parse_stops 
 78  from poi import parse_pois 
 79  from ride import parse_rides 
 80   
 81  ############################################################ 
 82  # PHOTOS 
 83  ############################################################ 
 84   
85 -class Photo(object):
86 """ 87 A C{Photo} object holds the information for an "a" HTML element that 88 links to photos of the ride. 89 90 @ivar desc: description of what this links to 91 @type desc: C{string} 92 @ivar href: URL of the link 93 @type href: C{string} 94 """
95 - def __init__(self, desc, href):
96 """ 97 Save the passed-in data. 98 99 @param desc: description of what this links to 100 @type desc: C{string} 101 @param href: URL of the link 102 @type href: C{string} 103 """ 104 self.desc = desc 105 self.href = href
106
107 -class PhotoSet(object):
108 """ 109 A C{PhotoSet} object holds information about a cover photo for the 110 rideset and links to additional photos of the ride. 111 112 @ivar title: title (hover) text for cover photo 113 @type title: C{string} 114 @ivar src: URL of cover photo 115 @type src: C{string} 116 @ivar photo_list: C{list} of C{Photo} objects 117 @type photo_list: C{list} of C{Photo} 118 """
119 - def __init__(self, title, src, photo_list):
120 """ 121 Save the passed-in data. 122 123 @param title: title (hover) text for cover photo 124 @type title: C{string} 125 @param src: URL of cover photo 126 @type src: C{string} 127 @param photo_list: C{list} of C{Photo} objects 128 @type photo_list: C{list} of C{Photo} 129 """ 130 self.title = title 131 self.src = src 132 self.photo_list = photo_list
133
134 -def _parse_photos_photo(xml_photos):
135 """ 136 Take the C{Element} for a photos element and turn it into a list of 137 L{Photo} objects. 138 139 @param xml_photos: C{Element} for the C{PhotoSet} element 140 @type xml_photos: C{Element} 141 142 @return: C{list} of L{Photo} objects 143 @rtype: C{list} of L{Photo} 144 """ 145 return [Photo(xml_photo.findtext('desc'), xml_photo.findtext('href')) 146 for xml_photo in xml_photos.findall('photo')]
147
148 -def parse_photos(xml_tree):
149 """ 150 Parse the photos element in the given C{Element} tree. 151 152 @param xml_tree: root of C{Element} tree that has photos 153 @type xml_tree: C{Element} or C{ElementTree} 154 155 @return: a C{PhotoSet} object or None 156 @rtype: C{PhotoSet} 157 """ 158 check_version(xml_tree) 159 xml_photos = xml_tree.find('photos') 160 if xml_photos: 161 return PhotoSet(xml_photos.findtext('title'), 162 xml_photos.findtext('src'), 163 _parse_photos_photo(xml_photos)) 164 else: 165 return None
166 167 ############################################################ 168 # HISTORY 169 ############################################################ 170
171 -class HistoryUpdate(object):
172 """ 173 A C{HistoryUpdate} object holds the information for a single update to 174 the SABX file. 175 176 @ivar version: version number of the change 177 @type version: C{string} 178 @ivar date: date the change was made 179 @type date: C{string} 180 @ivar description: description of what the change entailed 181 @type description: C{string} 182 """
183 - def __init__(self, version, date, description):
184 """ 185 Save the passed-in data. 186 187 @param version: version number of the change 188 @type version: C{string} 189 @param date: date the change was made 190 @type date: C{string} 191 @param description: description of what the change entailed 192 @type description: C{string} 193 """ 194 self.version = version 195 self.date = date 196 self.description = description
197
198 -def _parse_update(xml_update):
199 """ 200 Take the C{Element} for a history update and turn it into a 201 L{HistoryUpdate} object. 202 203 @param xml_update: C{Element} for a history update 204 @type xml_update: C{Element} 205 206 @return: L{HistoryUpdate} object 207 @rtype: L{HistoryUpdate} 208 """ 209 return HistoryUpdate(xml_update.findtext('version'), 210 xml_update.findtext('date'), 211 xml_update.findtext('description'))
212
213 -def parse_history(xml_tree):
214 """ 215 Parse the history element in the given C{Element} tree. 216 217 @param xml_tree: root of C{Element} tree that has history in it 218 @type xml_tree: C{Element} or C{ElementTree} 219 220 @return: C{List} of L{HistoryUpdate}s 221 @rtype: C{list} of L{HistoryUpdate} 222 """ 223 check_version(xml_tree) 224 return [_parse_update(xml_update) 225 for xml_update in xml_tree.findall('history/update')]
226 227 ############################################################ 228 # TOP 229 ############################################################ 230
231 -def parse_top_level(xml_tree):
232 """ 233 Parse all of the simple root elements in an SABX file. 234 235 The simple root elements are: 236 - uuid 237 - version 238 - zip_prefix 239 - description 240 - ride_type 241 - title 242 - description 243 - terrain 244 245 @param xml_tree: root of C{Element} tree that has SABX data 246 @type xml_tree: C{Element} or C{ElementTree} 247 248 @return: C{dict} of root elements 249 @rtype: C{dict} 250 """ 251 check_version(xml_tree) 252 sabx = {} 253 254 sabx['uuid'] = xml_tree.findtext('uuid') 255 sabx['version'] = xml_tree.findtext('version') 256 sabx['zip_prefix'] = xml_tree.findtext('zip_prefix') 257 258 desc = et.tostring(xml_tree.find('description')).\ 259 replace("<description>", "").\ 260 replace("</description>", "").\ 261 replace("<description />", "") 262 desc = desc.strip() 263 264 sabx['ride_type'] = xml_tree.findtext('ride_type') 265 sabx['title'] = xml_tree.findtext('title') 266 sabx['description'] = desc 267 sabx['terrain'] = xml_tree.findtext('terrain') 268 269 return sabx
270 271 ############################################################ 272 # OVERALL 273 ############################################################ 274
275 -def parse_tree(xml_tree):
276 """ 277 Parse a whole SABX file. 278 279 @param xml_tree: root of C{Element} tree that has SABX data 280 @type xml_tree: C{Element} or C{ElementTree} 281 282 @return: C{dict} of parsed SABX data 283 @rtype: C{dict} 284 """ 285 sabx = parse_top_level(xml_tree) 286 sabx['photos'] = parse_photos(xml_tree) 287 sabx['park_list'], sabx['park_dict'] = parse_parking(xml_tree) 288 sabx['turn_list'], sabx['turn_dict'] = parse_turns(xml_tree) 289 sabx['seg_list'], sabx['seg_dict'] = parse_segments(xml_tree) 290 sabx['stop_list'], sabx['stop_dict'] = parse_stops(xml_tree) 291 sabx['poi_list'], sabx['poi_dict'] = parse_pois(xml_tree) 292 sabx['ride_list'], sabx['ride_dict'] = parse_rides(xml_tree) 293 sabx['history'] = parse_history(xml_tree) 294 295 return sabx
296