annotate Lib/xml/etree/ElementTree.py @ 79675:c0a2294de787 2.7

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