annotate emeraldtree/tree.py @ 75:d9ac9fe47760

Tree - Allow all objects in tree
author Bastian Blank <bblank@thinkmo.de>
date Tue, 08 Jun 2010 22:28:35 +0200
parents a271e50bea43
children 2175b8941596
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
75
d9ac9fe47760 Tree - Allow all objects in tree
Bastian Blank <bblank@thinkmo.de>
parents: 74
diff changeset
153 self._children = list(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 self._children.__setitem__(index, element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
189
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
190 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
191 # Deletes the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
192 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
193 # @param index What subelement to delete.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
194 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
195
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
196 def __delitem__(self, index):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
197 self._children.__delitem__(index)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
198
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 # Adds a subelement to the end of this element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
201 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
202 # @param element The element to add.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
203 # @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
204
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
205 def append(self, element):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
206 self._children.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
207
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
208 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
209 # Appends subelements from a sequence.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
210 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
211 # @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
212 # @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
213 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
214
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
215 def extend(self, elements):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
216 self._children.extend(elements)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
217
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
218 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
219 # 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
220 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
221 # @param index Where to insert the new subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
222 # @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
223
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
224 def insert(self, index, element):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
225 self._children.insert(index, element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
226
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 # 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
229 # 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
230 # value or contents.
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 # @param element What element to remove.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
233 # @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
234 # @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
235
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
236 def remove(self, element):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
237 self._children.remove(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
238
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
239 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
240 # 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
241 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
242 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
243 # @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
244 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
245
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
246 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
247 return ElementPath.find(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
248
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
249 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
250 # 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
251 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
252 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
253 # @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
254 # @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
255 # 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
256 # 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
257 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
258 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
259
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
260 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
261 return ElementPath.findtext(self, path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
262
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 # Finds all matching subelements, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
265 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
266 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
267 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
268 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
269 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
270
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
271 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
272 return ElementPath.findall(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
273
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 # Resets an element. This function removes all subelements, clears
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
276 # 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
277
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
278 def clear(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
279 self.attrib.clear()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
280 self._children = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
281
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
282 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
283 # Gets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
284 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
285 # @param key What attribute to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
286 # @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
287 # @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
288 # attribute was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
289
33
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
290 def get(self, key, default=None):
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
291 return self.attrib.get(key, default)
0
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
294 # Sets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
295 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
296 # @param key What attribute to set.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
297 # @param value The attribute value.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
298
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
299 def set(self, key, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
300 self.attrib[key] = value
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
301
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
302 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
303 # 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
304 # arbitrary order (just like for an ordinary Python dictionary).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
305 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
306 # @return A list of element attribute names.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
307 # @defreturn list of strings
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 def keys(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
310 return self.attrib.keys()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
311
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
312 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
313 # Gets element attributes, as a sequence. The attributes are
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
314 # returned in an arbitrary order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
315 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
316 # @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
317 # @defreturn list of (string, string) tuples
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
318
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
319 def items(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
320 return self.attrib.items()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
321
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
322 def __iter__(self):
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
323 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
324 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
325 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
326 return self._children.__iter__()
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
327
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
328 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
329 # 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
330 # 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
331 # with a matching tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
332 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
333 # 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
334 # 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
335 # 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
336 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
337 # @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
338 # @return An iterator containing all the matching elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
339 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
340
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
341 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
342 if tag == "*":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
343 tag = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
344 if tag is None or self.tag == tag:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
345 yield self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
346 for e in self._children:
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
347 if isinstance(e, Element):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
348 for e in e.iter(tag):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
349 yield e
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
350 else:
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
351 yield e
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
352
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
353 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
354 # 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
355 # 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
356 # text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
357 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
358 # @return An iterator containing all inner text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
359 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
360
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
361 def itertext(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
362 for e in self:
9
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
363 if isinstance(e, Element):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
364 for s in e.itertext():
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
365 yield s
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
366 elif isinstance(e, basestring):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
367 yield e
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
368
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
369
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
370 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
371 # Subelement factory. This function creates an element instance, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
372 # appends it to an existing element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
373 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
374 # 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
375 # either 8-bit ASCII strings or Unicode strings.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
376 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
377 # @param parent The parent element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
378 # @param tag The subelement name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
379 # @param attrib An optional dictionary, containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
380 # @param **extra Additional attributes, given as keyword arguments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
381 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
382 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
383
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
384 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
385 attrib = attrib and attrib.copy() or {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
386 attrib.update(extra)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
387 element = parent.makeelement(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
388 parent.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
389 return element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
390
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 # Comment element factory. This factory function creates a special
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
393 # 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
394 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
395 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
396 # 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
397 # string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
398 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
399 # @param text A string containing the comment string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
400 # @return An element instance, representing a comment.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
401 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
402
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
403 class Comment(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
404 def __init__(self, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
405 self.text = text
0
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
408 # 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
409 # 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
410 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
411 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
412 # @param target A string containing the PI target.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
413 # @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
414 # @return An element instance, representing a PI.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
415 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
416
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
417 class ProcessingInstruction(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
418 def __init__(self, target, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
419 self.target, self.text = target, text
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
420
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
421 PI = ProcessingInstruction
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
422
64
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
423 class QName(unicode):
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
424 """
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
425 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
426 order to get proper namespace handling on output.
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
427
64
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
428 @ivar name: local part of the QName
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
429 @type name: unicode
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
430 @ivar uri: URI part of the QName
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
431 @type uri: unicode
bd93ebba58ba QName - Fix documentation
Bastian Blank <bblank@thinkmo.de>
parents: 62
diff changeset
432 """
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
433 __slots__ = 'name', 'uri'
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
434
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
435 def __new__(cls, name, uri=None):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
436 text = name = unicode(name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
437
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
438 if name[0] == '{':
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
439 if uri is not None:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
440 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
441 i = name.find('}')
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
442 if i == -1:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
443 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
444 uri = name[1:i]
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
445 name = name[i + 1:]
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
446
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
447 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
448 uri = unicode(uri)
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
449 text = '{' + uri + '}' + name
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
450
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
451 ret = unicode.__new__(cls, text)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
452 unicode.__setattr__(ret, 'name', name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
453 unicode.__setattr__(ret, 'uri', uri)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
454
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
455 return ret
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
456
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
457 def __getnewargs__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
458 return self.name, self.uri
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
459
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
460 def __getstate__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
461 pass
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
462
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
463 def __repr__(self):
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
464 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
465
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
466 def __setattr__(self, key, value):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
467 raise AttributeError('read-only')
62
2461a04b352e QName - Remove mostly useless copy function and text property
Bastian Blank <bblank@thinkmo.de>
parents: 56
diff changeset
468 __delattr__ = __setattr__
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
469
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
470 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
471
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
472 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
473 # ElementTree wrapper class. This class represents an entire element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
474 # 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
475 # standard XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
476 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
477 # @param element Optional root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
478 # @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
479 # 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
480
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
481 class ElementTree(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
482
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
483 def __init__(self, element=None, file=None):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
484 assert element is None or isinstance(element, Node)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
485 self._root = element # first node
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
486 if file:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
487 self.parse(file)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
488
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
489 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
490 # Gets the root element for this tree.
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 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
493 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
494
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
495 def getroot(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
496 return self._root
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
499 # Loads an external XML document into this element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
500 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
501 # @param source A file name or file object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
502 # @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
503 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
504 # @return The document root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
505 # @defreturn Element
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 def parse(self, source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
508 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
509 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
510 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
511 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
512 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
513 data = source.read(32768)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
514 if not data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
515 break
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
516 parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
517 self._root = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
518 return self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
519
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
520 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
521 # 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
522 # over all elements in this tree, in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
523 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
524 # @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
525 # @return An iterator.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
526 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
527
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
528 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
529 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
530 return self._root.iter(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
531
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
532 getiterator = iter
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
533
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 # Finds the first toplevel element with given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
536 # Same as getroot().find(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
537 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
538 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
539 # @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
540 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
541
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
542 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
543 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
544 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
545 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
546 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
547 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
548 "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
549 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
550 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
551 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
552 return self._root.find(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
553
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
554 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
555 # 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
556 # tag. Same as getroot().findtext(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
557 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
558 # @param path What toplevel element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
559 # @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
560 # @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
561 # 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
562 # 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
563 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
564 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
565
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
566 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
567 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
568 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
569 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
570 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
571 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
572 "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
573 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
574 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
575 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
576 return self._root.findtext(path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
577
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
578 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
579 # Finds all toplevel elements with the given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
580 # Same as getroot().findall(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
581 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
582 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
583 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
584 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
585 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
586
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
587 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
588 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
589 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
590 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
591 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
592 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
593 "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
594 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
595 FutureWarning
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 return self._root.findall(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
598
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
599 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
600 # Writes the element tree to a file, as XML.
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 # @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
603 # @keyparam encoding Optional output encoding (default is US-ASCII).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
604 # @keyparam method Optional output method ("xml" or "html"; default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
605 # is "xml".
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
606 # @keyparam xml_declaration Controls if an XML declaration should
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
607 # 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
608 # 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
609
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
610 def write(self, file,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
611 # keyword arguments
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
612 encoding="us-ascii",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
613 xml_declaration=None,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
614 default_namespace=None,
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
615 method=None,
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
616 namespaces={}):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
617 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
618 if not hasattr(file, "write"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
619 file = open(file, "wb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
620 write = file.write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
621 if not encoding:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
622 encoding = "us-ascii"
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
623
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
624 if default_namespace:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
625 namespaces = namespaces.copy()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
626 namespaces[default_namespace] = ''
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
627
74
a271e50bea43 Tree - Redefine ElementTree write with Node write
Bastian Blank <bblank@thinkmo.de>
parents: 66
diff changeset
628 self._root.write(write, encoding=encoding, namespaces=namespaces, method=method)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
629
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
630 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
631 # serialization support
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
632
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
633 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
634
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
635 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
636 # 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
637 # subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
638 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
639 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
640 # @return An encoded string containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
641 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
642
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
643 def tostring(element, encoding=None, method=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
644 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
645 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
646 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
647 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
648 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
649 ElementTree(element).write(file, encoding, method=method)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
650 return "".join(data)
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
653 # 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
654 # 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
655 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
656 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
657 # @return A sequence object containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
658 # @defreturn sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
659 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
660
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
661 def tostringlist(element, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
662 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
663 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
664 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
665 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
666 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
667 ElementTree(element).write(file, encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
668 # FIXME: merge small fragments into larger parts
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
669 return data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
670
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
671 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
672 # 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
673 # function should be used for debugging only.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
674 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
675 # The exact output format is implementation dependent. In this
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
676 # version, it's written as an ordinary XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
677 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
678 # @param elem An element tree or an individual element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
679
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
680 def dump(elem):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
681 # debugging
13
99619772e57c Move imports.
Bastian Blank <bblank@thinkmo.de>
parents: 12
diff changeset
682 import sys
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
683 if not isinstance(elem, ElementTree):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
684 elem = ElementTree(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
685 elem.write(sys.stdout)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
686 tail = elem.getroot().tail
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
687 if not tail or tail[-1] != "\n":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
688 sys.stdout.write("\n")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
689
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
690 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
691 # parsing
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
694 # Parses an XML document into an element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
695 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
696 # @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
697 # @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
698 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
699 # @return An ElementTree instance
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 parse(source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
702 tree = ElementTree()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
703 tree.parse(source, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
704 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
705
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
706 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
707 # 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
708 # what's going on to the user.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
709 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
710 # @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
711 # @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
712 # events are reported.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
713 # @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
714 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
715 # @return A (event, elem) iterator.
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 def iterparse(source, events=None, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
718 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
719 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
720 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
721 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
722 return _IterParseIterator(source, events, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
723
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
724 class _IterParseIterator(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
725
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
726 def __init__(self, source, events, parser):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
727 self._file = source
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
728 self._events = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
729 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
730 self.root = self._root = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
731 self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
732 # wire up the parser for event reporting
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
733 parser = self._parser._parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
734 append = self._events.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
735 if events is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
736 events = ["end"]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
737 for event in events:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
738 if event == "start":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
739 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
740 parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
741 parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
742 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
743 start=self._parser._start_list):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
744 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
745 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
746 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
747 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
748 start=self._parser._start):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
749 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
750 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
751 elif event == "end":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
752 def handler(tag, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
753 end=self._parser._end):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
754 append((event, end(tag)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
755 parser.EndElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
756 elif event == "start-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
757 def handler(prefix, uri, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
758 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
759 uri = uri.encode("ascii")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
760 except UnicodeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
761 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
762 append((event, (prefix or "", uri)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
763 parser.StartNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
764 elif event == "end-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
765 def handler(prefix, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
766 append((event, None))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
767 parser.EndNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
768
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
769 def next(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
770 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
771 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
772 item = self._events[self._index]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
773 except IndexError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
774 if self._parser is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
775 self.root = self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
776 raise StopIteration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
777 # load event buffer
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
778 del self._events[:]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
779 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
780 data = self._file.read(16384)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
781 if data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
782 self._parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
783 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
784 self._root = self._parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
785 self._parser = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
786 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
787 self._index = self._index + 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
788 return item
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 __iter__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
791 return self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
792
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
793 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
794 # 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
795 # be used to embed "XML literals" in Python code.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
796 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
797 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
798 # @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
799 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
800 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
801 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
802
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
803 def XML(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
804 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
805 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
806 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
807 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
808
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
809 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
810 # 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
811 # 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
812 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
813 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
814 # @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
815 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
816 # @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
817 # @defreturn (Element, dictionary)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
818
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
819 def XMLID(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
820 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
821 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
822 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
823 tree = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
824 ids = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
825 for elem in tree.getiterator():
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
826 id = elem.get("id")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
827 if id:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
828 ids[id] = elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
829 return tree, ids
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
832 # 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
833 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
834 # @def fromstring(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
835 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
836 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
837 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
838
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
839 fromstring = XML
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
840
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
841 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
842 # 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
843 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
844 # @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
845 # @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
846 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
847 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
848 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
849 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
850
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
851 def fromstringlist(sequence, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
852 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
853 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
854 for text in sequence:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
855 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
856 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
857
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
858 # --------------------------------------------------------------------
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 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
861 # Generic element structure builder. This builder converts a sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
862 # of {@link #TreeBuilder.start}, {@link #TreeBuilder.data}, and {@link
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
863 # #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
864 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
865 # 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
866 # 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
867 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
868 # @param element_factory Optional element factory. This factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
869 # is called to create new Element instances, as necessary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
870
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
871 class TreeBuilder(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
872
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
873 def __init__(self, element_factory=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
874 self._data = [] # data collector
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
875 self._elem = [] # element stack
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
876 self._last = None # last element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
877 if element_factory is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
878 element_factory = Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
879 self._factory = element_factory
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 # Flushes the builder buffers, and returns the toplevel document
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
883 # element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
884 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
885 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
886 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
887
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
888 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
889 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
890 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
891 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
892
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
893 def _flush(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
894 if self._data:
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
895 text = "".join(self._data)
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
896 self._elem[-1].append(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
897 self._data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
898
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
899 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
900 # Adds text to the current element.
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 # @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
903 # containing ASCII text, or a Unicode string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
904
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
905 def data(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
906 self._data.append(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
907
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 # Opens a new element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
910 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
911 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
912 # @param attrib A dictionary containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
913 # @return The opened element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
914 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
915
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
916 def start(self, tag, attrs):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
917 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
918 self._last = elem = self._factory(tag, attrs)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
919 if self._elem:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
920 self._elem[-1].append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
921 self._elem.append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
922 return elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
923
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
924 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
925 # Closes the current element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
926 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
927 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
928 # @return The closed element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
929 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
930
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
931 def end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
932 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
933 self._last = self._elem.pop()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
934 assert self._last.tag == tag,\
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
935 "end tag mismatch (expected %s, got %s)" % (
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
936 self._last.tag, tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
937 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
938
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
939 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
940 # 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
941 # <b>expat</b> parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
942 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
943 # @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
944 # instance of the standard {@link #TreeBuilder} class.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
945 # @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
946 # by the current implementation.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
947 # @keyparam encoding Optional encoding. If given, the value overrides
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
948 # the encoding specified in the XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
949 # @see #ElementTree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
950 # @see #TreeBuilder
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 class XMLParser(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
953
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
954 def __init__(self, html=0, target=None, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
955 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
956 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
957 except ImportError:
47
e647f30cc08e remove backwards compat code for python < 2.4
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 46
diff changeset
958 raise ImportError(
e647f30cc08e remove backwards compat code for python < 2.4
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 46
diff changeset
959 "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
960 )
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
961 parser = expat.ParserCreate(encoding, "}")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
962 if target is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
963 target = TreeBuilder()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
964 # underscored names are provided for compatibility only
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
965 self.parser = self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
966 self.target = self._target = target
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
967 self._error = expat.error
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
968 self._names = {} # name memo cache
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
969 # callbacks
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
970 parser.DefaultHandlerExpand = self._default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
971 parser.StartElementHandler = self._start
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
972 parser.EndElementHandler = self._end
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
973 parser.CharacterDataHandler = self._data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
974 # let expat do the buffering, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
975 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
976 self._parser.buffer_text = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
977 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
978 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
979 # use new-style attribute handling, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
980 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
981 self._parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
982 self._parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
983 parser.StartElementHandler = self._start_list
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
984 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
985 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
986 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
987 self.entity = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
988 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
989 self.version = "Expat %d.%d.%d" % expat.version_info
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
990 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
991 pass # unknown
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
992
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
993 def _raiseerror(self, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
994 err = ParseError(value)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
995 err.code = value.code
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
996 err.position = value.lineno, value.offset
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
997 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
998
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
999 def _fixname(self, key):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1000 # 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
1001 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
1002 return self._names[key]
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1003 if '}' in key:
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1004 uri, name = key.split('}', 1)
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1005 name = QName(name, uri)
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1006 else:
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1007 name = QName(key)
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1008 self._names[key] = name
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1009 return name
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1010
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1011 def _start(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1012 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1013 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1014 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1015 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
1016 attrib[fixname(key)] = value
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1017 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1018
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1019 def _start_list(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1020 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1021 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1022 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1023 if attrib_in:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1024 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
1025 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
1026 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1027
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1028 def _data(self, text):
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1029 return self.target.data(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1030
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1031 def _end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1032 return self.target.end(self._fixname(tag))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1033
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1034 def _default(self, text):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1035 prefix = text[:1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1036 if prefix == "&":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1037 # deal with undefined entities
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1038 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1039 self.target.data(self.entity[text[1:-1]])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1040 except KeyError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1041 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1042 err = expat.error(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1043 "undefined entity %s: line %d, column %d" %
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1044 (text, self._parser.ErrorLineNumber,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1045 self._parser.ErrorColumnNumber)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1046 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1047 err.code = 11 # XML_ERROR_UNDEFINED_ENTITY
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1048 err.lineno = self._parser.ErrorLineNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1049 err.offset = self._parser.ErrorColumnNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1050 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1051 elif prefix == "<" and text[:9] == "<!DOCTYPE":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1052 self._doctype = [] # inside a doctype declaration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1053 elif self._doctype is not None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1054 # parse doctype contents
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1055 if prefix == ">":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1056 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1057 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1058 text = text.strip()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1059 if not text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1060 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1061 self._doctype.append(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1062 n = len(self._doctype)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1063 if n > 2:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1064 type = self._doctype[1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1065 if type == "PUBLIC" and n == 4:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1066 name, type, pubid, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1067 elif type == "SYSTEM" and n == 3:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1068 name, type, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1069 pubid = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1070 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1071 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1072 if pubid:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1073 pubid = pubid[1:-1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1074 if hasattr(self.target, "doctype"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1075 self.target.doctype(name, pubid, system[1:-1])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1076 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1077
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1078 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1079 # Feeds data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1080 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1081 # @param data Encoded data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1082
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1083 def feed(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1084 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1085 self._parser.Parse(data, 0)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1086 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1087 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1088
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1089 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1090 # Finishes feeding data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1091 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1092 # @return An element structure.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1093 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1094
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1095 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1096 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1097 self._parser.Parse("", 1) # end of data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1098 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1099 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1100 tree = self.target.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1101 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
1102 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1103
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1104 class BaseWriter(object):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1105 def __init__(self, encoding=None, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1106 self.encoding = encoding
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1107 self.namespaces = namespaces
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1108
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1109 def _encode(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1110 if self.encoding:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1111 return text.encode(self.encoding, "xmlcharrefreplace")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1112 return text
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1113
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1114 def _escape_cdata(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1115 # escape character data
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1116 # 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
1117 # 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
1118 # the most common case in most applications.
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1119 if "&" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1120 text = text.replace("&", "&amp;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1121 if "<" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1122 text = text.replace("<", "&lt;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1123 if ">" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1124 text = text.replace(">", "&gt;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1125 return self._encode(text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1126
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1127 def _escape_attrib(self, text):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1128 # escape attribute value
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1129 if "\"" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1130 text = text.replace("\"", "&quot;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1131 if "\n" in text:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1132 text = text.replace("\n", "&#10;")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1133 return self._escape_cdata(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 _namespaces(self, elem):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1136 # identify namespaces used in this tree
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1137
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1138 # maps qnames to *encoded* prefix:local names
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1139 qnames = {None: None}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1140
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1141 # maps uri:s to prefixes
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1142 candidate_namespaces = self._namespace_map.copy()
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1143 candidate_namespaces = {}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1144 candidate_namespaces.update(self.namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1145 used_namespaces = {}
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1146
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1147 def add_qname(qname):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1148 if qname in qnames:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1149 return
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1150
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1151 # calculate serialized qname representation
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1152 try:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1153 if qname.uri is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1154 uri = qname.uri
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1155 prefix = used_namespaces.get(uri, None)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1156 if prefix is None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1157 prefix = candidate_namespaces.get(uri, None)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1158 if prefix is None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1159 prefix = "ns%d" % len(used_namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1160 if prefix != "xml":
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1161 used_namespaces[uri] = prefix
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1162 if prefix:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1163 qnames[qname] = "%s:%s" % (prefix, qname.name)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1164 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1165 qnames[qname] = qname.name
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1166 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1167 # XXX: What happens with undefined namespace?
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1168 qnames[qname] = qname.name
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1169 except TypeError:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1170 self._raise_serialization_error(qname)
54
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 # populate qname and namespaces table
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1173 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1174 for elem in elem.iter():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1175 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1176 tag = elem.tag
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1177 if isinstance(tag, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1178 add_qname(tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1179 elif isinstance(tag, basestring):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1180 add_qname(QName(tag))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1181 elif tag is not None:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1182 self._raise_serialization_error(tag)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1183
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1184 for key in elem.keys():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1185 if isinstance(key, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1186 add_qname(key)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1187 elif isinstance(key, basestring):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1188 add_qname(QName(key))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1189 elif key is not None:
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1190 self._raise_serialization_error(key)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1191
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1192 return qnames, used_namespaces
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1193
65
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1194 @staticmethod
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1195 def _raise_serialization_error(text):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1196 raise TypeError(
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1197 "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
1198 )
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1199
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1200 ##
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1201 # 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
1202 # 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
1203 # will be removed.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1204 #
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1205 # @param prefix Namespace prefix.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1206 # @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
1207 # 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
1208 # @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
1209 # invalid.
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1210
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1211 @classmethod
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1212 def register_namespace(cls, prefix, uri):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1213 import re
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1214 if re.match("ns\d+$", prefix):
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1215 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
1216 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
1217 if k == uri or v == prefix:
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1218 del _namespace_map[k]
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1219 cls._namespace_map[uri] = prefix
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 _namespace_map = {
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1222 # "well-known" namespace prefixes
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1223 "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
1224 "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
1225 "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
1226 "http://schemas.xmlsoap.org/wsdl/": "wsdl",
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1227 # xml schema
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1228 "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
1229 "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
1230 # dublic core
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1231 "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
1232 }
d3fcc3556413 Tree - Move remaining serialization code into writer class
Bastian Blank <bblank@thinkmo.de>
parents: 64
diff changeset
1233
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1234 def serialize_start(self, write):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1235 pass
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1236
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1237 def write(self, write, element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1238 qnames, namespaces = self._namespaces(element)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1239 self.serialize_start(write)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1240 self.serialize(write, element, qnames, namespaces)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1241
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1242
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1243 class TextWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1244 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
1245 for part in elem.itertext():
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1246 write(self._encode(part))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1247
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1248
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1249 class XMLWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1250 def serialize(self, write, elem, qnames, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1251 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1252 tag = qnames[elem.tag]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1253
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1254 if tag is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1255 write("<" + tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1256
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1257 if elem.attrib:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1258 items = elem.attrib.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1259 items.sort(key=lambda x: x[0])
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1260 for k, v in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1261 k = qnames[k]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1262 if isinstance(v, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1263 v = qnames[v]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1264 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1265 v = self._escape_attrib(unicode(v))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1266 write(' ' + k + '="' + v + '"')
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1267 if namespaces:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1268 items = namespaces.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1269 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
1270 for v, k in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1271 if k:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1272 k = ":" + k
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1273 write(" xmlns%s=\"%s\"" % (
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1274 self._encode(k),
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1275 self._escape_attrib(v)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1276 ))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1277 if len(elem):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1278 write(">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1279 for e in elem:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1280 self.serialize(write, e, qnames)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1281 write("</" + tag + ">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1282 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1283 write(" />")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1284
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 for e in elem:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1287 self.serialize(write, e, encoding, qnames)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1288
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1289 elif isinstance(elem, Comment):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1290 write("<!--%s-->" % self._escape_cdata(elem.text))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1291
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1292 elif isinstance(elem, ProcessingInstruction):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1293 text = self._escape_cdata(elem.target)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1294 if elem.text is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1295 text += ' ' + self._escape_cdata(elem.text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1296 write("<?%s?>" % text)
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 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1299 write(self._escape_cdata(unicode(elem)))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1300
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1301 def serialize_start(self, write):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1302 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
1303 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
1304
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 class HTMLWriter(BaseWriter):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1307 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
1308 "img", "input", "isindex", "link", "meta" "param"))
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 def __init__(self, encoding=None, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1311 namespaces["http://www.w3.org/1999/xhtml"] = ''
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1312 super(HTMLWriter, self).__init__(encoding, namespaces)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1313
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1314 def serialize(self, write, elem, qnames, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1315 if isinstance(elem, Element):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1316 tag = qnames[elem.tag]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1317
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1318 if tag is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1319 write("<" + tag)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1320
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1321 if elem.attrib:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1322 items = elem.attrib.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1323 items.sort(key=lambda x: x[0])
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1324 for k, v in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1325 k = qnames[k]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1326 if isinstance(v, QName):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1327 v = qnames[v]
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1328 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1329 v = self._escape_attrib(unicode(v))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1330 # FIXME: handle boolean attributes
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1331 write(' ' + k + '="' + v + '"')
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1332 if namespaces:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1333 items = namespaces.items()
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1334 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
1335 for v, k in items:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1336 if k:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1337 k = ":" + k
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1338 write(" xmlns%s=\"%s\"" % (
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1339 self._encode(k),
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1340 self._escape_attrib(v)
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 write(">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1343
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1344 if tag.lower() in ('script', 'style'):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1345 write(self._encode(''.join(elem.itertext())))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1346 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1347 for e in elem:
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1348 self.serialize(write, e, qnames)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1349
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1350 if tag not in self.empty_elements:
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1351 write("</" + tag + ">")
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1352
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1353 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1354 for e in elem:
56
644bf7185b57 Tree - Make Element writable
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
1355 self.serialize(write, e, qnames)
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1356
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1357 elif isinstance(elem, Comment):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1358 write("<!--%s-->" % self._escape_cdata(elem.text))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1359
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1360 elif isinstance(elem, ProcessingInstruction):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1361 text = self._escape_cdata(elem.target)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1362 if elem.text is not None:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1363 text += ' ' + self._escape_cdata(elem.text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1364 write("<?%s?>" % text)
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1365
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1366 else:
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1367 write(self._escape_cdata(elem))
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
1368