1 | n/a | """Lightweight XML support for Python. |
---|
2 | n/a | |
---|
3 | n/a | XML is an inherently hierarchical data format, and the most natural way to |
---|
4 | n/a | represent it is with a tree. This module has two classes for this purpose: |
---|
5 | n/a | |
---|
6 | n/a | 1. ElementTree represents the whole XML document as a tree and |
---|
7 | n/a | |
---|
8 | n/a | 2. Element represents a single node in this tree. |
---|
9 | n/a | |
---|
10 | n/a | Interactions with the whole document (reading and writing to/from files) are |
---|
11 | n/a | usually done on the ElementTree level. Interactions with a single XML element |
---|
12 | n/a | and its sub-elements are done on the Element level. |
---|
13 | n/a | |
---|
14 | n/a | Element is a flexible container object designed to store hierarchical data |
---|
15 | n/a | structures in memory. It can be described as a cross between a list and a |
---|
16 | n/a | dictionary. Each Element has a number of properties associated with it: |
---|
17 | n/a | |
---|
18 | n/a | 'tag' - a string containing the element's name. |
---|
19 | n/a | |
---|
20 | n/a | 'attributes' - a Python dictionary storing the element's attributes. |
---|
21 | n/a | |
---|
22 | n/a | 'text' - a string containing the element's text content. |
---|
23 | n/a | |
---|
24 | n/a | 'tail' - an optional string containing text after the element's end tag. |
---|
25 | n/a | |
---|
26 | n/a | And a number of child elements stored in a Python sequence. |
---|
27 | n/a | |
---|
28 | n/a | To create an element instance, use the Element constructor, |
---|
29 | n/a | or the SubElement factory function. |
---|
30 | n/a | |
---|
31 | n/a | You can also use the ElementTree class to wrap an element structure |
---|
32 | n/a | and convert it to and from XML. |
---|
33 | n/a | |
---|
34 | n/a | """ |
---|
35 | n/a | |
---|
36 | n/a | #--------------------------------------------------------------------- |
---|
37 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
38 | n/a | # See http://www.python.org/psf/license for licensing details. |
---|
39 | n/a | # |
---|
40 | n/a | # ElementTree |
---|
41 | n/a | # Copyright (c) 1999-2008 by Fredrik Lundh. All rights reserved. |
---|
42 | n/a | # |
---|
43 | n/a | # fredrik@pythonware.com |
---|
44 | n/a | # http://www.pythonware.com |
---|
45 | n/a | # -------------------------------------------------------------------- |
---|
46 | n/a | # The ElementTree toolkit is |
---|
47 | n/a | # |
---|
48 | n/a | # Copyright (c) 1999-2008 by Fredrik Lundh |
---|
49 | n/a | # |
---|
50 | n/a | # By obtaining, using, and/or copying this software and/or its |
---|
51 | n/a | # associated documentation, you agree that you have read, understood, |
---|
52 | n/a | # and will comply with the following terms and conditions: |
---|
53 | n/a | # |
---|
54 | n/a | # Permission to use, copy, modify, and distribute this software and |
---|
55 | n/a | # its associated documentation for any purpose and without fee is |
---|
56 | n/a | # hereby granted, provided that the above copyright notice appears in |
---|
57 | n/a | # all copies, and that both that copyright notice and this permission |
---|
58 | n/a | # notice appear in supporting documentation, and that the name of |
---|
59 | n/a | # Secret Labs AB or the author not be used in advertising or publicity |
---|
60 | n/a | # pertaining to distribution of the software without specific, written |
---|
61 | n/a | # prior permission. |
---|
62 | n/a | # |
---|
63 | n/a | # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD |
---|
64 | n/a | # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- |
---|
65 | n/a | # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR |
---|
66 | n/a | # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY |
---|
67 | n/a | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
---|
68 | n/a | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
69 | n/a | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
---|
70 | n/a | # OF THIS SOFTWARE. |
---|
71 | n/a | # -------------------------------------------------------------------- |
---|
72 | n/a | |
---|
73 | n/a | __all__ = [ |
---|
74 | n/a | # public symbols |
---|
75 | n/a | "Comment", |
---|
76 | n/a | "dump", |
---|
77 | n/a | "Element", "ElementTree", |
---|
78 | n/a | "fromstring", "fromstringlist", |
---|
79 | n/a | "iselement", "iterparse", |
---|
80 | n/a | "parse", "ParseError", |
---|
81 | n/a | "PI", "ProcessingInstruction", |
---|
82 | n/a | "QName", |
---|
83 | n/a | "SubElement", |
---|
84 | n/a | "tostring", "tostringlist", |
---|
85 | n/a | "TreeBuilder", |
---|
86 | n/a | "VERSION", |
---|
87 | n/a | "XML", "XMLID", |
---|
88 | n/a | "XMLParser", "XMLPullParser", |
---|
89 | n/a | "register_namespace", |
---|
90 | n/a | ] |
---|
91 | n/a | |
---|
92 | n/a | VERSION = "1.3.0" |
---|
93 | n/a | |
---|
94 | n/a | import sys |
---|
95 | n/a | import re |
---|
96 | n/a | import warnings |
---|
97 | n/a | import io |
---|
98 | n/a | import collections |
---|
99 | n/a | import contextlib |
---|
100 | n/a | |
---|
101 | n/a | from . import ElementPath |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | class ParseError(SyntaxError): |
---|
105 | n/a | """An error when parsing an XML document. |
---|
106 | n/a | |
---|
107 | n/a | In addition to its exception value, a ParseError contains |
---|
108 | n/a | two extra attributes: |
---|
109 | n/a | 'code' - the specific exception code |
---|
110 | n/a | 'position' - the line and column of the error |
---|
111 | n/a | |
---|
112 | n/a | """ |
---|
113 | n/a | pass |
---|
114 | n/a | |
---|
115 | n/a | # -------------------------------------------------------------------- |
---|
116 | n/a | |
---|
117 | n/a | |
---|
118 | n/a | def iselement(element): |
---|
119 | n/a | """Return True if *element* appears to be an Element.""" |
---|
120 | n/a | return hasattr(element, 'tag') |
---|
121 | n/a | |
---|
122 | n/a | |
---|
123 | n/a | class Element: |
---|
124 | n/a | """An XML element. |
---|
125 | n/a | |
---|
126 | n/a | This class is the reference implementation of the Element interface. |
---|
127 | n/a | |
---|
128 | n/a | An element's length is its number of subelements. That means if you |
---|
129 | n/a | want to check if an element is truly empty, you should check BOTH |
---|
130 | n/a | its length AND its text attribute. |
---|
131 | n/a | |
---|
132 | n/a | The element tag, attribute names, and attribute values can be either |
---|
133 | n/a | bytes or strings. |
---|
134 | n/a | |
---|
135 | n/a | *tag* is the element name. *attrib* is an optional dictionary containing |
---|
136 | n/a | element attributes. *extra* are additional element attributes given as |
---|
137 | n/a | keyword arguments. |
---|
138 | n/a | |
---|
139 | n/a | Example form: |
---|
140 | n/a | <tag attrib>text<child/>...</tag>tail |
---|
141 | n/a | |
---|
142 | n/a | """ |
---|
143 | n/a | |
---|
144 | n/a | tag = None |
---|
145 | n/a | """The element's name.""" |
---|
146 | n/a | |
---|
147 | n/a | attrib = None |
---|
148 | n/a | """Dictionary of the element's attributes.""" |
---|
149 | n/a | |
---|
150 | n/a | text = None |
---|
151 | n/a | """ |
---|
152 | n/a | Text before first subelement. This is either a string or the value None. |
---|
153 | n/a | Note that if there is no text, this attribute may be either |
---|
154 | n/a | None or the empty string, depending on the parser. |
---|
155 | n/a | |
---|
156 | n/a | """ |
---|
157 | n/a | |
---|
158 | n/a | tail = None |
---|
159 | n/a | """ |
---|
160 | n/a | Text after this element's end tag, but before the next sibling element's |
---|
161 | n/a | start tag. This is either a string or the value None. Note that if there |
---|
162 | n/a | was no text, this attribute may be either None or an empty string, |
---|
163 | n/a | depending on the parser. |
---|
164 | n/a | |
---|
165 | n/a | """ |
---|
166 | n/a | |
---|
167 | n/a | def __init__(self, tag, attrib={}, **extra): |
---|
168 | n/a | if not isinstance(attrib, dict): |
---|
169 | n/a | raise TypeError("attrib must be dict, not %s" % ( |
---|
170 | n/a | attrib.__class__.__name__,)) |
---|
171 | n/a | attrib = attrib.copy() |
---|
172 | n/a | attrib.update(extra) |
---|
173 | n/a | self.tag = tag |
---|
174 | n/a | self.attrib = attrib |
---|
175 | n/a | self._children = [] |
---|
176 | n/a | |
---|
177 | n/a | def __repr__(self): |
---|
178 | n/a | return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self)) |
---|
179 | n/a | |
---|
180 | n/a | def makeelement(self, tag, attrib): |
---|
181 | n/a | """Create a new element with the same type. |
---|
182 | n/a | |
---|
183 | n/a | *tag* is a string containing the element name. |
---|
184 | n/a | *attrib* is a dictionary containing the element attributes. |
---|
185 | n/a | |
---|
186 | n/a | Do not call this method, use the SubElement factory function instead. |
---|
187 | n/a | |
---|
188 | n/a | """ |
---|
189 | n/a | return self.__class__(tag, attrib) |
---|
190 | n/a | |
---|
191 | n/a | def copy(self): |
---|
192 | n/a | """Return copy of current element. |
---|
193 | n/a | |
---|
194 | n/a | This creates a shallow copy. Subelements will be shared with the |
---|
195 | n/a | original tree. |
---|
196 | n/a | |
---|
197 | n/a | """ |
---|
198 | n/a | elem = self.makeelement(self.tag, self.attrib) |
---|
199 | n/a | elem.text = self.text |
---|
200 | n/a | elem.tail = self.tail |
---|
201 | n/a | elem[:] = self |
---|
202 | n/a | return elem |
---|
203 | n/a | |
---|
204 | n/a | def __len__(self): |
---|
205 | n/a | return len(self._children) |
---|
206 | n/a | |
---|
207 | n/a | def __bool__(self): |
---|
208 | n/a | warnings.warn( |
---|
209 | n/a | "The behavior of this method will change in future versions. " |
---|
210 | n/a | "Use specific 'len(elem)' or 'elem is not None' test instead.", |
---|
211 | n/a | FutureWarning, stacklevel=2 |
---|
212 | n/a | ) |
---|
213 | n/a | return len(self._children) != 0 # emulate old behaviour, for now |
---|
214 | n/a | |
---|
215 | n/a | def __getitem__(self, index): |
---|
216 | n/a | return self._children[index] |
---|
217 | n/a | |
---|
218 | n/a | def __setitem__(self, index, element): |
---|
219 | n/a | # if isinstance(index, slice): |
---|
220 | n/a | # for elt in element: |
---|
221 | n/a | # assert iselement(elt) |
---|
222 | n/a | # else: |
---|
223 | n/a | # assert iselement(element) |
---|
224 | n/a | self._children[index] = element |
---|
225 | n/a | |
---|
226 | n/a | def __delitem__(self, index): |
---|
227 | n/a | del self._children[index] |
---|
228 | n/a | |
---|
229 | n/a | def append(self, subelement): |
---|
230 | n/a | """Add *subelement* to the end of this element. |
---|
231 | n/a | |
---|
232 | n/a | The new element will appear in document order after the last existing |
---|
233 | n/a | subelement (or directly after the text, if it's the first subelement), |
---|
234 | n/a | but before the end tag for this element. |
---|
235 | n/a | |
---|
236 | n/a | """ |
---|
237 | n/a | self._assert_is_element(subelement) |
---|
238 | n/a | self._children.append(subelement) |
---|
239 | n/a | |
---|
240 | n/a | def extend(self, elements): |
---|
241 | n/a | """Append subelements from a sequence. |
---|
242 | n/a | |
---|
243 | n/a | *elements* is a sequence with zero or more elements. |
---|
244 | n/a | |
---|
245 | n/a | """ |
---|
246 | n/a | for element in elements: |
---|
247 | n/a | self._assert_is_element(element) |
---|
248 | n/a | self._children.extend(elements) |
---|
249 | n/a | |
---|
250 | n/a | def insert(self, index, subelement): |
---|
251 | n/a | """Insert *subelement* at position *index*.""" |
---|
252 | n/a | self._assert_is_element(subelement) |
---|
253 | n/a | self._children.insert(index, subelement) |
---|
254 | n/a | |
---|
255 | n/a | def _assert_is_element(self, e): |
---|
256 | n/a | # Need to refer to the actual Python implementation, not the |
---|
257 | n/a | # shadowing C implementation. |
---|
258 | n/a | if not isinstance(e, _Element_Py): |
---|
259 | n/a | raise TypeError('expected an Element, not %s' % type(e).__name__) |
---|
260 | n/a | |
---|
261 | n/a | def remove(self, subelement): |
---|
262 | n/a | """Remove matching subelement. |
---|
263 | n/a | |
---|
264 | n/a | Unlike the find methods, this method compares elements based on |
---|
265 | n/a | identity, NOT ON tag value or contents. To remove subelements by |
---|
266 | n/a | other means, the easiest way is to use a list comprehension to |
---|
267 | n/a | select what elements to keep, and then use slice assignment to update |
---|
268 | n/a | the parent element. |
---|
269 | n/a | |
---|
270 | n/a | ValueError is raised if a matching element could not be found. |
---|
271 | n/a | |
---|
272 | n/a | """ |
---|
273 | n/a | # assert iselement(element) |
---|
274 | n/a | self._children.remove(subelement) |
---|
275 | n/a | |
---|
276 | n/a | def getchildren(self): |
---|
277 | n/a | """(Deprecated) Return all subelements. |
---|
278 | n/a | |
---|
279 | n/a | Elements are returned in document order. |
---|
280 | n/a | |
---|
281 | n/a | """ |
---|
282 | n/a | warnings.warn( |
---|
283 | n/a | "This method will be removed in future versions. " |
---|
284 | n/a | "Use 'list(elem)' or iteration over elem instead.", |
---|
285 | n/a | DeprecationWarning, stacklevel=2 |
---|
286 | n/a | ) |
---|
287 | n/a | return self._children |
---|
288 | n/a | |
---|
289 | n/a | def find(self, path, namespaces=None): |
---|
290 | n/a | """Find first matching element by tag name or path. |
---|
291 | n/a | |
---|
292 | n/a | *path* is a string having either an element tag or an XPath, |
---|
293 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
294 | n/a | |
---|
295 | n/a | Return the first matching element, or None if no element was found. |
---|
296 | n/a | |
---|
297 | n/a | """ |
---|
298 | n/a | return ElementPath.find(self, path, namespaces) |
---|
299 | n/a | |
---|
300 | n/a | def findtext(self, path, default=None, namespaces=None): |
---|
301 | n/a | """Find text for first matching element by tag name or path. |
---|
302 | n/a | |
---|
303 | n/a | *path* is a string having either an element tag or an XPath, |
---|
304 | n/a | *default* is the value to return if the element was not found, |
---|
305 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
306 | n/a | |
---|
307 | n/a | Return text content of first matching element, or default value if |
---|
308 | n/a | none was found. Note that if an element is found having no text |
---|
309 | n/a | content, the empty string is returned. |
---|
310 | n/a | |
---|
311 | n/a | """ |
---|
312 | n/a | return ElementPath.findtext(self, path, default, namespaces) |
---|
313 | n/a | |
---|
314 | n/a | def findall(self, path, namespaces=None): |
---|
315 | n/a | """Find all matching subelements by tag name or path. |
---|
316 | n/a | |
---|
317 | n/a | *path* is a string having either an element tag or an XPath, |
---|
318 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
319 | n/a | |
---|
320 | n/a | Returns list containing all matching elements in document order. |
---|
321 | n/a | |
---|
322 | n/a | """ |
---|
323 | n/a | return ElementPath.findall(self, path, namespaces) |
---|
324 | n/a | |
---|
325 | n/a | def iterfind(self, path, namespaces=None): |
---|
326 | n/a | """Find all matching subelements by tag name or path. |
---|
327 | n/a | |
---|
328 | n/a | *path* is a string having either an element tag or an XPath, |
---|
329 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
330 | n/a | |
---|
331 | n/a | Return an iterable yielding all matching elements in document order. |
---|
332 | n/a | |
---|
333 | n/a | """ |
---|
334 | n/a | return ElementPath.iterfind(self, path, namespaces) |
---|
335 | n/a | |
---|
336 | n/a | def clear(self): |
---|
337 | n/a | """Reset element. |
---|
338 | n/a | |
---|
339 | n/a | This function removes all subelements, clears all attributes, and sets |
---|
340 | n/a | the text and tail attributes to None. |
---|
341 | n/a | |
---|
342 | n/a | """ |
---|
343 | n/a | self.attrib.clear() |
---|
344 | n/a | self._children = [] |
---|
345 | n/a | self.text = self.tail = None |
---|
346 | n/a | |
---|
347 | n/a | def get(self, key, default=None): |
---|
348 | n/a | """Get element attribute. |
---|
349 | n/a | |
---|
350 | n/a | Equivalent to attrib.get, but some implementations may handle this a |
---|
351 | n/a | bit more efficiently. *key* is what attribute to look for, and |
---|
352 | n/a | *default* is what to return if the attribute was not found. |
---|
353 | n/a | |
---|
354 | n/a | Returns a string containing the attribute value, or the default if |
---|
355 | n/a | attribute was not found. |
---|
356 | n/a | |
---|
357 | n/a | """ |
---|
358 | n/a | return self.attrib.get(key, default) |
---|
359 | n/a | |
---|
360 | n/a | def set(self, key, value): |
---|
361 | n/a | """Set element attribute. |
---|
362 | n/a | |
---|
363 | n/a | Equivalent to attrib[key] = value, but some implementations may handle |
---|
364 | n/a | this a bit more efficiently. *key* is what attribute to set, and |
---|
365 | n/a | *value* is the attribute value to set it to. |
---|
366 | n/a | |
---|
367 | n/a | """ |
---|
368 | n/a | self.attrib[key] = value |
---|
369 | n/a | |
---|
370 | n/a | def keys(self): |
---|
371 | n/a | """Get list of attribute names. |
---|
372 | n/a | |
---|
373 | n/a | Names are returned in an arbitrary order, just like an ordinary |
---|
374 | n/a | Python dict. Equivalent to attrib.keys() |
---|
375 | n/a | |
---|
376 | n/a | """ |
---|
377 | n/a | return self.attrib.keys() |
---|
378 | n/a | |
---|
379 | n/a | def items(self): |
---|
380 | n/a | """Get element attributes as a sequence. |
---|
381 | n/a | |
---|
382 | n/a | The attributes are returned in arbitrary order. Equivalent to |
---|
383 | n/a | attrib.items(). |
---|
384 | n/a | |
---|
385 | n/a | Return a list of (name, value) tuples. |
---|
386 | n/a | |
---|
387 | n/a | """ |
---|
388 | n/a | return self.attrib.items() |
---|
389 | n/a | |
---|
390 | n/a | def iter(self, tag=None): |
---|
391 | n/a | """Create tree iterator. |
---|
392 | n/a | |
---|
393 | n/a | The iterator loops over the element and all subelements in document |
---|
394 | n/a | order, returning all elements with a matching tag. |
---|
395 | n/a | |
---|
396 | n/a | If the tree structure is modified during iteration, new or removed |
---|
397 | n/a | elements may or may not be included. To get a stable set, use the |
---|
398 | n/a | list() function on the iterator, and loop over the resulting list. |
---|
399 | n/a | |
---|
400 | n/a | *tag* is what tags to look for (default is to return all elements) |
---|
401 | n/a | |
---|
402 | n/a | Return an iterator containing all the matching elements. |
---|
403 | n/a | |
---|
404 | n/a | """ |
---|
405 | n/a | if tag == "*": |
---|
406 | n/a | tag = None |
---|
407 | n/a | if tag is None or self.tag == tag: |
---|
408 | n/a | yield self |
---|
409 | n/a | for e in self._children: |
---|
410 | n/a | yield from e.iter(tag) |
---|
411 | n/a | |
---|
412 | n/a | # compatibility |
---|
413 | n/a | def getiterator(self, tag=None): |
---|
414 | n/a | # Change for a DeprecationWarning in 1.4 |
---|
415 | n/a | warnings.warn( |
---|
416 | n/a | "This method will be removed in future versions. " |
---|
417 | n/a | "Use 'elem.iter()' or 'list(elem.iter())' instead.", |
---|
418 | n/a | PendingDeprecationWarning, stacklevel=2 |
---|
419 | n/a | ) |
---|
420 | n/a | return list(self.iter(tag)) |
---|
421 | n/a | |
---|
422 | n/a | def itertext(self): |
---|
423 | n/a | """Create text iterator. |
---|
424 | n/a | |
---|
425 | n/a | The iterator loops over the element and all subelements in document |
---|
426 | n/a | order, returning all inner text. |
---|
427 | n/a | |
---|
428 | n/a | """ |
---|
429 | n/a | tag = self.tag |
---|
430 | n/a | if not isinstance(tag, str) and tag is not None: |
---|
431 | n/a | return |
---|
432 | n/a | t = self.text |
---|
433 | n/a | if t: |
---|
434 | n/a | yield t |
---|
435 | n/a | for e in self: |
---|
436 | n/a | yield from e.itertext() |
---|
437 | n/a | t = e.tail |
---|
438 | n/a | if t: |
---|
439 | n/a | yield t |
---|
440 | n/a | |
---|
441 | n/a | |
---|
442 | n/a | def SubElement(parent, tag, attrib={}, **extra): |
---|
443 | n/a | """Subelement factory which creates an element instance, and appends it |
---|
444 | n/a | to an existing parent. |
---|
445 | n/a | |
---|
446 | n/a | The element tag, attribute names, and attribute values can be either |
---|
447 | n/a | bytes or Unicode strings. |
---|
448 | n/a | |
---|
449 | n/a | *parent* is the parent element, *tag* is the subelements name, *attrib* is |
---|
450 | n/a | an optional directory containing element attributes, *extra* are |
---|
451 | n/a | additional attributes given as keyword arguments. |
---|
452 | n/a | |
---|
453 | n/a | """ |
---|
454 | n/a | attrib = attrib.copy() |
---|
455 | n/a | attrib.update(extra) |
---|
456 | n/a | element = parent.makeelement(tag, attrib) |
---|
457 | n/a | parent.append(element) |
---|
458 | n/a | return element |
---|
459 | n/a | |
---|
460 | n/a | |
---|
461 | n/a | def Comment(text=None): |
---|
462 | n/a | """Comment element factory. |
---|
463 | n/a | |
---|
464 | n/a | This function creates a special element which the standard serializer |
---|
465 | n/a | serializes as an XML comment. |
---|
466 | n/a | |
---|
467 | n/a | *text* is a string containing the comment string. |
---|
468 | n/a | |
---|
469 | n/a | """ |
---|
470 | n/a | element = Element(Comment) |
---|
471 | n/a | element.text = text |
---|
472 | n/a | return element |
---|
473 | n/a | |
---|
474 | n/a | |
---|
475 | n/a | def ProcessingInstruction(target, text=None): |
---|
476 | n/a | """Processing Instruction element factory. |
---|
477 | n/a | |
---|
478 | n/a | This function creates a special element which the standard serializer |
---|
479 | n/a | serializes as an XML comment. |
---|
480 | n/a | |
---|
481 | n/a | *target* is a string containing the processing instruction, *text* is a |
---|
482 | n/a | string containing the processing instruction contents, if any. |
---|
483 | n/a | |
---|
484 | n/a | """ |
---|
485 | n/a | element = Element(ProcessingInstruction) |
---|
486 | n/a | element.text = target |
---|
487 | n/a | if text: |
---|
488 | n/a | element.text = element.text + " " + text |
---|
489 | n/a | return element |
---|
490 | n/a | |
---|
491 | n/a | PI = ProcessingInstruction |
---|
492 | n/a | |
---|
493 | n/a | |
---|
494 | n/a | class QName: |
---|
495 | n/a | """Qualified name wrapper. |
---|
496 | n/a | |
---|
497 | n/a | This class can be used to wrap a QName attribute value in order to get |
---|
498 | n/a | proper namespace handing on output. |
---|
499 | n/a | |
---|
500 | n/a | *text_or_uri* is a string containing the QName value either in the form |
---|
501 | n/a | {uri}local, or if the tag argument is given, the URI part of a QName. |
---|
502 | n/a | |
---|
503 | n/a | *tag* is an optional argument which if given, will make the first |
---|
504 | n/a | argument (text_or_uri) be interpreted as a URI, and this argument (tag) |
---|
505 | n/a | be interpreted as a local name. |
---|
506 | n/a | |
---|
507 | n/a | """ |
---|
508 | n/a | def __init__(self, text_or_uri, tag=None): |
---|
509 | n/a | if tag: |
---|
510 | n/a | text_or_uri = "{%s}%s" % (text_or_uri, tag) |
---|
511 | n/a | self.text = text_or_uri |
---|
512 | n/a | def __str__(self): |
---|
513 | n/a | return self.text |
---|
514 | n/a | def __repr__(self): |
---|
515 | n/a | return '<%s %r>' % (self.__class__.__name__, self.text) |
---|
516 | n/a | def __hash__(self): |
---|
517 | n/a | return hash(self.text) |
---|
518 | n/a | def __le__(self, other): |
---|
519 | n/a | if isinstance(other, QName): |
---|
520 | n/a | return self.text <= other.text |
---|
521 | n/a | return self.text <= other |
---|
522 | n/a | def __lt__(self, other): |
---|
523 | n/a | if isinstance(other, QName): |
---|
524 | n/a | return self.text < other.text |
---|
525 | n/a | return self.text < other |
---|
526 | n/a | def __ge__(self, other): |
---|
527 | n/a | if isinstance(other, QName): |
---|
528 | n/a | return self.text >= other.text |
---|
529 | n/a | return self.text >= other |
---|
530 | n/a | def __gt__(self, other): |
---|
531 | n/a | if isinstance(other, QName): |
---|
532 | n/a | return self.text > other.text |
---|
533 | n/a | return self.text > other |
---|
534 | n/a | def __eq__(self, other): |
---|
535 | n/a | if isinstance(other, QName): |
---|
536 | n/a | return self.text == other.text |
---|
537 | n/a | return self.text == other |
---|
538 | n/a | |
---|
539 | n/a | # -------------------------------------------------------------------- |
---|
540 | n/a | |
---|
541 | n/a | |
---|
542 | n/a | class ElementTree: |
---|
543 | n/a | """An XML element hierarchy. |
---|
544 | n/a | |
---|
545 | n/a | This class also provides support for serialization to and from |
---|
546 | n/a | standard XML. |
---|
547 | n/a | |
---|
548 | n/a | *element* is an optional root element node, |
---|
549 | n/a | *file* is an optional file handle or file name of an XML file whose |
---|
550 | n/a | contents will be used to initialize the tree with. |
---|
551 | n/a | |
---|
552 | n/a | """ |
---|
553 | n/a | def __init__(self, element=None, file=None): |
---|
554 | n/a | # assert element is None or iselement(element) |
---|
555 | n/a | self._root = element # first node |
---|
556 | n/a | if file: |
---|
557 | n/a | self.parse(file) |
---|
558 | n/a | |
---|
559 | n/a | def getroot(self): |
---|
560 | n/a | """Return root element of this tree.""" |
---|
561 | n/a | return self._root |
---|
562 | n/a | |
---|
563 | n/a | def _setroot(self, element): |
---|
564 | n/a | """Replace root element of this tree. |
---|
565 | n/a | |
---|
566 | n/a | This will discard the current contents of the tree and replace it |
---|
567 | n/a | with the given element. Use with care! |
---|
568 | n/a | |
---|
569 | n/a | """ |
---|
570 | n/a | # assert iselement(element) |
---|
571 | n/a | self._root = element |
---|
572 | n/a | |
---|
573 | n/a | def parse(self, source, parser=None): |
---|
574 | n/a | """Load external XML document into element tree. |
---|
575 | n/a | |
---|
576 | n/a | *source* is a file name or file object, *parser* is an optional parser |
---|
577 | n/a | instance that defaults to XMLParser. |
---|
578 | n/a | |
---|
579 | n/a | ParseError is raised if the parser fails to parse the document. |
---|
580 | n/a | |
---|
581 | n/a | Returns the root element of the given source document. |
---|
582 | n/a | |
---|
583 | n/a | """ |
---|
584 | n/a | close_source = False |
---|
585 | n/a | if not hasattr(source, "read"): |
---|
586 | n/a | source = open(source, "rb") |
---|
587 | n/a | close_source = True |
---|
588 | n/a | try: |
---|
589 | n/a | if parser is None: |
---|
590 | n/a | # If no parser was specified, create a default XMLParser |
---|
591 | n/a | parser = XMLParser() |
---|
592 | n/a | if hasattr(parser, '_parse_whole'): |
---|
593 | n/a | # The default XMLParser, when it comes from an accelerator, |
---|
594 | n/a | # can define an internal _parse_whole API for efficiency. |
---|
595 | n/a | # It can be used to parse the whole source without feeding |
---|
596 | n/a | # it with chunks. |
---|
597 | n/a | self._root = parser._parse_whole(source) |
---|
598 | n/a | return self._root |
---|
599 | n/a | while True: |
---|
600 | n/a | data = source.read(65536) |
---|
601 | n/a | if not data: |
---|
602 | n/a | break |
---|
603 | n/a | parser.feed(data) |
---|
604 | n/a | self._root = parser.close() |
---|
605 | n/a | return self._root |
---|
606 | n/a | finally: |
---|
607 | n/a | if close_source: |
---|
608 | n/a | source.close() |
---|
609 | n/a | |
---|
610 | n/a | def iter(self, tag=None): |
---|
611 | n/a | """Create and return tree iterator for the root element. |
---|
612 | n/a | |
---|
613 | n/a | The iterator loops over all elements in this tree, in document order. |
---|
614 | n/a | |
---|
615 | n/a | *tag* is a string with the tag name to iterate over |
---|
616 | n/a | (default is to return all elements). |
---|
617 | n/a | |
---|
618 | n/a | """ |
---|
619 | n/a | # assert self._root is not None |
---|
620 | n/a | return self._root.iter(tag) |
---|
621 | n/a | |
---|
622 | n/a | # compatibility |
---|
623 | n/a | def getiterator(self, tag=None): |
---|
624 | n/a | # Change for a DeprecationWarning in 1.4 |
---|
625 | n/a | warnings.warn( |
---|
626 | n/a | "This method will be removed in future versions. " |
---|
627 | n/a | "Use 'tree.iter()' or 'list(tree.iter())' instead.", |
---|
628 | n/a | PendingDeprecationWarning, stacklevel=2 |
---|
629 | n/a | ) |
---|
630 | n/a | return list(self.iter(tag)) |
---|
631 | n/a | |
---|
632 | n/a | def find(self, path, namespaces=None): |
---|
633 | n/a | """Find first matching element by tag name or path. |
---|
634 | n/a | |
---|
635 | n/a | Same as getroot().find(path), which is Element.find() |
---|
636 | n/a | |
---|
637 | n/a | *path* is a string having either an element tag or an XPath, |
---|
638 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
639 | n/a | |
---|
640 | n/a | Return the first matching element, or None if no element was found. |
---|
641 | n/a | |
---|
642 | n/a | """ |
---|
643 | n/a | # assert self._root is not None |
---|
644 | n/a | if path[:1] == "/": |
---|
645 | n/a | path = "." + path |
---|
646 | n/a | warnings.warn( |
---|
647 | n/a | "This search is broken in 1.3 and earlier, and will be " |
---|
648 | n/a | "fixed in a future version. If you rely on the current " |
---|
649 | n/a | "behaviour, change it to %r" % path, |
---|
650 | n/a | FutureWarning, stacklevel=2 |
---|
651 | n/a | ) |
---|
652 | n/a | return self._root.find(path, namespaces) |
---|
653 | n/a | |
---|
654 | n/a | def findtext(self, path, default=None, namespaces=None): |
---|
655 | n/a | """Find first matching element by tag name or path. |
---|
656 | n/a | |
---|
657 | n/a | Same as getroot().findtext(path), which is Element.findtext() |
---|
658 | n/a | |
---|
659 | n/a | *path* is a string having either an element tag or an XPath, |
---|
660 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
661 | n/a | |
---|
662 | n/a | Return the first matching element, or None if no element was found. |
---|
663 | n/a | |
---|
664 | n/a | """ |
---|
665 | n/a | # assert self._root is not None |
---|
666 | n/a | if path[:1] == "/": |
---|
667 | n/a | path = "." + path |
---|
668 | n/a | warnings.warn( |
---|
669 | n/a | "This search is broken in 1.3 and earlier, and will be " |
---|
670 | n/a | "fixed in a future version. If you rely on the current " |
---|
671 | n/a | "behaviour, change it to %r" % path, |
---|
672 | n/a | FutureWarning, stacklevel=2 |
---|
673 | n/a | ) |
---|
674 | n/a | return self._root.findtext(path, default, namespaces) |
---|
675 | n/a | |
---|
676 | n/a | def findall(self, path, namespaces=None): |
---|
677 | n/a | """Find all matching subelements by tag name or path. |
---|
678 | n/a | |
---|
679 | n/a | Same as getroot().findall(path), which is Element.findall(). |
---|
680 | n/a | |
---|
681 | n/a | *path* is a string having either an element tag or an XPath, |
---|
682 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
683 | n/a | |
---|
684 | n/a | Return list containing all matching elements in document order. |
---|
685 | n/a | |
---|
686 | n/a | """ |
---|
687 | n/a | # assert self._root is not None |
---|
688 | n/a | if path[:1] == "/": |
---|
689 | n/a | path = "." + path |
---|
690 | n/a | warnings.warn( |
---|
691 | n/a | "This search is broken in 1.3 and earlier, and will be " |
---|
692 | n/a | "fixed in a future version. If you rely on the current " |
---|
693 | n/a | "behaviour, change it to %r" % path, |
---|
694 | n/a | FutureWarning, stacklevel=2 |
---|
695 | n/a | ) |
---|
696 | n/a | return self._root.findall(path, namespaces) |
---|
697 | n/a | |
---|
698 | n/a | def iterfind(self, path, namespaces=None): |
---|
699 | n/a | """Find all matching subelements by tag name or path. |
---|
700 | n/a | |
---|
701 | n/a | Same as getroot().iterfind(path), which is element.iterfind() |
---|
702 | n/a | |
---|
703 | n/a | *path* is a string having either an element tag or an XPath, |
---|
704 | n/a | *namespaces* is an optional mapping from namespace prefix to full name. |
---|
705 | n/a | |
---|
706 | n/a | Return an iterable yielding all matching elements in document order. |
---|
707 | n/a | |
---|
708 | n/a | """ |
---|
709 | n/a | # assert self._root is not None |
---|
710 | n/a | if path[:1] == "/": |
---|
711 | n/a | path = "." + path |
---|
712 | n/a | warnings.warn( |
---|
713 | n/a | "This search is broken in 1.3 and earlier, and will be " |
---|
714 | n/a | "fixed in a future version. If you rely on the current " |
---|
715 | n/a | "behaviour, change it to %r" % path, |
---|
716 | n/a | FutureWarning, stacklevel=2 |
---|
717 | n/a | ) |
---|
718 | n/a | return self._root.iterfind(path, namespaces) |
---|
719 | n/a | |
---|
720 | n/a | def write(self, file_or_filename, |
---|
721 | n/a | encoding=None, |
---|
722 | n/a | xml_declaration=None, |
---|
723 | n/a | default_namespace=None, |
---|
724 | n/a | method=None, *, |
---|
725 | n/a | short_empty_elements=True): |
---|
726 | n/a | """Write element tree to a file as XML. |
---|
727 | n/a | |
---|
728 | n/a | Arguments: |
---|
729 | n/a | *file_or_filename* -- file name or a file object opened for writing |
---|
730 | n/a | |
---|
731 | n/a | *encoding* -- the output encoding (default: US-ASCII) |
---|
732 | n/a | |
---|
733 | n/a | *xml_declaration* -- bool indicating if an XML declaration should be |
---|
734 | n/a | added to the output. If None, an XML declaration |
---|
735 | n/a | is added if encoding IS NOT either of: |
---|
736 | n/a | US-ASCII, UTF-8, or Unicode |
---|
737 | n/a | |
---|
738 | n/a | *default_namespace* -- sets the default XML namespace (for "xmlns") |
---|
739 | n/a | |
---|
740 | n/a | *method* -- either "xml" (default), "html, "text", or "c14n" |
---|
741 | n/a | |
---|
742 | n/a | *short_empty_elements* -- controls the formatting of elements |
---|
743 | n/a | that contain no content. If True (default) |
---|
744 | n/a | they are emitted as a single self-closed |
---|
745 | n/a | tag, otherwise they are emitted as a pair |
---|
746 | n/a | of start/end tags |
---|
747 | n/a | |
---|
748 | n/a | """ |
---|
749 | n/a | if not method: |
---|
750 | n/a | method = "xml" |
---|
751 | n/a | elif method not in _serialize: |
---|
752 | n/a | raise ValueError("unknown method %r" % method) |
---|
753 | n/a | if not encoding: |
---|
754 | n/a | if method == "c14n": |
---|
755 | n/a | encoding = "utf-8" |
---|
756 | n/a | else: |
---|
757 | n/a | encoding = "us-ascii" |
---|
758 | n/a | enc_lower = encoding.lower() |
---|
759 | n/a | with _get_writer(file_or_filename, enc_lower) as write: |
---|
760 | n/a | if method == "xml" and (xml_declaration or |
---|
761 | n/a | (xml_declaration is None and |
---|
762 | n/a | enc_lower not in ("utf-8", "us-ascii", "unicode"))): |
---|
763 | n/a | declared_encoding = encoding |
---|
764 | n/a | if enc_lower == "unicode": |
---|
765 | n/a | # Retrieve the default encoding for the xml declaration |
---|
766 | n/a | import locale |
---|
767 | n/a | declared_encoding = locale.getpreferredencoding() |
---|
768 | n/a | write("<?xml version='1.0' encoding='%s'?>\n" % ( |
---|
769 | n/a | declared_encoding,)) |
---|
770 | n/a | if method == "text": |
---|
771 | n/a | _serialize_text(write, self._root) |
---|
772 | n/a | else: |
---|
773 | n/a | qnames, namespaces = _namespaces(self._root, default_namespace) |
---|
774 | n/a | serialize = _serialize[method] |
---|
775 | n/a | serialize(write, self._root, qnames, namespaces, |
---|
776 | n/a | short_empty_elements=short_empty_elements) |
---|
777 | n/a | |
---|
778 | n/a | def write_c14n(self, file): |
---|
779 | n/a | # lxml.etree compatibility. use output method instead |
---|
780 | n/a | return self.write(file, method="c14n") |
---|
781 | n/a | |
---|
782 | n/a | # -------------------------------------------------------------------- |
---|
783 | n/a | # serialization support |
---|
784 | n/a | |
---|
785 | n/a | @contextlib.contextmanager |
---|
786 | n/a | def _get_writer(file_or_filename, encoding): |
---|
787 | n/a | # returns text write method and release all resources after using |
---|
788 | n/a | try: |
---|
789 | n/a | write = file_or_filename.write |
---|
790 | n/a | except AttributeError: |
---|
791 | n/a | # file_or_filename is a file name |
---|
792 | n/a | if encoding == "unicode": |
---|
793 | n/a | file = open(file_or_filename, "w") |
---|
794 | n/a | else: |
---|
795 | n/a | file = open(file_or_filename, "w", encoding=encoding, |
---|
796 | n/a | errors="xmlcharrefreplace") |
---|
797 | n/a | with file: |
---|
798 | n/a | yield file.write |
---|
799 | n/a | else: |
---|
800 | n/a | # file_or_filename is a file-like object |
---|
801 | n/a | # encoding determines if it is a text or binary writer |
---|
802 | n/a | if encoding == "unicode": |
---|
803 | n/a | # use a text writer as is |
---|
804 | n/a | yield write |
---|
805 | n/a | else: |
---|
806 | n/a | # wrap a binary writer with TextIOWrapper |
---|
807 | n/a | with contextlib.ExitStack() as stack: |
---|
808 | n/a | if isinstance(file_or_filename, io.BufferedIOBase): |
---|
809 | n/a | file = file_or_filename |
---|
810 | n/a | elif isinstance(file_or_filename, io.RawIOBase): |
---|
811 | n/a | file = io.BufferedWriter(file_or_filename) |
---|
812 | n/a | # Keep the original file open when the BufferedWriter is |
---|
813 | n/a | # destroyed |
---|
814 | n/a | stack.callback(file.detach) |
---|
815 | n/a | else: |
---|
816 | n/a | # This is to handle passed objects that aren't in the |
---|
817 | n/a | # IOBase hierarchy, but just have a write method |
---|
818 | n/a | file = io.BufferedIOBase() |
---|
819 | n/a | file.writable = lambda: True |
---|
820 | n/a | file.write = write |
---|
821 | n/a | try: |
---|
822 | n/a | # TextIOWrapper uses this methods to determine |
---|
823 | n/a | # if BOM (for UTF-16, etc) should be added |
---|
824 | n/a | file.seekable = file_or_filename.seekable |
---|
825 | n/a | file.tell = file_or_filename.tell |
---|
826 | n/a | except AttributeError: |
---|
827 | n/a | pass |
---|
828 | n/a | file = io.TextIOWrapper(file, |
---|
829 | n/a | encoding=encoding, |
---|
830 | n/a | errors="xmlcharrefreplace", |
---|
831 | n/a | newline="\n") |
---|
832 | n/a | # Keep the original file open when the TextIOWrapper is |
---|
833 | n/a | # destroyed |
---|
834 | n/a | stack.callback(file.detach) |
---|
835 | n/a | yield file.write |
---|
836 | n/a | |
---|
837 | n/a | def _namespaces(elem, default_namespace=None): |
---|
838 | n/a | # identify namespaces used in this tree |
---|
839 | n/a | |
---|
840 | n/a | # maps qnames to *encoded* prefix:local names |
---|
841 | n/a | qnames = {None: None} |
---|
842 | n/a | |
---|
843 | n/a | # maps uri:s to prefixes |
---|
844 | n/a | namespaces = {} |
---|
845 | n/a | if default_namespace: |
---|
846 | n/a | namespaces[default_namespace] = "" |
---|
847 | n/a | |
---|
848 | n/a | def add_qname(qname): |
---|
849 | n/a | # calculate serialized qname representation |
---|
850 | n/a | try: |
---|
851 | n/a | if qname[:1] == "{": |
---|
852 | n/a | uri, tag = qname[1:].rsplit("}", 1) |
---|
853 | n/a | prefix = namespaces.get(uri) |
---|
854 | n/a | if prefix is None: |
---|
855 | n/a | prefix = _namespace_map.get(uri) |
---|
856 | n/a | if prefix is None: |
---|
857 | n/a | prefix = "ns%d" % len(namespaces) |
---|
858 | n/a | if prefix != "xml": |
---|
859 | n/a | namespaces[uri] = prefix |
---|
860 | n/a | if prefix: |
---|
861 | n/a | qnames[qname] = "%s:%s" % (prefix, tag) |
---|
862 | n/a | else: |
---|
863 | n/a | qnames[qname] = tag # default element |
---|
864 | n/a | else: |
---|
865 | n/a | if default_namespace: |
---|
866 | n/a | # FIXME: can this be handled in XML 1.0? |
---|
867 | n/a | raise ValueError( |
---|
868 | n/a | "cannot use non-qualified names with " |
---|
869 | n/a | "default_namespace option" |
---|
870 | n/a | ) |
---|
871 | n/a | qnames[qname] = qname |
---|
872 | n/a | except TypeError: |
---|
873 | n/a | _raise_serialization_error(qname) |
---|
874 | n/a | |
---|
875 | n/a | # populate qname and namespaces table |
---|
876 | n/a | for elem in elem.iter(): |
---|
877 | n/a | tag = elem.tag |
---|
878 | n/a | if isinstance(tag, QName): |
---|
879 | n/a | if tag.text not in qnames: |
---|
880 | n/a | add_qname(tag.text) |
---|
881 | n/a | elif isinstance(tag, str): |
---|
882 | n/a | if tag not in qnames: |
---|
883 | n/a | add_qname(tag) |
---|
884 | n/a | elif tag is not None and tag is not Comment and tag is not PI: |
---|
885 | n/a | _raise_serialization_error(tag) |
---|
886 | n/a | for key, value in elem.items(): |
---|
887 | n/a | if isinstance(key, QName): |
---|
888 | n/a | key = key.text |
---|
889 | n/a | if key not in qnames: |
---|
890 | n/a | add_qname(key) |
---|
891 | n/a | if isinstance(value, QName) and value.text not in qnames: |
---|
892 | n/a | add_qname(value.text) |
---|
893 | n/a | text = elem.text |
---|
894 | n/a | if isinstance(text, QName) and text.text not in qnames: |
---|
895 | n/a | add_qname(text.text) |
---|
896 | n/a | return qnames, namespaces |
---|
897 | n/a | |
---|
898 | n/a | def _serialize_xml(write, elem, qnames, namespaces, |
---|
899 | n/a | short_empty_elements, **kwargs): |
---|
900 | n/a | tag = elem.tag |
---|
901 | n/a | text = elem.text |
---|
902 | n/a | if tag is Comment: |
---|
903 | n/a | write("<!--%s-->" % text) |
---|
904 | n/a | elif tag is ProcessingInstruction: |
---|
905 | n/a | write("<?%s?>" % text) |
---|
906 | n/a | else: |
---|
907 | n/a | tag = qnames[tag] |
---|
908 | n/a | if tag is None: |
---|
909 | n/a | if text: |
---|
910 | n/a | write(_escape_cdata(text)) |
---|
911 | n/a | for e in elem: |
---|
912 | n/a | _serialize_xml(write, e, qnames, None, |
---|
913 | n/a | short_empty_elements=short_empty_elements) |
---|
914 | n/a | else: |
---|
915 | n/a | write("<" + tag) |
---|
916 | n/a | items = list(elem.items()) |
---|
917 | n/a | if items or namespaces: |
---|
918 | n/a | if namespaces: |
---|
919 | n/a | for v, k in sorted(namespaces.items(), |
---|
920 | n/a | key=lambda x: x[1]): # sort on prefix |
---|
921 | n/a | if k: |
---|
922 | n/a | k = ":" + k |
---|
923 | n/a | write(" xmlns%s=\"%s\"" % ( |
---|
924 | n/a | k, |
---|
925 | n/a | _escape_attrib(v) |
---|
926 | n/a | )) |
---|
927 | n/a | for k, v in sorted(items): # lexical order |
---|
928 | n/a | if isinstance(k, QName): |
---|
929 | n/a | k = k.text |
---|
930 | n/a | if isinstance(v, QName): |
---|
931 | n/a | v = qnames[v.text] |
---|
932 | n/a | else: |
---|
933 | n/a | v = _escape_attrib(v) |
---|
934 | n/a | write(" %s=\"%s\"" % (qnames[k], v)) |
---|
935 | n/a | if text or len(elem) or not short_empty_elements: |
---|
936 | n/a | write(">") |
---|
937 | n/a | if text: |
---|
938 | n/a | write(_escape_cdata(text)) |
---|
939 | n/a | for e in elem: |
---|
940 | n/a | _serialize_xml(write, e, qnames, None, |
---|
941 | n/a | short_empty_elements=short_empty_elements) |
---|
942 | n/a | write("</" + tag + ">") |
---|
943 | n/a | else: |
---|
944 | n/a | write(" />") |
---|
945 | n/a | if elem.tail: |
---|
946 | n/a | write(_escape_cdata(elem.tail)) |
---|
947 | n/a | |
---|
948 | n/a | HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr", |
---|
949 | n/a | "img", "input", "isindex", "link", "meta", "param") |
---|
950 | n/a | |
---|
951 | n/a | try: |
---|
952 | n/a | HTML_EMPTY = set(HTML_EMPTY) |
---|
953 | n/a | except NameError: |
---|
954 | n/a | pass |
---|
955 | n/a | |
---|
956 | n/a | def _serialize_html(write, elem, qnames, namespaces, **kwargs): |
---|
957 | n/a | tag = elem.tag |
---|
958 | n/a | text = elem.text |
---|
959 | n/a | if tag is Comment: |
---|
960 | n/a | write("<!--%s-->" % _escape_cdata(text)) |
---|
961 | n/a | elif tag is ProcessingInstruction: |
---|
962 | n/a | write("<?%s?>" % _escape_cdata(text)) |
---|
963 | n/a | else: |
---|
964 | n/a | tag = qnames[tag] |
---|
965 | n/a | if tag is None: |
---|
966 | n/a | if text: |
---|
967 | n/a | write(_escape_cdata(text)) |
---|
968 | n/a | for e in elem: |
---|
969 | n/a | _serialize_html(write, e, qnames, None) |
---|
970 | n/a | else: |
---|
971 | n/a | write("<" + tag) |
---|
972 | n/a | items = list(elem.items()) |
---|
973 | n/a | if items or namespaces: |
---|
974 | n/a | if namespaces: |
---|
975 | n/a | for v, k in sorted(namespaces.items(), |
---|
976 | n/a | key=lambda x: x[1]): # sort on prefix |
---|
977 | n/a | if k: |
---|
978 | n/a | k = ":" + k |
---|
979 | n/a | write(" xmlns%s=\"%s\"" % ( |
---|
980 | n/a | k, |
---|
981 | n/a | _escape_attrib(v) |
---|
982 | n/a | )) |
---|
983 | n/a | for k, v in sorted(items): # lexical order |
---|
984 | n/a | if isinstance(k, QName): |
---|
985 | n/a | k = k.text |
---|
986 | n/a | if isinstance(v, QName): |
---|
987 | n/a | v = qnames[v.text] |
---|
988 | n/a | else: |
---|
989 | n/a | v = _escape_attrib_html(v) |
---|
990 | n/a | # FIXME: handle boolean attributes |
---|
991 | n/a | write(" %s=\"%s\"" % (qnames[k], v)) |
---|
992 | n/a | write(">") |
---|
993 | n/a | ltag = tag.lower() |
---|
994 | n/a | if text: |
---|
995 | n/a | if ltag == "script" or ltag == "style": |
---|
996 | n/a | write(text) |
---|
997 | n/a | else: |
---|
998 | n/a | write(_escape_cdata(text)) |
---|
999 | n/a | for e in elem: |
---|
1000 | n/a | _serialize_html(write, e, qnames, None) |
---|
1001 | n/a | if ltag not in HTML_EMPTY: |
---|
1002 | n/a | write("</" + tag + ">") |
---|
1003 | n/a | if elem.tail: |
---|
1004 | n/a | write(_escape_cdata(elem.tail)) |
---|
1005 | n/a | |
---|
1006 | n/a | def _serialize_text(write, elem): |
---|
1007 | n/a | for part in elem.itertext(): |
---|
1008 | n/a | write(part) |
---|
1009 | n/a | if elem.tail: |
---|
1010 | n/a | write(elem.tail) |
---|
1011 | n/a | |
---|
1012 | n/a | _serialize = { |
---|
1013 | n/a | "xml": _serialize_xml, |
---|
1014 | n/a | "html": _serialize_html, |
---|
1015 | n/a | "text": _serialize_text, |
---|
1016 | n/a | # this optional method is imported at the end of the module |
---|
1017 | n/a | # "c14n": _serialize_c14n, |
---|
1018 | n/a | } |
---|
1019 | n/a | |
---|
1020 | n/a | |
---|
1021 | n/a | def register_namespace(prefix, uri): |
---|
1022 | n/a | """Register a namespace prefix. |
---|
1023 | n/a | |
---|
1024 | n/a | The registry is global, and any existing mapping for either the |
---|
1025 | n/a | given prefix or the namespace URI will be removed. |
---|
1026 | n/a | |
---|
1027 | n/a | *prefix* is the namespace prefix, *uri* is a namespace uri. Tags and |
---|
1028 | n/a | attributes in this namespace will be serialized with prefix if possible. |
---|
1029 | n/a | |
---|
1030 | n/a | ValueError is raised if prefix is reserved or is invalid. |
---|
1031 | n/a | |
---|
1032 | n/a | """ |
---|
1033 | n/a | if re.match(r"ns\d+$", prefix): |
---|
1034 | n/a | raise ValueError("Prefix format reserved for internal use") |
---|
1035 | n/a | for k, v in list(_namespace_map.items()): |
---|
1036 | n/a | if k == uri or v == prefix: |
---|
1037 | n/a | del _namespace_map[k] |
---|
1038 | n/a | _namespace_map[uri] = prefix |
---|
1039 | n/a | |
---|
1040 | n/a | _namespace_map = { |
---|
1041 | n/a | # "well-known" namespace prefixes |
---|
1042 | n/a | "http://www.w3.org/XML/1998/namespace": "xml", |
---|
1043 | n/a | "http://www.w3.org/1999/xhtml": "html", |
---|
1044 | n/a | "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf", |
---|
1045 | n/a | "http://schemas.xmlsoap.org/wsdl/": "wsdl", |
---|
1046 | n/a | # xml schema |
---|
1047 | n/a | "http://www.w3.org/2001/XMLSchema": "xs", |
---|
1048 | n/a | "http://www.w3.org/2001/XMLSchema-instance": "xsi", |
---|
1049 | n/a | # dublin core |
---|
1050 | n/a | "http://purl.org/dc/elements/1.1/": "dc", |
---|
1051 | n/a | } |
---|
1052 | n/a | # For tests and troubleshooting |
---|
1053 | n/a | register_namespace._namespace_map = _namespace_map |
---|
1054 | n/a | |
---|
1055 | n/a | def _raise_serialization_error(text): |
---|
1056 | n/a | raise TypeError( |
---|
1057 | n/a | "cannot serialize %r (type %s)" % (text, type(text).__name__) |
---|
1058 | n/a | ) |
---|
1059 | n/a | |
---|
1060 | n/a | def _escape_cdata(text): |
---|
1061 | n/a | # escape character data |
---|
1062 | n/a | try: |
---|
1063 | n/a | # it's worth avoiding do-nothing calls for strings that are |
---|
1064 | n/a | # shorter than 500 character, or so. assume that's, by far, |
---|
1065 | n/a | # the most common case in most applications. |
---|
1066 | n/a | if "&" in text: |
---|
1067 | n/a | text = text.replace("&", "&") |
---|
1068 | n/a | if "<" in text: |
---|
1069 | n/a | text = text.replace("<", "<") |
---|
1070 | n/a | if ">" in text: |
---|
1071 | n/a | text = text.replace(">", ">") |
---|
1072 | n/a | return text |
---|
1073 | n/a | except (TypeError, AttributeError): |
---|
1074 | n/a | _raise_serialization_error(text) |
---|
1075 | n/a | |
---|
1076 | n/a | def _escape_attrib(text): |
---|
1077 | n/a | # escape attribute value |
---|
1078 | n/a | try: |
---|
1079 | n/a | if "&" in text: |
---|
1080 | n/a | text = text.replace("&", "&") |
---|
1081 | n/a | if "<" in text: |
---|
1082 | n/a | text = text.replace("<", "<") |
---|
1083 | n/a | if ">" in text: |
---|
1084 | n/a | text = text.replace(">", ">") |
---|
1085 | n/a | if "\"" in text: |
---|
1086 | n/a | text = text.replace("\"", """) |
---|
1087 | n/a | # The following business with carriage returns is to satisfy |
---|
1088 | n/a | # Section 2.11 of the XML specification, stating that |
---|
1089 | n/a | # CR or CR LN should be replaced with just LN |
---|
1090 | n/a | # http://www.w3.org/TR/REC-xml/#sec-line-ends |
---|
1091 | n/a | if "\r\n" in text: |
---|
1092 | n/a | text = text.replace("\r\n", "\n") |
---|
1093 | n/a | if "\r" in text: |
---|
1094 | n/a | text = text.replace("\r", "\n") |
---|
1095 | n/a | #The following four lines are issue 17582 |
---|
1096 | n/a | if "\n" in text: |
---|
1097 | n/a | text = text.replace("\n", " ") |
---|
1098 | n/a | if "\t" in text: |
---|
1099 | n/a | text = text.replace("\t", "	") |
---|
1100 | n/a | return text |
---|
1101 | n/a | except (TypeError, AttributeError): |
---|
1102 | n/a | _raise_serialization_error(text) |
---|
1103 | n/a | |
---|
1104 | n/a | def _escape_attrib_html(text): |
---|
1105 | n/a | # escape attribute value |
---|
1106 | n/a | try: |
---|
1107 | n/a | if "&" in text: |
---|
1108 | n/a | text = text.replace("&", "&") |
---|
1109 | n/a | if ">" in text: |
---|
1110 | n/a | text = text.replace(">", ">") |
---|
1111 | n/a | if "\"" in text: |
---|
1112 | n/a | text = text.replace("\"", """) |
---|
1113 | n/a | return text |
---|
1114 | n/a | except (TypeError, AttributeError): |
---|
1115 | n/a | _raise_serialization_error(text) |
---|
1116 | n/a | |
---|
1117 | n/a | # -------------------------------------------------------------------- |
---|
1118 | n/a | |
---|
1119 | n/a | def tostring(element, encoding=None, method=None, *, |
---|
1120 | n/a | short_empty_elements=True): |
---|
1121 | n/a | """Generate string representation of XML element. |
---|
1122 | n/a | |
---|
1123 | n/a | All subelements are included. If encoding is "unicode", a string |
---|
1124 | n/a | is returned. Otherwise a bytestring is returned. |
---|
1125 | n/a | |
---|
1126 | n/a | *element* is an Element instance, *encoding* is an optional output |
---|
1127 | n/a | encoding defaulting to US-ASCII, *method* is an optional output which can |
---|
1128 | n/a | be one of "xml" (default), "html", "text" or "c14n". |
---|
1129 | n/a | |
---|
1130 | n/a | Returns an (optionally) encoded string containing the XML data. |
---|
1131 | n/a | |
---|
1132 | n/a | """ |
---|
1133 | n/a | stream = io.StringIO() if encoding == 'unicode' else io.BytesIO() |
---|
1134 | n/a | ElementTree(element).write(stream, encoding, method=method, |
---|
1135 | n/a | short_empty_elements=short_empty_elements) |
---|
1136 | n/a | return stream.getvalue() |
---|
1137 | n/a | |
---|
1138 | n/a | class _ListDataStream(io.BufferedIOBase): |
---|
1139 | n/a | """An auxiliary stream accumulating into a list reference.""" |
---|
1140 | n/a | def __init__(self, lst): |
---|
1141 | n/a | self.lst = lst |
---|
1142 | n/a | |
---|
1143 | n/a | def writable(self): |
---|
1144 | n/a | return True |
---|
1145 | n/a | |
---|
1146 | n/a | def seekable(self): |
---|
1147 | n/a | return True |
---|
1148 | n/a | |
---|
1149 | n/a | def write(self, b): |
---|
1150 | n/a | self.lst.append(b) |
---|
1151 | n/a | |
---|
1152 | n/a | def tell(self): |
---|
1153 | n/a | return len(self.lst) |
---|
1154 | n/a | |
---|
1155 | n/a | def tostringlist(element, encoding=None, method=None, *, |
---|
1156 | n/a | short_empty_elements=True): |
---|
1157 | n/a | lst = [] |
---|
1158 | n/a | stream = _ListDataStream(lst) |
---|
1159 | n/a | ElementTree(element).write(stream, encoding, method=method, |
---|
1160 | n/a | short_empty_elements=short_empty_elements) |
---|
1161 | n/a | return lst |
---|
1162 | n/a | |
---|
1163 | n/a | |
---|
1164 | n/a | def dump(elem): |
---|
1165 | n/a | """Write element tree or element structure to sys.stdout. |
---|
1166 | n/a | |
---|
1167 | n/a | This function should be used for debugging only. |
---|
1168 | n/a | |
---|
1169 | n/a | *elem* is either an ElementTree, or a single Element. The exact output |
---|
1170 | n/a | format is implementation dependent. In this version, it's written as an |
---|
1171 | n/a | ordinary XML file. |
---|
1172 | n/a | |
---|
1173 | n/a | """ |
---|
1174 | n/a | # debugging |
---|
1175 | n/a | if not isinstance(elem, ElementTree): |
---|
1176 | n/a | elem = ElementTree(elem) |
---|
1177 | n/a | elem.write(sys.stdout, encoding="unicode") |
---|
1178 | n/a | tail = elem.getroot().tail |
---|
1179 | n/a | if not tail or tail[-1] != "\n": |
---|
1180 | n/a | sys.stdout.write("\n") |
---|
1181 | n/a | |
---|
1182 | n/a | # -------------------------------------------------------------------- |
---|
1183 | n/a | # parsing |
---|
1184 | n/a | |
---|
1185 | n/a | |
---|
1186 | n/a | def parse(source, parser=None): |
---|
1187 | n/a | """Parse XML document into element tree. |
---|
1188 | n/a | |
---|
1189 | n/a | *source* is a filename or file object containing XML data, |
---|
1190 | n/a | *parser* is an optional parser instance defaulting to XMLParser. |
---|
1191 | n/a | |
---|
1192 | n/a | Return an ElementTree instance. |
---|
1193 | n/a | |
---|
1194 | n/a | """ |
---|
1195 | n/a | tree = ElementTree() |
---|
1196 | n/a | tree.parse(source, parser) |
---|
1197 | n/a | return tree |
---|
1198 | n/a | |
---|
1199 | n/a | |
---|
1200 | n/a | def iterparse(source, events=None, parser=None): |
---|
1201 | n/a | """Incrementally parse XML document into ElementTree. |
---|
1202 | n/a | |
---|
1203 | n/a | This class also reports what's going on to the user based on the |
---|
1204 | n/a | *events* it is initialized with. The supported events are the strings |
---|
1205 | n/a | "start", "end", "start-ns" and "end-ns" (the "ns" events are used to get |
---|
1206 | n/a | detailed namespace information). If *events* is omitted, only |
---|
1207 | n/a | "end" events are reported. |
---|
1208 | n/a | |
---|
1209 | n/a | *source* is a filename or file object containing XML data, *events* is |
---|
1210 | n/a | a list of events to report back, *parser* is an optional parser instance. |
---|
1211 | n/a | |
---|
1212 | n/a | Returns an iterator providing (event, elem) pairs. |
---|
1213 | n/a | |
---|
1214 | n/a | """ |
---|
1215 | n/a | # Use the internal, undocumented _parser argument for now; When the |
---|
1216 | n/a | # parser argument of iterparse is removed, this can be killed. |
---|
1217 | n/a | pullparser = XMLPullParser(events=events, _parser=parser) |
---|
1218 | n/a | def iterator(): |
---|
1219 | n/a | try: |
---|
1220 | n/a | while True: |
---|
1221 | n/a | yield from pullparser.read_events() |
---|
1222 | n/a | # load event buffer |
---|
1223 | n/a | data = source.read(16 * 1024) |
---|
1224 | n/a | if not data: |
---|
1225 | n/a | break |
---|
1226 | n/a | pullparser.feed(data) |
---|
1227 | n/a | root = pullparser._close_and_return_root() |
---|
1228 | n/a | yield from pullparser.read_events() |
---|
1229 | n/a | it.root = root |
---|
1230 | n/a | finally: |
---|
1231 | n/a | if close_source: |
---|
1232 | n/a | source.close() |
---|
1233 | n/a | |
---|
1234 | n/a | class IterParseIterator(collections.Iterator): |
---|
1235 | n/a | __next__ = iterator().__next__ |
---|
1236 | n/a | it = IterParseIterator() |
---|
1237 | n/a | it.root = None |
---|
1238 | n/a | del iterator, IterParseIterator |
---|
1239 | n/a | |
---|
1240 | n/a | close_source = False |
---|
1241 | n/a | if not hasattr(source, "read"): |
---|
1242 | n/a | source = open(source, "rb") |
---|
1243 | n/a | close_source = True |
---|
1244 | n/a | |
---|
1245 | n/a | return it |
---|
1246 | n/a | |
---|
1247 | n/a | |
---|
1248 | n/a | class XMLPullParser: |
---|
1249 | n/a | |
---|
1250 | n/a | def __init__(self, events=None, *, _parser=None): |
---|
1251 | n/a | # The _parser argument is for internal use only and must not be relied |
---|
1252 | n/a | # upon in user code. It will be removed in a future release. |
---|
1253 | n/a | # See http://bugs.python.org/issue17741 for more details. |
---|
1254 | n/a | |
---|
1255 | n/a | self._events_queue = collections.deque() |
---|
1256 | n/a | self._parser = _parser or XMLParser(target=TreeBuilder()) |
---|
1257 | n/a | # wire up the parser for event reporting |
---|
1258 | n/a | if events is None: |
---|
1259 | n/a | events = ("end",) |
---|
1260 | n/a | self._parser._setevents(self._events_queue, events) |
---|
1261 | n/a | |
---|
1262 | n/a | def feed(self, data): |
---|
1263 | n/a | """Feed encoded data to parser.""" |
---|
1264 | n/a | if self._parser is None: |
---|
1265 | n/a | raise ValueError("feed() called after end of stream") |
---|
1266 | n/a | if data: |
---|
1267 | n/a | try: |
---|
1268 | n/a | self._parser.feed(data) |
---|
1269 | n/a | except SyntaxError as exc: |
---|
1270 | n/a | self._events_queue.append(exc) |
---|
1271 | n/a | |
---|
1272 | n/a | def _close_and_return_root(self): |
---|
1273 | n/a | # iterparse needs this to set its root attribute properly :( |
---|
1274 | n/a | root = self._parser.close() |
---|
1275 | n/a | self._parser = None |
---|
1276 | n/a | return root |
---|
1277 | n/a | |
---|
1278 | n/a | def close(self): |
---|
1279 | n/a | """Finish feeding data to parser. |
---|
1280 | n/a | |
---|
1281 | n/a | Unlike XMLParser, does not return the root element. Use |
---|
1282 | n/a | read_events() to consume elements from XMLPullParser. |
---|
1283 | n/a | """ |
---|
1284 | n/a | self._close_and_return_root() |
---|
1285 | n/a | |
---|
1286 | n/a | def read_events(self): |
---|
1287 | n/a | """Return an iterator over currently available (event, elem) pairs. |
---|
1288 | n/a | |
---|
1289 | n/a | Events are consumed from the internal event queue as they are |
---|
1290 | n/a | retrieved from the iterator. |
---|
1291 | n/a | """ |
---|
1292 | n/a | events = self._events_queue |
---|
1293 | n/a | while events: |
---|
1294 | n/a | event = events.popleft() |
---|
1295 | n/a | if isinstance(event, Exception): |
---|
1296 | n/a | raise event |
---|
1297 | n/a | else: |
---|
1298 | n/a | yield event |
---|
1299 | n/a | |
---|
1300 | n/a | |
---|
1301 | n/a | def XML(text, parser=None): |
---|
1302 | n/a | """Parse XML document from string constant. |
---|
1303 | n/a | |
---|
1304 | n/a | This function can be used to embed "XML Literals" in Python code. |
---|
1305 | n/a | |
---|
1306 | n/a | *text* is a string containing XML data, *parser* is an |
---|
1307 | n/a | optional parser instance, defaulting to the standard XMLParser. |
---|
1308 | n/a | |
---|
1309 | n/a | Returns an Element instance. |
---|
1310 | n/a | |
---|
1311 | n/a | """ |
---|
1312 | n/a | if not parser: |
---|
1313 | n/a | parser = XMLParser(target=TreeBuilder()) |
---|
1314 | n/a | parser.feed(text) |
---|
1315 | n/a | return parser.close() |
---|
1316 | n/a | |
---|
1317 | n/a | |
---|
1318 | n/a | def XMLID(text, parser=None): |
---|
1319 | n/a | """Parse XML document from string constant for its IDs. |
---|
1320 | n/a | |
---|
1321 | n/a | *text* is a string containing XML data, *parser* is an |
---|
1322 | n/a | optional parser instance, defaulting to the standard XMLParser. |
---|
1323 | n/a | |
---|
1324 | n/a | Returns an (Element, dict) tuple, in which the |
---|
1325 | n/a | dict maps element id:s to elements. |
---|
1326 | n/a | |
---|
1327 | n/a | """ |
---|
1328 | n/a | if not parser: |
---|
1329 | n/a | parser = XMLParser(target=TreeBuilder()) |
---|
1330 | n/a | parser.feed(text) |
---|
1331 | n/a | tree = parser.close() |
---|
1332 | n/a | ids = {} |
---|
1333 | n/a | for elem in tree.iter(): |
---|
1334 | n/a | id = elem.get("id") |
---|
1335 | n/a | if id: |
---|
1336 | n/a | ids[id] = elem |
---|
1337 | n/a | return tree, ids |
---|
1338 | n/a | |
---|
1339 | n/a | # Parse XML document from string constant. Alias for XML(). |
---|
1340 | n/a | fromstring = XML |
---|
1341 | n/a | |
---|
1342 | n/a | def fromstringlist(sequence, parser=None): |
---|
1343 | n/a | """Parse XML document from sequence of string fragments. |
---|
1344 | n/a | |
---|
1345 | n/a | *sequence* is a list of other sequence, *parser* is an optional parser |
---|
1346 | n/a | instance, defaulting to the standard XMLParser. |
---|
1347 | n/a | |
---|
1348 | n/a | Returns an Element instance. |
---|
1349 | n/a | |
---|
1350 | n/a | """ |
---|
1351 | n/a | if not parser: |
---|
1352 | n/a | parser = XMLParser(target=TreeBuilder()) |
---|
1353 | n/a | for text in sequence: |
---|
1354 | n/a | parser.feed(text) |
---|
1355 | n/a | return parser.close() |
---|
1356 | n/a | |
---|
1357 | n/a | # -------------------------------------------------------------------- |
---|
1358 | n/a | |
---|
1359 | n/a | |
---|
1360 | n/a | class TreeBuilder: |
---|
1361 | n/a | """Generic element structure builder. |
---|
1362 | n/a | |
---|
1363 | n/a | This builder converts a sequence of start, data, and end method |
---|
1364 | n/a | calls to a well-formed element structure. |
---|
1365 | n/a | |
---|
1366 | n/a | You can use this class to build an element structure using a custom XML |
---|
1367 | n/a | parser, or a parser for some other XML-like format. |
---|
1368 | n/a | |
---|
1369 | n/a | *element_factory* is an optional element factory which is called |
---|
1370 | n/a | to create new Element instances, as necessary. |
---|
1371 | n/a | |
---|
1372 | n/a | """ |
---|
1373 | n/a | def __init__(self, element_factory=None): |
---|
1374 | n/a | self._data = [] # data collector |
---|
1375 | n/a | self._elem = [] # element stack |
---|
1376 | n/a | self._last = None # last element |
---|
1377 | n/a | self._tail = None # true if we're after an end tag |
---|
1378 | n/a | if element_factory is None: |
---|
1379 | n/a | element_factory = Element |
---|
1380 | n/a | self._factory = element_factory |
---|
1381 | n/a | |
---|
1382 | n/a | def close(self): |
---|
1383 | n/a | """Flush builder buffers and return toplevel document Element.""" |
---|
1384 | n/a | assert len(self._elem) == 0, "missing end tags" |
---|
1385 | n/a | assert self._last is not None, "missing toplevel element" |
---|
1386 | n/a | return self._last |
---|
1387 | n/a | |
---|
1388 | n/a | def _flush(self): |
---|
1389 | n/a | if self._data: |
---|
1390 | n/a | if self._last is not None: |
---|
1391 | n/a | text = "".join(self._data) |
---|
1392 | n/a | if self._tail: |
---|
1393 | n/a | assert self._last.tail is None, "internal error (tail)" |
---|
1394 | n/a | self._last.tail = text |
---|
1395 | n/a | else: |
---|
1396 | n/a | assert self._last.text is None, "internal error (text)" |
---|
1397 | n/a | self._last.text = text |
---|
1398 | n/a | self._data = [] |
---|
1399 | n/a | |
---|
1400 | n/a | def data(self, data): |
---|
1401 | n/a | """Add text to current element.""" |
---|
1402 | n/a | self._data.append(data) |
---|
1403 | n/a | |
---|
1404 | n/a | def start(self, tag, attrs): |
---|
1405 | n/a | """Open new element and return it. |
---|
1406 | n/a | |
---|
1407 | n/a | *tag* is the element name, *attrs* is a dict containing element |
---|
1408 | n/a | attributes. |
---|
1409 | n/a | |
---|
1410 | n/a | """ |
---|
1411 | n/a | self._flush() |
---|
1412 | n/a | self._last = elem = self._factory(tag, attrs) |
---|
1413 | n/a | if self._elem: |
---|
1414 | n/a | self._elem[-1].append(elem) |
---|
1415 | n/a | self._elem.append(elem) |
---|
1416 | n/a | self._tail = 0 |
---|
1417 | n/a | return elem |
---|
1418 | n/a | |
---|
1419 | n/a | def end(self, tag): |
---|
1420 | n/a | """Close and return current Element. |
---|
1421 | n/a | |
---|
1422 | n/a | *tag* is the element name. |
---|
1423 | n/a | |
---|
1424 | n/a | """ |
---|
1425 | n/a | self._flush() |
---|
1426 | n/a | self._last = self._elem.pop() |
---|
1427 | n/a | assert self._last.tag == tag,\ |
---|
1428 | n/a | "end tag mismatch (expected %s, got %s)" % ( |
---|
1429 | n/a | self._last.tag, tag) |
---|
1430 | n/a | self._tail = 1 |
---|
1431 | n/a | return self._last |
---|
1432 | n/a | |
---|
1433 | n/a | |
---|
1434 | n/a | # also see ElementTree and TreeBuilder |
---|
1435 | n/a | class XMLParser: |
---|
1436 | n/a | """Element structure builder for XML source data based on the expat parser. |
---|
1437 | n/a | |
---|
1438 | n/a | *html* are predefined HTML entities (deprecated and not supported), |
---|
1439 | n/a | *target* is an optional target object which defaults to an instance of the |
---|
1440 | n/a | standard TreeBuilder class, *encoding* is an optional encoding string |
---|
1441 | n/a | which if given, overrides the encoding specified in the XML file: |
---|
1442 | n/a | http://www.iana.org/assignments/character-sets |
---|
1443 | n/a | |
---|
1444 | n/a | """ |
---|
1445 | n/a | |
---|
1446 | n/a | def __init__(self, html=0, target=None, encoding=None): |
---|
1447 | n/a | try: |
---|
1448 | n/a | from xml.parsers import expat |
---|
1449 | n/a | except ImportError: |
---|
1450 | n/a | try: |
---|
1451 | n/a | import pyexpat as expat |
---|
1452 | n/a | except ImportError: |
---|
1453 | n/a | raise ImportError( |
---|
1454 | n/a | "No module named expat; use SimpleXMLTreeBuilder instead" |
---|
1455 | n/a | ) |
---|
1456 | n/a | parser = expat.ParserCreate(encoding, "}") |
---|
1457 | n/a | if target is None: |
---|
1458 | n/a | target = TreeBuilder() |
---|
1459 | n/a | # underscored names are provided for compatibility only |
---|
1460 | n/a | self.parser = self._parser = parser |
---|
1461 | n/a | self.target = self._target = target |
---|
1462 | n/a | self._error = expat.error |
---|
1463 | n/a | self._names = {} # name memo cache |
---|
1464 | n/a | # main callbacks |
---|
1465 | n/a | parser.DefaultHandlerExpand = self._default |
---|
1466 | n/a | if hasattr(target, 'start'): |
---|
1467 | n/a | parser.StartElementHandler = self._start |
---|
1468 | n/a | if hasattr(target, 'end'): |
---|
1469 | n/a | parser.EndElementHandler = self._end |
---|
1470 | n/a | if hasattr(target, 'data'): |
---|
1471 | n/a | parser.CharacterDataHandler = target.data |
---|
1472 | n/a | # miscellaneous callbacks |
---|
1473 | n/a | if hasattr(target, 'comment'): |
---|
1474 | n/a | parser.CommentHandler = target.comment |
---|
1475 | n/a | if hasattr(target, 'pi'): |
---|
1476 | n/a | parser.ProcessingInstructionHandler = target.pi |
---|
1477 | n/a | # Configure pyexpat: buffering, new-style attribute handling. |
---|
1478 | n/a | parser.buffer_text = 1 |
---|
1479 | n/a | parser.ordered_attributes = 1 |
---|
1480 | n/a | parser.specified_attributes = 1 |
---|
1481 | n/a | self._doctype = None |
---|
1482 | n/a | self.entity = {} |
---|
1483 | n/a | try: |
---|
1484 | n/a | self.version = "Expat %d.%d.%d" % expat.version_info |
---|
1485 | n/a | except AttributeError: |
---|
1486 | n/a | pass # unknown |
---|
1487 | n/a | |
---|
1488 | n/a | def _setevents(self, events_queue, events_to_report): |
---|
1489 | n/a | # Internal API for XMLPullParser |
---|
1490 | n/a | # events_to_report: a list of events to report during parsing (same as |
---|
1491 | n/a | # the *events* of XMLPullParser's constructor. |
---|
1492 | n/a | # events_queue: a list of actual parsing events that will be populated |
---|
1493 | n/a | # by the underlying parser. |
---|
1494 | n/a | # |
---|
1495 | n/a | parser = self._parser |
---|
1496 | n/a | append = events_queue.append |
---|
1497 | n/a | for event_name in events_to_report: |
---|
1498 | n/a | if event_name == "start": |
---|
1499 | n/a | parser.ordered_attributes = 1 |
---|
1500 | n/a | parser.specified_attributes = 1 |
---|
1501 | n/a | def handler(tag, attrib_in, event=event_name, append=append, |
---|
1502 | n/a | start=self._start): |
---|
1503 | n/a | append((event, start(tag, attrib_in))) |
---|
1504 | n/a | parser.StartElementHandler = handler |
---|
1505 | n/a | elif event_name == "end": |
---|
1506 | n/a | def handler(tag, event=event_name, append=append, |
---|
1507 | n/a | end=self._end): |
---|
1508 | n/a | append((event, end(tag))) |
---|
1509 | n/a | parser.EndElementHandler = handler |
---|
1510 | n/a | elif event_name == "start-ns": |
---|
1511 | n/a | def handler(prefix, uri, event=event_name, append=append): |
---|
1512 | n/a | append((event, (prefix or "", uri or ""))) |
---|
1513 | n/a | parser.StartNamespaceDeclHandler = handler |
---|
1514 | n/a | elif event_name == "end-ns": |
---|
1515 | n/a | def handler(prefix, event=event_name, append=append): |
---|
1516 | n/a | append((event, None)) |
---|
1517 | n/a | parser.EndNamespaceDeclHandler = handler |
---|
1518 | n/a | else: |
---|
1519 | n/a | raise ValueError("unknown event %r" % event_name) |
---|
1520 | n/a | |
---|
1521 | n/a | def _raiseerror(self, value): |
---|
1522 | n/a | err = ParseError(value) |
---|
1523 | n/a | err.code = value.code |
---|
1524 | n/a | err.position = value.lineno, value.offset |
---|
1525 | n/a | raise err |
---|
1526 | n/a | |
---|
1527 | n/a | def _fixname(self, key): |
---|
1528 | n/a | # expand qname, and convert name string to ascii, if possible |
---|
1529 | n/a | try: |
---|
1530 | n/a | name = self._names[key] |
---|
1531 | n/a | except KeyError: |
---|
1532 | n/a | name = key |
---|
1533 | n/a | if "}" in name: |
---|
1534 | n/a | name = "{" + name |
---|
1535 | n/a | self._names[key] = name |
---|
1536 | n/a | return name |
---|
1537 | n/a | |
---|
1538 | n/a | def _start(self, tag, attr_list): |
---|
1539 | n/a | # Handler for expat's StartElementHandler. Since ordered_attributes |
---|
1540 | n/a | # is set, the attributes are reported as a list of alternating |
---|
1541 | n/a | # attribute name,value. |
---|
1542 | n/a | fixname = self._fixname |
---|
1543 | n/a | tag = fixname(tag) |
---|
1544 | n/a | attrib = {} |
---|
1545 | n/a | if attr_list: |
---|
1546 | n/a | for i in range(0, len(attr_list), 2): |
---|
1547 | n/a | attrib[fixname(attr_list[i])] = attr_list[i+1] |
---|
1548 | n/a | return self.target.start(tag, attrib) |
---|
1549 | n/a | |
---|
1550 | n/a | def _end(self, tag): |
---|
1551 | n/a | return self.target.end(self._fixname(tag)) |
---|
1552 | n/a | |
---|
1553 | n/a | def _default(self, text): |
---|
1554 | n/a | prefix = text[:1] |
---|
1555 | n/a | if prefix == "&": |
---|
1556 | n/a | # deal with undefined entities |
---|
1557 | n/a | try: |
---|
1558 | n/a | data_handler = self.target.data |
---|
1559 | n/a | except AttributeError: |
---|
1560 | n/a | return |
---|
1561 | n/a | try: |
---|
1562 | n/a | data_handler(self.entity[text[1:-1]]) |
---|
1563 | n/a | except KeyError: |
---|
1564 | n/a | from xml.parsers import expat |
---|
1565 | n/a | err = expat.error( |
---|
1566 | n/a | "undefined entity %s: line %d, column %d" % |
---|
1567 | n/a | (text, self.parser.ErrorLineNumber, |
---|
1568 | n/a | self.parser.ErrorColumnNumber) |
---|
1569 | n/a | ) |
---|
1570 | n/a | err.code = 11 # XML_ERROR_UNDEFINED_ENTITY |
---|
1571 | n/a | err.lineno = self.parser.ErrorLineNumber |
---|
1572 | n/a | err.offset = self.parser.ErrorColumnNumber |
---|
1573 | n/a | raise err |
---|
1574 | n/a | elif prefix == "<" and text[:9] == "<!DOCTYPE": |
---|
1575 | n/a | self._doctype = [] # inside a doctype declaration |
---|
1576 | n/a | elif self._doctype is not None: |
---|
1577 | n/a | # parse doctype contents |
---|
1578 | n/a | if prefix == ">": |
---|
1579 | n/a | self._doctype = None |
---|
1580 | n/a | return |
---|
1581 | n/a | text = text.strip() |
---|
1582 | n/a | if not text: |
---|
1583 | n/a | return |
---|
1584 | n/a | self._doctype.append(text) |
---|
1585 | n/a | n = len(self._doctype) |
---|
1586 | n/a | if n > 2: |
---|
1587 | n/a | type = self._doctype[1] |
---|
1588 | n/a | if type == "PUBLIC" and n == 4: |
---|
1589 | n/a | name, type, pubid, system = self._doctype |
---|
1590 | n/a | if pubid: |
---|
1591 | n/a | pubid = pubid[1:-1] |
---|
1592 | n/a | elif type == "SYSTEM" and n == 3: |
---|
1593 | n/a | name, type, system = self._doctype |
---|
1594 | n/a | pubid = None |
---|
1595 | n/a | else: |
---|
1596 | n/a | return |
---|
1597 | n/a | if hasattr(self.target, "doctype"): |
---|
1598 | n/a | self.target.doctype(name, pubid, system[1:-1]) |
---|
1599 | n/a | elif self.doctype != self._XMLParser__doctype: |
---|
1600 | n/a | # warn about deprecated call |
---|
1601 | n/a | self._XMLParser__doctype(name, pubid, system[1:-1]) |
---|
1602 | n/a | self.doctype(name, pubid, system[1:-1]) |
---|
1603 | n/a | self._doctype = None |
---|
1604 | n/a | |
---|
1605 | n/a | def doctype(self, name, pubid, system): |
---|
1606 | n/a | """(Deprecated) Handle doctype declaration |
---|
1607 | n/a | |
---|
1608 | n/a | *name* is the Doctype name, *pubid* is the public identifier, |
---|
1609 | n/a | and *system* is the system identifier. |
---|
1610 | n/a | |
---|
1611 | n/a | """ |
---|
1612 | n/a | warnings.warn( |
---|
1613 | n/a | "This method of XMLParser is deprecated. Define doctype() " |
---|
1614 | n/a | "method on the TreeBuilder target.", |
---|
1615 | n/a | DeprecationWarning, |
---|
1616 | n/a | ) |
---|
1617 | n/a | |
---|
1618 | n/a | # sentinel, if doctype is redefined in a subclass |
---|
1619 | n/a | __doctype = doctype |
---|
1620 | n/a | |
---|
1621 | n/a | def feed(self, data): |
---|
1622 | n/a | """Feed encoded data to parser.""" |
---|
1623 | n/a | try: |
---|
1624 | n/a | self.parser.Parse(data, 0) |
---|
1625 | n/a | except self._error as v: |
---|
1626 | n/a | self._raiseerror(v) |
---|
1627 | n/a | |
---|
1628 | n/a | def close(self): |
---|
1629 | n/a | """Finish feeding data to parser and return element structure.""" |
---|
1630 | n/a | try: |
---|
1631 | n/a | self.parser.Parse("", 1) # end of data |
---|
1632 | n/a | except self._error as v: |
---|
1633 | n/a | self._raiseerror(v) |
---|
1634 | n/a | try: |
---|
1635 | n/a | close_handler = self.target.close |
---|
1636 | n/a | except AttributeError: |
---|
1637 | n/a | pass |
---|
1638 | n/a | else: |
---|
1639 | n/a | return close_handler() |
---|
1640 | n/a | finally: |
---|
1641 | n/a | # get rid of circular references |
---|
1642 | n/a | del self.parser, self._parser |
---|
1643 | n/a | del self.target, self._target |
---|
1644 | n/a | |
---|
1645 | n/a | |
---|
1646 | n/a | # Import the C accelerators |
---|
1647 | n/a | try: |
---|
1648 | n/a | # Element is going to be shadowed by the C implementation. We need to keep |
---|
1649 | n/a | # the Python version of it accessible for some "creative" by external code |
---|
1650 | n/a | # (see tests) |
---|
1651 | n/a | _Element_Py = Element |
---|
1652 | n/a | |
---|
1653 | n/a | # Element, SubElement, ParseError, TreeBuilder, XMLParser |
---|
1654 | n/a | from _elementtree import * |
---|
1655 | n/a | except ImportError: |
---|
1656 | n/a | pass |
---|