annotate emeraldtree/tree.py @ 66:048d2f8de762

Tree - Make Node writable
author Bastian Blank <bblank@thinkmo.de>
date Sun, 30 May 2010 17:28:19 +0200
parents d3fcc3556413
children a271e50bea43
rev   line source
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1 # Copyright (c) 1999-2007 by Fredrik Lundh
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
2 # 2008 Bastian Blank <bblank@thinkmo.de>
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
4 # By obtaining, using, and/or copying this software and/or its
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
5 # associated documentation, you agree that you have read, understood,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 # and will comply with the following terms and conditions:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
7 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
8 # Permission to use, copy, modify, and distribute this software and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
9 # its associated documentation for any purpose and without fee is
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
10 # hereby granted, provided that the above copyright notice appears in
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
11 # all copies, and that both that copyright notice and this permission
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
12 # notice appear in supporting documentation, and that the name of
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
13 # Secret Labs AB or the author not be used in advertising or publicity
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
14 # pertaining to distribution of the software without specific, written
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
15 # prior permission.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
16 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
17 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
18 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
19 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
20 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
21 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
22 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
23 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
24 # OF THIS SOFTWARE.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
25
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
26 from __future__ import generators
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
27
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
28 __all__ = [
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
29 # public symbols
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
30 "Comment",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
31 "dump",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
32 "Element", "ElementTree",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
33 "fromstring", "fromstringlist",
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
34 "iterparse",
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
35 "Node",
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
36 "parse", "ParseError",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
37 "PI", "ProcessingInstruction",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
38 "QName",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
39 "SubElement",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
40 "tostring", "tostringlist",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
41 "TreeBuilder",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
42 "XML",
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
43 "XMLParser", "XMLWriter",
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
44 ]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
45
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
46 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
47 # The <b>Element</b> type is a flexible container object, designed to
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
48 # store hierarchical data structures in memory. The type can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
49 # described as a cross between a list and a dictionary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
50 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
51 # Each element has a number of properties associated with it:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
52 # <ul>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
53 # <li>a <i>tag</i>. This is a string identifying what kind of data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
54 # this element represents (the element type, in other words).</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
55 # <li>a number of <i>attributes</i>, stored in a Python dictionary.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
56 # <li>a <i>text</i> string.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
57 # <li>an optional <i>tail</i> string.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
58 # <li>a number of <i>child elements</i>, stored in a Python sequence</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
59 # </ul>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
60 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
61 # To create an element instance, use the {@link #Element} constructor
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
62 # or the {@link #SubElement} factory function.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
63 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
64 # The {@link #ElementTree} class can be used to wrap an element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
65 # structure, and convert it from and to XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
66 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
67
14
3d7555e0be1b Remove several backward compatibility entries.
Bastian Blank <bblank@thinkmo.de>
parents: 13
diff changeset
68 import ElementPath
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
69
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
70 class ParseError(SyntaxError):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
71 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
72
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
73 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
74
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
75 class Node(object):
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
76 """
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
77 Node class.
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
78 """
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
79
66
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
80 def write(self, write, encoding=None, namespaces={}, method=None):
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
81 if not method or method == "xml":
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
82 Writer = XMLWriter
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
83 elif method == "html":
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
84 Writer = HTMLWriter
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
85 else:
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
86 Writer = TextWriter
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
87
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
88 Writer(encoding, namespaces).write(write, self)
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
89
048d2f8de762 Tree - Make Node writable
Bastian Blank <bblank@thinkmo.de>
parents: 65
diff changeset
90
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
91 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
92 # Element class. This class defines the Element interface, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
93 # provides a reference implementation of this interface.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
94 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
95 # The element name, attribute names, and attribute values can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
96 # either 8-bit ASCII strings or Unicode strings.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
97 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
98 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
99 # @param attrib An optional dictionary, containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
100 # @param **extra Additional attributes, given as keyword arguments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
101 # @see Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
102 # @see SubElement
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
103 # @see Comment
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
104 # @see ProcessingInstruction
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
105
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
106 class Element(Node):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
107 # <tag attrib>text<child/>...</tag>tail
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
108
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
109 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
110 # (Attribute) Element tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
111
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
112 tag = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
113
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
114 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
115 # (Attribute) Element attribute dictionary. Where possible, use
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
116 # {@link #Element.get},
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
117 # {@link #Element.set},
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
118 # {@link #Element.keys}, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
119 # {@link #Element.items} to access
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
120 # element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
121
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
122 attrib = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
123
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
124 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
125 # (Attribute) Text before first subelement. This is either a
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
126 # string or the value None, if there was no text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
127
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
128 @property
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
129 def text(self):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
130 if len(self) and isinstance(self[0], basestring):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
131 return self[0]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
132
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
133 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
134 # (Attribute) Text after this element's end tag, but before the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
135 # next sibling element's start tag. This is either a string or
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
136 # the value None, if there was no text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
137
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
138 @property
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
139 def tail(self):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
140 raise RuntimeError('The tail argument is not supported')
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
141
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
142 def __init__(self, tag, attrib=None, children=(), **extra):
50
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
143 if attrib:
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
144 if isinstance(attrib, dict):
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
145 attrib = attrib.copy()
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
146 else:
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
147 raise TypeError('attrib')
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
148 else:
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
149 attrib = {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
150 attrib.update(extra)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
151 self.tag = tag
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
152 self.attrib = attrib
46
c228aba7258c ElementTree - Also check nodes in children attribute
Bastian Blank <bblank@thinkmo.de>
parents: 45
diff changeset
153 self._children = [self._check_node(i) for i in children]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
154
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
155 def __repr__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
156 return "<Element %s at %x>" % (repr(self.tag), id(self))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
157
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
158 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
159 # Returns the number of subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
160 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
161 # @return The number of subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
162
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
163 def __len__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
164 return len(self._children)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
165
20
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
166 def __nonzero__(self):
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
167 return True
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
168
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
169 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
170 # Returns the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
171 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
172 # @param index What subelement to return.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
173 # @return The given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
174 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
175
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
176 def __getitem__(self, index):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
177 return self._children.__getitem__(index)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
178
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
179 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
180 # Replaces the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
181 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
182 # @param index What subelement to replace.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
183 # @param element The new element value.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
184 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
185 # @exception AssertionError If element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
186
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
187 def __setitem__(self, index, element):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
188 if isinstance(index, slice):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
189 element = [self._check_node(i) for i in element]
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
190 else:
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
191 element = self._check_node(element)
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
192 self._children.__setitem__(index, element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
193
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
194 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
195 # Deletes the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
196 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
197 # @param index What subelement to delete.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
198 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
199
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
200 def __delitem__(self, index):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
201 self._children.__delitem__(index)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
202
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
203 @staticmethod
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
204 def _check_node(node):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
205 if isinstance(node, (Node, unicode)):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
206 return node
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
207 if isinstance(node, str):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
208 return unicode(node)
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
209 raise TypeError
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
210
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
211 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
212 # Adds a subelement to the end of this element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
213 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
214 # @param element The element to add.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
215 # @exception AssertionError If a sequence member is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
216
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
217 def append(self, element):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
218 element = self._check_node(element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
219 self._children.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
220
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
221 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
222 # Appends subelements from a sequence.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
223 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
224 # @param elements A sequence object with zero or more elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
225 # @exception AssertionError If a subelement is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
226 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
227
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
228 def extend(self, elements):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
229 elements = [self._check_node(i) for i in elements]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
230 self._children.extend(elements)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
231
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
232 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
233 # Inserts a subelement at the given position in this element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
234 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
235 # @param index Where to insert the new subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
236 # @exception AssertionError If the element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
237
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
238 def insert(self, index, element):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
239 element = self._check_node(element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
240 self._children.insert(index, element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
241
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
242 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
243 # Removes a matching subelement. Unlike the <b>find</b> methods,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
244 # this method compares elements based on identity, not on tag
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
245 # value or contents.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
246 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
247 # @param element What element to remove.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
248 # @exception ValueError If a matching element could not be found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
249 # @exception AssertionError If the element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
250
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
251 def remove(self, element):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
252 self._children.remove(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
253
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
254 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
255 # Finds the first matching subelement, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
256 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
257 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
258 # @return The first matching element, or None if no element was found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
259 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
260
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
261 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
262 return ElementPath.find(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
263
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
264 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
265 # Finds text for the first matching subelement, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
266 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
267 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
268 # @param default What to return if the element was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
269 # @return The text content of the first matching element, or the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
270 # default value no element was found. Note that if the element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
271 # has is found, but has no text content, this method returns an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
272 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
273 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
274
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
275 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
276 return ElementPath.findtext(self, path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
277
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
278 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
279 # Finds all matching subelements, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
280 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
281 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
282 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
283 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
284 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
285
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
286 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
287 return ElementPath.findall(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
288
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
289 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
290 # Resets an element. This function removes all subelements, clears
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
291 # all attributes, and sets the text and tail attributes to None.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
292
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
293 def clear(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
294 self.attrib.clear()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
295 self._children = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
296
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
297 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
298 # Gets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
299 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
300 # @param key What attribute to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
301 # @param default What to return if the attribute was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
302 # @return The attribute value, or the default value, if the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
303 # attribute was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
304
33
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
305 def get(self, key, default=None):
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
306 return self.attrib.get(key, default)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
307
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
308 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
309 # Sets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
310 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
311 # @param key What attribute to set.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
312 # @param value The attribute value.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
313
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
314 def set(self, key, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
315 self.attrib[key] = value
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
316
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
317 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
318 # Gets a list of attribute names. The names are returned in an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
319 # arbitrary order (just like for an ordinary Python dictionary).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
320 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
321 # @return A list of element attribute names.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
322 # @defreturn list of strings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
323
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
324 def keys(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
325 return self.attrib.keys()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
326
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
327 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
328 # Gets element attributes, as a sequence. The attributes are
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
329 # returned in an arbitrary order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
330 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
331 # @return A list of (name, value) tuples for all attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
332 # @defreturn list of (string, string) tuples
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
333
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
334 def items(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
335 return self.attrib.items()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
336
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
337 def __iter__(self):
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
338 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
339 Creates a element iterator. The iterator loops over all children.
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
340 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
341 return self._children.__iter__()
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
342
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
343 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
344 # Creates a tree iterator. The iterator loops over this element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
345 # and all subelements, in document order, and returns all elements
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
346 # with a matching tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
347 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
348 # If the tree structure is modified during iteration, new or removed
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
349 # elements may or may not be included. To get a stable set, use the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
350 # list() function on the iterator, and loop over the resulting list.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
351 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
352 # @param tag What tags to look for (default is to return all elements).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
353 # @return An iterator containing all the matching elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
354 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
355
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
356 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
357 if tag == "*":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
358 tag = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
359 if tag is None or self.tag == tag:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
360 yield self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
361 for e in self._children:
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
362 if isinstance(e, Element):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
363 for e in e.iter(tag):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
364 yield e
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
365 else:
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
366 yield e
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
367
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
368 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
369 # Creates a text iterator. The iterator loops over this element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
370 # and all subelements, in document order, and returns all inner
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
371 # text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
372 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
373 # @return An iterator containing all inner text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
374 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
375
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
376 def itertext(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
377 for e in self:
9
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
378 if isinstance(e, Element):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
379 for s in e.itertext():
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
380 yield s
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
381 elif isinstance(e, basestring):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
382 yield e
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
383
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
384
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
385 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
386 # Subelement factory. This function creates an element instance, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
387 # appends it to an existing element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
388 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
389 # The element name, attribute names, and attribute values can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
390 # either 8-bit ASCII strings or Unicode strings.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
391 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
392 # @param parent The parent element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
393 # @param tag The subelement name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
394 # @param attrib An optional dictionary, containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
395 # @param **extra Additional attributes, given as keyword arguments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
396 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
397 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
398
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
399 def SubElement(parent, tag, attrib=None, **extra):
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
400 attrib = attrib and attrib.copy() or {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
401 attrib.update(extra)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
402 element = parent.makeelement(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
403 parent.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
404 return element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
405
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
406 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
407 # Comment element factory. This factory function creates a special
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
408 # element that will be serialized as an XML comment by the standard
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
409 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
410 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
411 # The comment string can be either an 8-bit ASCII string or a Unicode
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
412 # string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
413 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
414 # @param text A string containing the comment string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
415 # @return An element instance, representing a comment.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
416 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
417
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
418 class Comment(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
419 def __init__(self, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
420 self.text = text
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
421
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
422 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
423 # PI element factory. This factory function creates a special element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
424 # that will be serialized as an XML processing instruction by the standard
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
425 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
426 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
427 # @param target A string containing the PI target.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
428 # @param text A string containing the PI contents, if any.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
429 # @return An element instance, representing a PI.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
430 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
431
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
432 class ProcessingInstruction(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
433 def __init__(self, target, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
434 self.target, self.text = target, text
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
435
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
436 PI = ProcessingInstruction
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
437
64
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
438 class QName(unicode):
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
439 """
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
440 QName wrapper. This can be used to wrap a QName attribute value, in
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
441 order to get proper namespace handling on output.
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
442
64
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
443 @ivar name: local part of the QName
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
444 @type name: unicode
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
445 @ivar uri: URI part of the QName
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
446 @type uri: unicode
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
447 """
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
448 __slots__ = 'name', 'uri'
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
449
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
450 def __new__(cls, name, uri=None):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
451 text = name = unicode(name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
452
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
453 if name[0] == '{':
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
454 if uri is not None:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
455 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
456 i = name.find('}')
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
457 if i == -1:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
458 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
459 uri = name[1:i]
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
460 name = name[i + 1:]
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
461
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
462 if uri is not None:
50
d3767204a325 ElementTree - Bail out if attrib argument is no dict, force uri argument to be unicode
Bastian Blank <bblank@thinkmo.de>
parents: 48
diff changeset
463 uri = unicode(uri)
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
464 text = '{' + uri + '}' + name
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
465
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
466 ret = unicode.__new__(cls, text)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
467 unicode.__setattr__(ret, 'name', name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
468 unicode.__setattr__(ret, 'uri', uri)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
469
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
470 return ret
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
471
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
472 def __getnewargs__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
473 return self.name, self.uri
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
474
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
475 def __getstate__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
476 pass
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
477
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
478 def __repr__(self):
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
479 return '%s(%r, %r)' % (self.__class__.__name__, self.name, self.uri)
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
480
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
481 def __setattr__(self, key, value):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
482 raise AttributeError('read-only')
62
2461a04b352e QName - Remove mostly useless copy function and text property
Bastian Blank <bblank@thinkmo.de>
parents: 56
diff changeset
483 __delattr__ = __setattr__
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
484
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
485 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
486
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
487 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
488 # ElementTree wrapper class. This class represents an entire element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
489 # hierarchy, and adds some extra support for serialization to and from
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
490 # standard XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
491 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
492 # @param element Optional root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
493 # @keyparam file Optional file handle or file name. If given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
494 # tree is initialized with the contents of this XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
495
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
496 class ElementTree(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
497
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
498 def __init__(self, element=None, file=None):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
499 assert element is None or isinstance(element, Node)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
500 self._root = element # first node
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
501 if file:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
502 self.parse(file)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
503
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
504 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
505 # Gets the root element for this tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
506 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
507 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
508 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
509
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
510 def getroot(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
511 return self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
512
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
513 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
514 # Loads an external XML document into this element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
515 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
516 # @param source A file name or file object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
517 # @keyparam parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
518 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
519 # @return The document root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
520 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
521
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
522 def parse(self, source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
523 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
524 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
525 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
526 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
527 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
528 data = source.read(32768)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
529 if not data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
530 break
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
531 parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
532 self._root = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
533 return self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
534
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
535 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
536 # Creates a tree iterator for the root element. The iterator loops
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
537 # over all elements in this tree, in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
538 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
539 # @param tag What tags to look for (default is to return all elements)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
540 # @return An iterator.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
541 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
542
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
543 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
544 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
545 return self._root.iter(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
546
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
547 getiterator = iter
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
548
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
549 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
550 # Finds the first toplevel element with given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
551 # Same as getroot().find(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
552 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
553 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
554 # @return The first matching element, or None if no element was found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
555 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
556
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
557 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
558 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
559 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
560 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
561 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
562 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
563 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
564 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
565 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
566 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
567 return self._root.find(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
568
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
569 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
570 # Finds the element text for the first toplevel element with given
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
571 # tag. Same as getroot().findtext(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
572 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
573 # @param path What toplevel element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
574 # @param default What to return if the element was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
575 # @return The text content of the first matching element, or the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
576 # default value no element was found. Note that if the element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
577 # has is found, but has no text content, this method returns an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
578 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
579 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
580
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
581 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
582 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
583 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
584 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
585 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
586 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
587 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
588 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
589 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
590 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
591 return self._root.findtext(path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
592
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
593 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
594 # Finds all toplevel elements with the given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
595 # Same as getroot().findall(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
596 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
597 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
598 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
599 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
600 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
601
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
602 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
603 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
604 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
605 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
606 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
607 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
608 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
609 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
610 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
611 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
612 return self._root.findall(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
613
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
614 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
615 # Writes the element tree to a file, as XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
616 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
617 # @param file A file name, or a file object opened for writing.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
618 # @keyparam encoding Optional output encoding (default is US-ASCII).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
619 # @keyparam method Optional output method ("xml" or "html"; default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
620 # is "xml".
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
621 # @keyparam xml_declaration Controls if an XML declaration should
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
622 # be added to the file. Use False for never, True for always,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
623 # None for only if not US-ASCII or UTF-8. None is default.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
624
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
625 def write(self, file,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
626 # keyword arguments
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
627 encoding="us-ascii",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
628 xml_declaration=None,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
629 default_namespace=None,
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
630 method=None,
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
631 namespaces={}):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
632 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
633 if not hasattr(file, "write"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
634 file = open(file, "wb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
635 write = file.write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
636 if not encoding:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
637 encoding = "us-ascii"
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
638 if not method or method == "xml":
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
639 Writer = XMLWriter
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
640 elif method == "html":
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
641 Writer = HTMLWriter
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
642 else:
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
643 Writer = TextWriter
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
644
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
645 if default_namespace:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
646 namespaces = namespaces.copy()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
647 namespaces[default_namespace] = ''
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
648
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
649 Writer(encoding, namespaces).write(write, self._root)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
650
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
651 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
652 # serialization support
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
653
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
654 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
655
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
656 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
657 # Generates a string representation of an XML element, including all
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
658 # subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
659 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
660 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
661 # @return An encoded string containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
662 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
663
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
664 def tostring(element, encoding=None, method=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
665 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
666 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
667 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
668 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
669 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
670 ElementTree(element).write(file, encoding, method=method)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
671 return "".join(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
672
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
673 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
674 # Generates a string representation of an XML element, including all
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
675 # subelements. The string is returned as a sequence of string fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
676 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
677 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
678 # @return A sequence object containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
679 # @defreturn sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
680 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
681
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
682 def tostringlist(element, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
683 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
684 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
685 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
686 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
687 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
688 ElementTree(element).write(file, encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
689 # FIXME: merge small fragments into larger parts
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
690 return data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
691
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
692 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
693 # Writes an element tree or element structure to sys.stdout. This
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
694 # function should be used for debugging only.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
695 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
696 # The exact output format is implementation dependent. In this
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
697 # version, it's written as an ordinary XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
698 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
699 # @param elem An element tree or an individual element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
700
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
701 def dump(elem):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
702 # debugging
13
99619772e57c Move imports.
Bastian Blank <bblank@thinkmo.de>
parents: 12
diff changeset
703 import sys
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
704 if not isinstance(elem, ElementTree):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
705 elem = ElementTree(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
706 elem.write(sys.stdout)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
707 tail = elem.getroot().tail
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
708 if not tail or tail[-1] != "\n":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
709 sys.stdout.write("\n")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
710
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
711 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
712 # parsing
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
713
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
714 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
715 # Parses an XML document into an element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
716 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
717 # @param source A filename or file object containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
718 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
719 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
720 # @return An ElementTree instance
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
721
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
722 def parse(source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
723 tree = ElementTree()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
724 tree.parse(source, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
725 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
726
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
727 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
728 # Parses an XML document into an element tree incrementally, and reports
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
729 # what's going on to the user.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
730 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
731 # @param source A filename or file object containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
732 # @param events A list of events to report back. If omitted, only "end"
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
733 # events are reported.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
734 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
735 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
736 # @return A (event, elem) iterator.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
737
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
738 def iterparse(source, events=None, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
739 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
740 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
741 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
742 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
743 return _IterParseIterator(source, events, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
744
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
745 class _IterParseIterator(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
746
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
747 def __init__(self, source, events, parser):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
748 self._file = source
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
749 self._events = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
750 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
751 self.root = self._root = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
752 self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
753 # wire up the parser for event reporting
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
754 parser = self._parser._parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
755 append = self._events.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
756 if events is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
757 events = ["end"]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
758 for event in events:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
759 if event == "start":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
760 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
761 parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
762 parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
763 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
764 start=self._parser._start_list):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
765 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
766 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
767 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
768 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
769 start=self._parser._start):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
770 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
771 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
772 elif event == "end":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
773 def handler(tag, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
774 end=self._parser._end):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
775 append((event, end(tag)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
776 parser.EndElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
777 elif event == "start-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
778 def handler(prefix, uri, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
779 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
780 uri = uri.encode("ascii")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
781 except UnicodeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
782 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
783 append((event, (prefix or "", uri)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
784 parser.StartNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
785 elif event == "end-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
786 def handler(prefix, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
787 append((event, None))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
788 parser.EndNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
789
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
790 def next(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
791 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
792 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
793 item = self._events[self._index]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
794 except IndexError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
795 if self._parser is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
796 self.root = self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
797 raise StopIteration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
798 # load event buffer
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
799 del self._events[:]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
800 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
801 data = self._file.read(16384)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
802 if data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
803 self._parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
804 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
805 self._root = self._parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
806 self._parser = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
807 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
808 self._index = self._index + 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
809 return item
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
810
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
811 def __iter__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
812 return self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
813
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
814 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
815 # Parses an XML document from a string constant. This function can
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
816 # be used to embed "XML literals" in Python code.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
817 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
818 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
819 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
820 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
821 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
822 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
823
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
824 def XML(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
825 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
826 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
827 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
828 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
829
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
830 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
831 # Parses an XML document from a string constant, and also returns
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
832 # a dictionary which maps from element id:s to elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
833 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
834 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
835 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
836 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
837 # @return A tuple containing an Element instance and a dictionary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
838 # @defreturn (Element, dictionary)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
839
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
840 def XMLID(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
841 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
842 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
843 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
844 tree = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
845 ids = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
846 for elem in tree.getiterator():
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
847 id = elem.get("id")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
848 if id:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
849 ids[id] = elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
850 return tree, ids
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
851
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
852 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
853 # Parses an XML document from a string constant. Same as {@link #XML}.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
854 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
855 # @def fromstring(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
856 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
857 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
858 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
859
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
860 fromstring = XML
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
861
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
862 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
863 # Parses an XML document from a sequence of string fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
864 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
865 # @param sequence A list or other sequence containing XML data fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
866 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
867 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
868 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
869 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
870 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
871
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
872 def fromstringlist(sequence, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
873 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
874 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
875 for text in sequence:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
876 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
877 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
878
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
879 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
880
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
881 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
882 # Generic element structure builder. This builder converts a sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
883 # of {@link #TreeBuilder.start}, {@link #TreeBuilder.data}, and {@link
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
884 # #TreeBuilder.end} method calls to a well-formed element structure.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
885 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
886 # You can use this class to build an element structure using a custom XML
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
887 # parser, or a parser for some other XML-like format.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
888 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
889 # @param element_factory Optional element factory. This factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
890 # is called to create new Element instances, as necessary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
891
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
892 class TreeBuilder(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
893
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
894 def __init__(self, element_factory=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
895 self._data = [] # data collector
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
896 self._elem = [] # element stack
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
897 self._last = None # last element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
898 if element_factory is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
899 element_factory = Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
900 self._factory = element_factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
901
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
902 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
903 # Flushes the builder buffers, and returns the toplevel document
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
904 # element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
905 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
906 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
907 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
908
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
909 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
910 assert len(self._elem) == 0, "missing end tags"
21
7b33b90de8be some minor coding style cleanups
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 20
diff changeset
911 assert self._last is not None, "missing toplevel element"
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
912 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
913
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
914 def _flush(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
915 if self._data:
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
916 text = "".join(self._data)
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
917 self._elem[-1].append(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
918 self._data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
919
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
920 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
921 # Adds text to the current element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
922 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
923 # @param data A string. This should be either an 8-bit string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
924 # containing ASCII text, or a Unicode string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
925
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
926 def data(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
927 self._data.append(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
928
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
929 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
930 # Opens a new element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
931 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
932 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
933 # @param attrib A dictionary containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
934 # @return The opened element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
935 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
936
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
937 def start(self, tag, attrs):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
938 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
939 self._last = elem = self._factory(tag, attrs)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
940 if self._elem:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
941 self._elem[-1].append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
942 self._elem.append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
943 return elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
944
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
945 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
946 # Closes the current element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
947 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
948 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
949 # @return The closed element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
950 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
951
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
952 def end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
953 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
954 self._last = self._elem.pop()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
955 assert self._last.tag == tag,\
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
956 "end tag mismatch (expected %s, got %s)" % (
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
957 self._last.tag, tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
958 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
959
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
960 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
961 # Element structure builder for XML source data, based on the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
962 # <b>expat</b> parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
963 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
964 # @keyparam target Target object. If omitted, the builder uses an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
965 # instance of the standard {@link #TreeBuilder} class.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
966 # @keyparam html Predefine HTML entities. This flag is not supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
967 # by the current implementation.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
968 # @keyparam encoding Optional encoding. If given, the value overrides
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
969 # the encoding specified in the XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
970 # @see #ElementTree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
971 # @see #TreeBuilder
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
972
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
973 class XMLParser(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
974
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
975 def __init__(self, html=0, target=None, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
976 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
977 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
978 except ImportError:
47
e647f30cc08e remove backwards compat code for python < 2.4
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 46
diff changeset
979 raise ImportError(
e647f30cc08e remove backwards compat code for python < 2.4
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 46
diff changeset
980 "No module named expat; use SimpleXMLTreeBuilder instead"
e647f30cc08e remove backwards compat code for python < 2.4
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 46
diff changeset
981 )
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
982 parser = expat.ParserCreate(encoding, "}")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
983 if target is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
984 target = TreeBuilder()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
985 # underscored names are provided for compatibility only
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
986 self.parser = self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
987 self.target = self._target = target
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
988 self._error = expat.error
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
989 self._names = {} # name memo cache
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
990 # callbacks
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
991 parser.DefaultHandlerExpand = self._default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
992 parser.StartElementHandler = self._start
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
993 parser.EndElementHandler = self._end
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
994 parser.CharacterDataHandler = self._data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
995 # let expat do the buffering, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
996 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
997 self._parser.buffer_text = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
998 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
999 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1000 # use new-style attribute handling, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1001 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1002 self._parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1003 self._parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1004 parser.StartElementHandler = self._start_list
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1005 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1006 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1007 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1008 self.entity = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1009 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1010 self.version = "Expat %d.%d.%d" % expat.version_info
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1011 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1012 pass # unknown
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1013
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1014 def _raiseerror(self, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1015 err = ParseError(value)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1016 err.code = value.code
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1017 err.position = value.lineno, value.offset
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1018 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1019
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1020 def _fixname(self, key):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1021 # expand qname, and convert name string to ascii, if possible
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1022 if key in self._names:
62
2461a04b352e QName - Remove mostly useless copy function and text property
Bastian Blank <bblank@thinkmo.de>
parents: 56
diff changeset
1023 return self._names[key]
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1024 if '}' in key:
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1025 uri, name = key.split('}', 1)
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1026 name = QName(name, uri)
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1027 else:
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1028 name = QName(key)
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1029 self._names[key] = name
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1030 return name
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1031
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1032 def _start(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1033 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1034 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1035 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1036 for key, value in attrib_in.items():
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1037 attrib[fixname(key)] = value
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1038 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1039
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1040 def _start_list(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1041 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1042 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1043 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1044 if attrib_in:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1045 for i in range(0, len(attrib_in), 2):
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1046 attrib[fixname(attrib_in[i])] = attrib_in[i+1]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1047 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1048
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1049 def _data(self, text):
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1050 return self.target.data(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1051
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1052 def _end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1053 return self.target.end(self._fixname(tag))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1054
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1055 def _default(self, text):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1056 prefix = text[:1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1057 if prefix == "&":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1058 # deal with undefined entities
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1059 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1060 self.target.data(self.entity[text[1:-1]])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1061 except KeyError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1062 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1063 err = expat.error(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1064 "undefined entity %s: line %d, column %d" %
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1065 (text, self._parser.ErrorLineNumber,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1066 self._parser.ErrorColumnNumber)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1067 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1068 err.code = 11 # XML_ERROR_UNDEFINED_ENTITY
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1069 err.lineno = self._parser.ErrorLineNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1070 err.offset = self._parser.ErrorColumnNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1071 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1072 elif prefix == "<" and text[:9] == "<!DOCTYPE":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1073 self._doctype = [] # inside a doctype declaration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1074 elif self._doctype is not None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1075 # parse doctype contents
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1076 if prefix == ">":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1077 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1078 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1079 text = text.strip()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1080 if not text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1081 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1082 self._doctype.append(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1083 n = len(self._doctype)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1084 if n > 2:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1085 type = self._doctype[1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1086 if type == "PUBLIC" and n == 4:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1087 name, type, pubid, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1088 elif type == "SYSTEM" and n == 3:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1089 name, type, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1090 pubid = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1091 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1092 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1093 if pubid:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1094 pubid = pubid[1:-1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1095 if hasattr(self.target, "doctype"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1096 self.target.doctype(name, pubid, system[1:-1])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1097 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1098
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1099 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1100 # Feeds data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1101 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1102 # @param data Encoded data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1103
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1104 def feed(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1105 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1106 self._parser.Parse(data, 0)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1107 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1108 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1109
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1110 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1111 # Finishes feeding data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1112 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1113 # @return An element structure.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1114 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1115
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1116 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1117 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1118 self._parser.Parse("", 1) # end of data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1119 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1120 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1121 tree = self.target.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1122 del self.target, self._parser # get rid of circular references
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1123 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1124
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1125 class BaseWriter(object):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1126 def __init__(self, encoding=None, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1127 self.encoding = encoding
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1128 self.namespaces = namespaces
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1129
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1130 def _encode(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1131 if self.encoding:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1132 return text.encode(self.encoding, "xmlcharrefreplace")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1133 return text
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1134
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1135 def _escape_cdata(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1136 # escape character data
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1137 # it's worth avoiding do-nothing calls for strings that are
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1138 # shorter than 500 character, or so. assume that's, by far,
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1139 # the most common case in most applications.
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1140 if "&" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1141 text = text.replace("&", "&amp;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1142 if "<" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1143 text = text.replace("<", "&lt;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1144 if ">" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1145 text = text.replace(">", "&gt;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1146 return self._encode(text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1147
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1148 def _escape_attrib(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1149 # escape attribute value
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1150 if "\"" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1151 text = text.replace("\"", "&quot;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1152 if "\n" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1153 text = text.replace("\n", "&#10;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1154 return self._escape_cdata(text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1155
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1156 def _namespaces(self, elem):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1157 # identify namespaces used in this tree
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1158
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1159 # maps qnames to *encoded* prefix:local names
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1160 qnames = {None: None}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1161
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1162 # maps uri:s to prefixes
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1163 candidate_namespaces = self._namespace_map.copy()
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1164 candidate_namespaces = {}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1165 candidate_namespaces.update(self.namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1166 used_namespaces = {}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1167
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1168 def add_qname(qname):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1169 if qname in qnames:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1170 return
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1171
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1172 # calculate serialized qname representation
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1173 try:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1174 if qname.uri is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1175 uri = qname.uri
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1176 prefix = used_namespaces.get(uri, None)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1177 if prefix is None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1178 prefix = candidate_namespaces.get(uri, None)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1179 if prefix is None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1180 prefix = "ns%d" % len(used_namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1181 if prefix != "xml":
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1182 used_namespaces[uri] = prefix
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1183 if prefix:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1184 qnames[qname] = "%s:%s" % (prefix, qname.name)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1185 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1186 qnames[qname] = qname.name
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1187 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1188 # XXX: What happens with undefined namespace?
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1189 qnames[qname] = qname.name
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1190 except TypeError:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1191 self._raise_serialization_error(qname)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1192
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1193 # populate qname and namespaces table
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1194 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1195 for elem in elem.iter():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1196 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1197 tag = elem.tag
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1198 if isinstance(tag, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1199 add_qname(tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1200 elif isinstance(tag, basestring):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1201 add_qname(QName(tag))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1202 elif tag is not None:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1203 self._raise_serialization_error(tag)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1204
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1205 for key in elem.keys():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1206 if isinstance(key, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1207 add_qname(key)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1208 elif isinstance(key, basestring):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1209 add_qname(QName(key))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1210 elif key is not None:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1211 self._raise_serialization_error(key)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1212
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1213 return qnames, used_namespaces
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1214
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1215 @staticmethod
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1216 def _raise_serialization_error(text):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1217 raise TypeError(
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1218 "cannot serialize %r (type %s)" % (text, type(text).__name__)
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1219 )
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1220
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1221 ##
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1222 # Registers a namespace prefix. The registry is global, and any
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1223 # existing mapping for either the given prefix or the namespace URI
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1224 # will be removed.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1225 #
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1226 # @param prefix Namespace prefix.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1227 # @param uri Namespace uri. Tags and attributes in this namespace
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1228 # will be serialized with the given prefix, if at all possible.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1229 # @raise ValueError If the prefix is reserved, or is otherwise
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1230 # invalid.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1231
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1232 @classmethod
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1233 def register_namespace(cls, prefix, uri):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1234 import re
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1235 if re.match("ns\d+$", prefix):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1236 raise ValueError("Prefix format reserved for internal use")
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1237 for k, v in cls._namespace_map.items():
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1238 if k == uri or v == prefix:
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1239 del _namespace_map[k]
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1240 cls._namespace_map[uri] = prefix
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1241
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1242 _namespace_map = {
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1243 # "well-known" namespace prefixes
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1244 "http://www.w3.org/XML/1998/namespace": "xml",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1245 "http://www.w3.org/1999/xhtml": "html",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1246 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1247 "http://schemas.xmlsoap.org/wsdl/": "wsdl",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1248 # xml schema
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1249 "http://www.w3.org/2001/XMLSchema": "xs",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1250 "http://www.w3.org/2001/XMLSchema-instance": "xsi",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1251 # dublic core
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1252 "http://purl.org/dc/elements/1.1/": "dc",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1253 }
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1254
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1255 def serialize_start(self, write):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1256 pass
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1257
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1258 def write(self, write, element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1259 qnames, namespaces = self._namespaces(element)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1260 self.serialize_start(write)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1261 self.serialize(write, element, qnames, namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1262
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1263
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1264 class TextWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1265 def serialize(self, write, elem, qnames=None, namespaces=None):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1266 for part in elem.itertext():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1267 write(self._encode(part))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1268
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1269
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1270 class XMLWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1271 def serialize(self, write, elem, qnames, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1272 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1273 tag = qnames[elem.tag]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1274
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1275 if tag is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1276 write("<" + tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1277
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1278 if elem.attrib:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1279 items = elem.attrib.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1280 items.sort(key=lambda x: x[0])
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1281 for k, v in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1282 k = qnames[k]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1283 if isinstance(v, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1284 v = qnames[v]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1285 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1286 v = self._escape_attrib(unicode(v))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1287 write(' ' + k + '="' + v + '"')
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1288 if namespaces:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1289 items = namespaces.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1290 items.sort(key=lambda x: x[1]) # sort on prefix
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1291 for v, k in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1292 if k:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1293 k = ":" + k
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1294 write(" xmlns%s=\"%s\"" % (
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1295 self._encode(k),
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1296 self._escape_attrib(v)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1297 ))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1298 if len(elem):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1299 write(">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1300 for e in elem:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1301 self.serialize(write, e, qnames)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1302 write("</" + tag + ">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1303 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1304 write(" />")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1305
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1306 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1307 for e in elem:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1308 self.serialize(write, e, encoding, qnames)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1309
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1310 elif isinstance(elem, Comment):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1311 write("<!--%s-->" % self._escape_cdata(elem.text))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1312
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1313 elif isinstance(elem, ProcessingInstruction):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1314 text = self._escape_cdata(elem.target)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1315 if elem.text is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1316 text += ' ' + self._escape_cdata(elem.text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1317 write("<?%s?>" % text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1318
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1319 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1320 write(self._escape_cdata(unicode(elem)))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1321
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1322 def serialize_start(self, write):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1323 if self.encoding and self.encoding not in ("utf-8", "us-ascii"):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1324 write("<?xml version='1.0' encoding='%s'?>\n" % self.encoding)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1325
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1326
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1327 class HTMLWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1328 empty_elements = frozenset(("area", "base", "basefont", "br", "col", "frame", "hr",
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1329 "img", "input", "isindex", "link", "meta" "param"))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1330
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1331 def __init__(self, encoding=None, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1332 namespaces["http://www.w3.org/1999/xhtml"] = ''
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1333 super(HTMLWriter, self).__init__(encoding, namespaces)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1334
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1335 def serialize(self, write, elem, qnames, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1336 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1337 tag = qnames[elem.tag]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1338
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1339 if tag is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1340 write("<" + tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1341
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1342 if elem.attrib:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1343 items = elem.attrib.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1344 items.sort(key=lambda x: x[0])
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1345 for k, v in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1346 k = qnames[k]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1347 if isinstance(v, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1348 v = qnames[v]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1349 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1350 v = self._escape_attrib(unicode(v))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1351 # FIXME: handle boolean attributes
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1352 write(' ' + k + '="' + v + '"')
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1353 if namespaces:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1354 items = namespaces.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1355 items.sort(key=lambda x: x[1]) # sort on prefix
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1356 for v, k in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1357 if k:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1358 k = ":" + k
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1359 write(" xmlns%s=\"%s\"" % (
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1360 self._encode(k),
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1361 self._escape_attrib(v)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1362 ))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1363 write(">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1364
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1365 if tag.lower() in ('script', 'style'):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1366 write(self._encode(''.join(elem.itertext())))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1367 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1368 for e in elem:
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1369 self.serialize(write, e, qnames)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1370
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1371 if tag not in self.empty_elements:
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1372 write("</" + tag + ">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1373
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1374 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1375 for e in elem:
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1376 self.serialize(write, e, qnames)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1377
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1378 elif isinstance(elem, Comment):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1379 write("<!--%s-->" % self._escape_cdata(elem.text))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1380
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1381 elif isinstance(elem, ProcessingInstruction):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1382 text = self._escape_cdata(elem.target)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1383 if elem.text is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1384 text += ' ' + self._escape_cdata(elem.text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1385 write("<?%s?>" % text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1386
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1387 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1388 write(self._escape_cdata(elem))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1389