Mercurial > genshi > mirror
annotate genshi/builder.py @ 735:7e428b22dbaa trunk
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.
author | cmlenz |
---|---|
date | Thu, 05 Jun 2008 09:49:53 +0000 |
parents | 5e9d250ad3ad |
children | ca72e3dc443d |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
408 | 3 # Copyright (C) 2006-2007 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
ad01564e87f2
* 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
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
70 |
730
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
71 try: |
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
72 set |
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
73 except NameError: |
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
74 from sets import Set as set |
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
75 |
230 | 76 from genshi.core import Attrs, Namespace, QName, Stream, START, END, TEXT |
1 | 77 |
433 | 78 __all__ = ['Fragment', 'Element', 'ElementFactory', 'tag'] |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
79 __docformat__ = 'restructuredtext en' |
1 | 80 |
81 | |
82 class Fragment(object): | |
28 | 83 """Represents a markup fragment, which is basically just a list of element |
84 or text nodes. | |
85 """ | |
1 | 86 __slots__ = ['children'] |
87 | |
88 def __init__(self): | |
433 | 89 """Create a new fragment.""" |
1 | 90 self.children = [] |
91 | |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
92 def __add__(self, other): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
93 return Fragment()(self, other) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
94 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
95 def __call__(self, *args): |
433 | 96 """Append any positional arguments as child nodes. |
97 | |
98 :see: `append` | |
99 """ | |
133
79f445396cd7
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
100 map(self.append, args) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
101 return self |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
102 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
103 def __iter__(self): |
98
44af12832c5a
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
104 return self._generate() |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
105 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
106 def __repr__(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
107 return '<%s>' % self.__class__.__name__ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
108 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
109 def __str__(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
110 return str(self.generate()) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
111 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
112 def __unicode__(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
113 return unicode(self.generate()) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
114 |
1 | 115 def append(self, node): |
433 | 116 """Append an element or string as child node. |
117 | |
118 :param node: the node to append; can be an `Element`, `Fragment`, or a | |
119 `Stream`, or a Python string or number | |
120 """ | |
379
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
121 if isinstance(node, (Stream, Element, basestring, int, float, long)): |
1 | 122 # For objects of a known/primitive type, we avoid the check for |
123 # whether it is iterable for better performance | |
124 self.children.append(node) | |
125 elif isinstance(node, Fragment): | |
133
79f445396cd7
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
126 self.children.extend(node.children) |
1 | 127 elif node is not None: |
128 try: | |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
501
diff
changeset
|
129 map(self.append, iter(node)) |
1 | 130 except TypeError: |
131 self.children.append(node) | |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
132 |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
133 def _generate(self): |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
134 for child in self.children: |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
135 if isinstance(child, Fragment): |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
136 for event in child._generate(): |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
137 yield event |
379
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
138 elif isinstance(child, Stream): |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
139 for event in child: |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
140 yield event |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
141 else: |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
142 if not isinstance(child, basestring): |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
143 child = unicode(child) |
133
79f445396cd7
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
144 yield TEXT, child, (None, -1, -1) |
1 | 145 |
146 def generate(self): | |
498 | 147 """Return a markup event stream for the fragment. |
148 | |
149 :rtype: `Stream` | |
150 """ | |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
151 return Stream(self._generate()) |
1 | 152 |
153 | |
345 | 154 def _kwargs_to_attrs(kwargs): |
735
7e428b22dbaa
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 attrs = [] |
7e428b22dbaa
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
|
156 names = set() |
730
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
157 for name, value in kwargs.items(): |
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
158 name = name.rstrip('_').replace('_', '-') |
735
7e428b22dbaa
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 if value is not None and name not in names: |
7e428b22dbaa
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 attrs.append((QName(name), unicode(value))) |
7e428b22dbaa
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 names.add(name) |
7e428b22dbaa
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
|
162 return Attrs(attrs) |
345 | 163 |
164 | |
1 | 165 class Element(Fragment): |
166 """Simple XML output generator based on the builder pattern. | |
167 | |
168 Construct XML elements by passing the tag name to the constructor: | |
169 | |
170 >>> print Element('strong') | |
171 <strong/> | |
172 | |
173 Attributes can be specified using keyword arguments. The values of the | |
174 arguments will be converted to strings and any special XML characters | |
175 escaped: | |
176 | |
177 >>> print Element('textarea', rows=10, cols=60) | |
178 <textarea rows="10" cols="60"/> | |
179 >>> print Element('span', title='1 < 2') | |
180 <span title="1 < 2"/> | |
181 >>> print Element('span', title='"baz"') | |
182 <span title=""baz""/> | |
183 | |
184 The " character is escaped using a numerical entity. | |
185 The order in which attributes are rendered is undefined. | |
186 | |
187 If an attribute value evaluates to `None`, that attribute is not included | |
188 in the output: | |
189 | |
190 >>> print Element('a', name=None) | |
191 <a/> | |
192 | |
193 Attribute names that conflict with Python keywords can be specified by | |
194 appending an underscore: | |
195 | |
196 >>> print Element('div', class_='warning') | |
197 <div class="warning"/> | |
198 | |
199 Nested elements can be added to an element using item access notation. | |
200 The call notation can also be used for this and for adding attributes | |
201 using keyword arguments, as one would do in the constructor. | |
202 | |
203 >>> print Element('ul')(Element('li'), Element('li')) | |
204 <ul><li/><li/></ul> | |
205 >>> print Element('a')('Label') | |
206 <a>Label</a> | |
207 >>> print Element('a')('Label', href="target") | |
208 <a href="target">Label</a> | |
209 | |
210 Text nodes can be nested in an element by adding strings instead of | |
211 elements. Any special characters in the strings are escaped automatically: | |
212 | |
213 >>> print Element('em')('Hello world') | |
214 <em>Hello world</em> | |
215 >>> print Element('em')(42) | |
216 <em>42</em> | |
217 >>> print Element('em')('1 < 2') | |
218 <em>1 < 2</em> | |
219 | |
220 This technique also allows mixed content: | |
221 | |
222 >>> print Element('p')('Hello ', Element('b')('world')) | |
223 <p>Hello <b>world</b></p> | |
224 | |
34 | 225 Quotes are not escaped inside text nodes: |
226 >>> print Element('p')('"Hello"') | |
227 <p>"Hello"</p> | |
228 | |
1 | 229 Elements can also be combined with other elements or strings using the |
230 addition operator, which results in a `Fragment` object that contains the | |
231 operands: | |
232 | |
233 >>> print Element('br') + 'some text' + Element('br') | |
234 <br/>some text<br/> | |
235 | |
236 Elements with a namespace can be generated using the `Namespace` and/or | |
237 `QName` classes: | |
238 | |
230 | 239 >>> from genshi.core import Namespace |
1 | 240 >>> xhtml = Namespace('http://www.w3.org/1999/xhtml') |
241 >>> print Element(xhtml.html, lang='en') | |
410
d14d89995c29
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
242 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/> |
1 | 243 """ |
244 __slots__ = ['tag', 'attrib'] | |
245 | |
246 def __init__(self, tag_, **attrib): | |
247 Fragment.__init__(self) | |
248 self.tag = QName(tag_) | |
730
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
249 self.attrib = _kwargs_to_attrs(attrib) |
1 | 250 |
251 def __call__(self, *args, **kwargs): | |
433 | 252 """Append any positional arguments as child nodes, and keyword arguments |
253 as attributes. | |
254 | |
498 | 255 :return: the element itself so that calls can be chained |
256 :rtype: `Element` | |
433 | 257 :see: `Fragment.append` |
258 """ | |
730
5e9d250ad3ad
Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents:
506
diff
changeset
|
259 self.attrib |= _kwargs_to_attrs(kwargs) |
98
44af12832c5a
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
260 Fragment.__call__(self, *args) |
44af12832c5a
Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents:
94
diff
changeset
|
261 return self |
1 | 262 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
263 def __repr__(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
264 return '<%s "%s">' % (self.__class__.__name__, self.tag) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
34
diff
changeset
|
265 |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
266 def _generate(self): |
133
79f445396cd7
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
267 yield START, (self.tag, self.attrib), (None, -1, -1) |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
268 for kind, data, pos in Fragment._generate(self): |
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
269 yield kind, data, pos |
133
79f445396cd7
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
119
diff
changeset
|
270 yield END, self.tag, (None, -1, -1) |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
271 |
1 | 272 def generate(self): |
498 | 273 """Return a markup event stream for the fragment. |
274 | |
275 :rtype: `Stream` | |
276 """ | |
94
0f8800c46e21
Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents:
91
diff
changeset
|
277 return Stream(self._generate()) |
1 | 278 |
279 | |
280 class ElementFactory(object): | |
28 | 281 """Factory for `Element` objects. |
282 | |
283 A new element is created simply by accessing a correspondingly named | |
284 attribute of the factory object: | |
285 | |
286 >>> factory = ElementFactory() | |
287 >>> print factory.foo | |
288 <foo/> | |
289 >>> print factory.foo(id=2) | |
290 <foo id="2"/> | |
291 | |
119
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
292 Markup fragments (lists of nodes without a parent element) can be created |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
293 by calling the factory: |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
294 |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
295 >>> print factory('Hello, ', factory.em('world'), '!') |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
296 Hello, <em>world</em>! |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
297 |
28 | 298 A factory can also be bound to a specific namespace: |
299 | |
300 >>> factory = ElementFactory('http://www.w3.org/1999/xhtml') | |
301 >>> print factory.html(lang="en") | |
410
d14d89995c29
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
302 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/> |
28 | 303 |
304 The namespace for a specific element can be altered on an existing factory | |
305 by specifying the new namespace using item access: | |
306 | |
307 >>> factory = ElementFactory() | |
308 >>> print factory.html(factory['http://www.w3.org/2000/svg'].g(id=3)) | |
410
d14d89995c29
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
309 <html><g xmlns="http://www.w3.org/2000/svg" id="3"/></html> |
28 | 310 |
311 Usually, the `ElementFactory` class is not be used directly. Rather, the | |
312 `tag` instance should be used to create elements. | |
313 """ | |
1 | 314 |
19
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
315 def __init__(self, namespace=None): |
28 | 316 """Create the factory, optionally bound to the given namespace. |
317 | |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
318 :param namespace: the namespace URI for any created elements, or `None` |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
410
diff
changeset
|
319 for no namespace |
28 | 320 """ |
20 | 321 if namespace and not isinstance(namespace, Namespace): |
19
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
322 namespace = Namespace(namespace) |
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
323 self.namespace = namespace |
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
324 |
119
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
325 def __call__(self, *args): |
433 | 326 """Create a fragment that has the given positional arguments as child |
327 nodes. | |
328 | |
329 :return: the created `Fragment` | |
498 | 330 :rtype: `Fragment` |
433 | 331 """ |
119
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
332 return Fragment()(*args) |
cc2aee07f53b
Allow creating fragments from the `tag` object in `markup.builder`.
cmlenz
parents:
98
diff
changeset
|
333 |
19
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
334 def __getitem__(self, namespace): |
433 | 335 """Return a new factory that is bound to the specified namespace. |
336 | |
337 :param namespace: the namespace URI or `Namespace` object | |
338 :return: an `ElementFactory` that produces elements bound to the given | |
339 namespace | |
498 | 340 :rtype: `ElementFactory` |
433 | 341 """ |
19
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
342 return ElementFactory(namespace) |
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
343 |
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
344 def __getattr__(self, name): |
433 | 345 """Create an `Element` with the given name. |
346 | |
347 :param name: the tag name of the element to create | |
348 :return: an `Element` with the specified name | |
498 | 349 :rtype: `Element` |
433 | 350 """ |
19
704f10b06507
Enable `ElementFactory` to create namespaced elements.
cmlenz
parents:
17
diff
changeset
|
351 return Element(self.namespace and self.namespace[name] or name) |
1 | 352 |
353 | |
354 tag = ElementFactory() | |
498 | 355 """Global `ElementFactory` bound to the default namespace. |
356 | |
357 :type: `ElementFactory` | |
358 """ |