Mercurial > genshi > genshi-test
annotate genshi/builder.py @ 750:d007a0d7ba81
Remove some cruft for supporting Python 2.3.
author | cmlenz |
---|---|
date | Mon, 09 Jun 2008 15:19:59 +0000 |
parents | f7da2de9b293 |
children | 4376010bb97e |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
741 | 3 # Copyright (C) 2006-2008 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
430 | 14 """Support for programmatically generating markup streams from Python code using |
15 a very simple syntax. The main entry point to this module is the `tag` object | |
16 (which is actually an instance of the ``ElementFactory`` class). You should | |
17 rarely (if ever) need to directly import and use any of the other classes in | |
18 this module. | |
19 | |
20 Elements can be created using the `tag` object using attribute access. For | |
21 example: | |
22 | |
23 >>> doc = tag.p('Some text and ', tag.a('a link', href='http://example.org/'), '.') | |
24 >>> doc | |
25 <Element "p"> | |
26 | |
27 This produces an `Element` instance which can be further modified to add child | |
28 nodes and attributes. This is done by "calling" the element: positional | |
29 arguments are added as child nodes (alternatively, the `Element.append` method | |
30 can be used for that purpose), whereas keywords arguments are added as | |
31 attributes: | |
32 | |
33 >>> doc(tag.br) | |
34 <Element "p"> | |
35 >>> print doc | |
36 <p>Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
37 | |
38 If an attribute name collides with a Python keyword, simply append an underscore | |
39 to the name: | |
40 | |
41 >>> doc(class_='intro') | |
42 <Element "p"> | |
43 >>> print doc | |
44 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
45 | |
46 As shown above, an `Element` can easily be directly rendered to XML text by | |
47 printing it or using the Python ``str()`` function. This is basically a | |
48 shortcut for converting the `Element` to a stream and serializing that | |
49 stream: | |
50 | |
51 >>> stream = doc.generate() | |
431
747baa1cd597
* Don't allow `style` attributes by default in the `HTMLSanitizer`. Closes #97.
cmlenz
parents:
430
diff
changeset
|
52 >>> stream #doctest: +ELLIPSIS |
430 | 53 <genshi.core.Stream object at ...> |
54 >>> print stream | |
55 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
56 | |
57 | |
58 The `tag` object also allows creating "fragments", which are basically lists | |
59 of nodes (elements or text) that don't have a parent element. This can be useful | |
60 for creating snippets of markup that are attached to a parent element later (for | |
61 example in a template). Fragments are created by calling the `tag` object, which | |
62 returns an object of type `Fragment`: | |
63 | |
64 >>> fragment = tag('Hello, ', tag.em('world'), '!') | |
65 >>> fragment | |
66 <Fragment> | |
67 >>> print fragment | |
68 Hello, <em>world</em>! | |
69 """ | |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
70 |
737
686bbeecb9ac
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
735
diff
changeset
|
71 from genshi.core import Attrs, Markup, Namespace, QName, Stream, \ |
686bbeecb9ac
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
735
diff
changeset
|
72 START, END, TEXT |
1 | 73 |
433 | 74 __all__ = ['Fragment', 'Element', 'ElementFactory', 'tag'] |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
75 __docformat__ = 'restructuredtext en' |
1 | 76 |
77 | |
78 class Fragment(object): | |
28 | 79 """Represents a markup fragment, which is basically just a list of element |
80 or text nodes. | |
81 """ | |
1 | 82 __slots__ = ['children'] |
83 | |
84 def __init__(self): | |
433 | 85 """Create a new fragment.""" |
1 | 86 self.children = [] |
87 | |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
88 def __add__(self, other): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
89 return Fragment()(self, other) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
90 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
91 def __call__(self, *args): |
433 | 92 """Append any positional arguments as child nodes. |
93 | |
94 :see: `append` | |
95 """ | |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
96 map(self.append, args) |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
97 return self |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
98 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
99 def __iter__(self): |
98
bc73d3ab823f
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
100 return self._generate() |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
101 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
102 def __repr__(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
103 return '<%s>' % self.__class__.__name__ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
104 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
105 def __str__(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
106 return str(self.generate()) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
107 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
108 def __unicode__(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
109 return unicode(self.generate()) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
110 |
737
686bbeecb9ac
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
735
diff
changeset
|
111 def __html__(self): |
686bbeecb9ac
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
735
diff
changeset
|
112 return Markup(self.generate()) |
686bbeecb9ac
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
735
diff
changeset
|
113 |
1 | 114 def append(self, node): |
433 | 115 """Append an element or string as child node. |
116 | |
117 :param node: the node to append; can be an `Element`, `Fragment`, or a | |
118 `Stream`, or a Python string or number | |
119 """ | |
379
d8c5045b547a
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
120 if isinstance(node, (Stream, Element, basestring, int, float, long)): |
1 | 121 # For objects of a known/primitive type, we avoid the check for |
122 # whether it is iterable for better performance | |
123 self.children.append(node) | |
124 elif isinstance(node, Fragment): | |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
125 self.children.extend(node.children) |
1 | 126 elif node is not None: |
127 try: | |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
501
diff
changeset
|
128 map(self.append, iter(node)) |
1 | 129 except TypeError: |
130 self.children.append(node) | |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
131 |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
132 def _generate(self): |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
133 for child in self.children: |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
134 if isinstance(child, Fragment): |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
135 for event in child._generate(): |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
136 yield event |
379
d8c5045b547a
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
137 elif isinstance(child, Stream): |
d8c5045b547a
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
138 for event in child: |
d8c5045b547a
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
139 yield event |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
140 else: |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
141 if not isinstance(child, basestring): |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
142 child = unicode(child) |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
143 yield TEXT, child, (None, -1, -1) |
1 | 144 |
145 def generate(self): | |
498 | 146 """Return a markup event stream for the fragment. |
147 | |
148 :rtype: `Stream` | |
149 """ | |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
150 return Stream(self._generate()) |
1 | 151 |
152 | |
345 | 153 def _kwargs_to_attrs(kwargs): |
735
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
154 attrs = [] |
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
155 names = set() |
730
a424b2443e11
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
156 for name, value in kwargs.items(): |
a424b2443e11
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
157 name = name.rstrip('_').replace('_', '-') |
735
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
158 if value is not None and name not in names: |
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
159 attrs.append((QName(name), unicode(value))) |
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
160 names.add(name) |
92e2fb9c056f
Tweak to [854] to not change the ordering of attributes compared to how they were previously ordered, to avoid breaking any tests that rely on the order. See #216.
cmlenz
parents:
730
diff
changeset
|
161 return Attrs(attrs) |
345 | 162 |
163 | |
1 | 164 class Element(Fragment): |
165 """Simple XML output generator based on the builder pattern. | |
166 | |
167 Construct XML elements by passing the tag name to the constructor: | |
168 | |
169 >>> print Element('strong') | |
170 <strong/> | |
171 | |
172 Attributes can be specified using keyword arguments. The values of the | |
173 arguments will be converted to strings and any special XML characters | |
174 escaped: | |
175 | |
176 >>> print Element('textarea', rows=10, cols=60) | |
177 <textarea rows="10" cols="60"/> | |
178 >>> print Element('span', title='1 < 2') | |
179 <span title="1 < 2"/> | |
180 >>> print Element('span', title='"baz"') | |
181 <span title=""baz""/> | |
182 | |
183 The " character is escaped using a numerical entity. | |
184 The order in which attributes are rendered is undefined. | |
185 | |
186 If an attribute value evaluates to `None`, that attribute is not included | |
187 in the output: | |
188 | |
189 >>> print Element('a', name=None) | |
190 <a/> | |
191 | |
192 Attribute names that conflict with Python keywords can be specified by | |
193 appending an underscore: | |
194 | |
195 >>> print Element('div', class_='warning') | |
196 <div class="warning"/> | |
197 | |
198 Nested elements can be added to an element using item access notation. | |
199 The call notation can also be used for this and for adding attributes | |
200 using keyword arguments, as one would do in the constructor. | |
201 | |
202 >>> print Element('ul')(Element('li'), Element('li')) | |
203 <ul><li/><li/></ul> | |
204 >>> print Element('a')('Label') | |
205 <a>Label</a> | |
206 >>> print Element('a')('Label', href="target") | |
207 <a href="target">Label</a> | |
208 | |
209 Text nodes can be nested in an element by adding strings instead of | |
210 elements. Any special characters in the strings are escaped automatically: | |
211 | |
212 >>> print Element('em')('Hello world') | |
213 <em>Hello world</em> | |
214 >>> print Element('em')(42) | |
215 <em>42</em> | |
216 >>> print Element('em')('1 < 2') | |
217 <em>1 < 2</em> | |
218 | |
219 This technique also allows mixed content: | |
220 | |
221 >>> print Element('p')('Hello ', Element('b')('world')) | |
222 <p>Hello <b>world</b></p> | |
223 | |
34 | 224 Quotes are not escaped inside text nodes: |
225 >>> print Element('p')('"Hello"') | |
226 <p>"Hello"</p> | |
227 | |
1 | 228 Elements can also be combined with other elements or strings using the |
229 addition operator, which results in a `Fragment` object that contains the | |
230 operands: | |
231 | |
232 >>> print Element('br') + 'some text' + Element('br') | |
233 <br/>some text<br/> | |
234 | |
235 Elements with a namespace can be generated using the `Namespace` and/or | |
236 `QName` classes: | |
237 | |
230 | 238 >>> from genshi.core import Namespace |
1 | 239 >>> xhtml = Namespace('http://www.w3.org/1999/xhtml') |
240 >>> print Element(xhtml.html, lang='en') | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
241 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/> |
1 | 242 """ |
243 __slots__ = ['tag', 'attrib'] | |
244 | |
245 def __init__(self, tag_, **attrib): | |
246 Fragment.__init__(self) | |
247 self.tag = QName(tag_) | |
730
a424b2443e11
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
248 self.attrib = _kwargs_to_attrs(attrib) |
1 | 249 |
250 def __call__(self, *args, **kwargs): | |
433 | 251 """Append any positional arguments as child nodes, and keyword arguments |
252 as attributes. | |
253 | |
498 | 254 :return: the element itself so that calls can be chained |
255 :rtype: `Element` | |
433 | 256 :see: `Fragment.append` |
257 """ | |
730
a424b2443e11
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
258 self.attrib |= _kwargs_to_attrs(kwargs) |
98
bc73d3ab823f
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
259 Fragment.__call__(self, *args) |
bc73d3ab823f
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
260 return self |
1 | 261 |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
262 def __repr__(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
263 return '<%s "%s">' % (self.__class__.__name__, self.tag) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
264 |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
265 def _generate(self): |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
266 yield START, (self.tag, self.attrib), (None, -1, -1) |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
267 for kind, data, pos in Fragment._generate(self): |
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
268 yield kind, data, pos |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
269 yield END, self.tag, (None, -1, -1) |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
270 |
1 | 271 def generate(self): |
498 | 272 """Return a markup event stream for the fragment. |
273 | |
274 :rtype: `Stream` | |
275 """ | |
94
80c72e936e72
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
276 return Stream(self._generate()) |
1 | 277 |
278 | |
279 class ElementFactory(object): | |
28 | 280 """Factory for `Element` objects. |
281 | |
282 A new element is created simply by accessing a correspondingly named | |
283 attribute of the factory object: | |
284 | |
285 >>> factory = ElementFactory() | |
286 >>> print factory.foo | |
287 <foo/> | |
288 >>> print factory.foo(id=2) | |
289 <foo id="2"/> | |
290 | |
119
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
291 Markup fragments (lists of nodes without a parent element) can be created |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
292 by calling the factory: |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
293 |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
294 >>> print factory('Hello, ', factory.em('world'), '!') |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
295 Hello, <em>world</em>! |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
296 |
28 | 297 A factory can also be bound to a specific namespace: |
298 | |
299 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml') | |
300 >>> print factory.html(lang="en") | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
301 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/> |
28 | 302 |
303 The namespace for a specific element can be altered on an existing factory | |
304 by specifying the new namespace using item access: | |
305 | |
306 >>> factory = ElementFactory() | |
307 >>> print factory.html(factory['http://www.w3.org/2000/svg'].g(id=3)) | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
308 <html><g xmlns="http://www.w3.org/2000/svg" id="3"/></html> |
28 | 309 |
310 Usually, the `ElementFactory` class is not be used directly. Rather, the | |
311 `tag` instance should be used to create elements. | |
312 """ | |
1 | 313 |
19
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
314 def __init__(self, namespace=None): |
28 | 315 """Create the factory, optionally bound to the given namespace. |
316 | |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
317 :param namespace: the namespace URI for any created elements, or `None` |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
318 for no namespace |
28 | 319 """ |
20 | 320 if namespace and not isinstance(namespace, Namespace): |
19
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
321 namespace = Namespace(namespace) |
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
322 self.namespace = namespace |
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
323 |
119
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
324 def __call__(self, *args): |
433 | 325 """Create a fragment that has the given positional arguments as child |
326 nodes. | |
327 | |
328 :return: the created `Fragment` | |
498 | 329 :rtype: `Fragment` |
433 | 330 """ |
119
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
331 return Fragment()(*args) |
61b133ed95b2
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
332 |
19
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
333 def __getitem__(self, namespace): |
433 | 334 """Return a new factory that is bound to the specified namespace. |
335 | |
336 :param namespace: the namespace URI or `Namespace` object | |
337 :return: an `ElementFactory` that produces elements bound to the given | |
338 namespace | |
498 | 339 :rtype: `ElementFactory` |
433 | 340 """ |
19
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
341 return ElementFactory(namespace) |
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
342 |
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
343 def __getattr__(self, name): |
433 | 344 """Create an `Element` with the given name. |
345 | |
346 :param name: the tag name of the element to create | |
347 :return: an `Element` with the specified name | |
498 | 348 :rtype: `Element` |
433 | 349 """ |
19
7b589453b797
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
350 return Element(self.namespace and self.namespace[name] or name) |
1 | 351 |
352 | |
353 tag = ElementFactory() | |
498 | 354 """Global `ElementFactory` bound to the default namespace. |
355 | |
356 :type: `ElementFactory` | |
357 """ |