1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
83
84
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 """
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
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
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
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
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
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
229
230
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
273
274
296