# HG changeset patch # User cmlenz # Date 1172051503 0 # Node ID 228907abb726945085e3930c7ddfbb96b60da2ab # Parent c199e9b958848d74651f0efdcf9635c8bc53c8a7 Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`. diff --git a/genshi/builder.py b/genshi/builder.py --- a/genshi/builder.py +++ b/genshi/builder.py @@ -82,7 +82,7 @@ return unicode(value) def _kwargs_to_attrs(kwargs): - return [(k.rstrip('_').replace('_', '-'), _value_to_unicode(v)) + return [(QName(k.rstrip('_').replace('_', '-')), _value_to_unicode(v)) for k, v in kwargs.items() if v is not None] diff --git a/genshi/core.py b/genshi/core.py --- a/genshi/core.py +++ b/genshi/core.py @@ -222,13 +222,13 @@ >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) >>> attrs - Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')]) + Attrs([('href', '#'), ('title', 'Foo')]) >>> 'href' in attrs True >>> 'tabindex' in attrs False - >>> attrs.get(u'title') + >>> attrs.get('title') 'Foo' Instances may not be manipulated directly. Instead, the operators `|` and @@ -240,7 +240,7 @@ the attribute(s) to remove: >>> attrs - 'title' - Attrs([(QName(u'href'), '#')]) + Attrs([('href', '#')]) >>> attrs - ('title', 'href') Attrs() @@ -248,34 +248,26 @@ used with an assignment: >>> attrs - Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')]) + Attrs([('href', '#'), ('title', 'Foo')]) >>> attrs -= 'title' >>> attrs - Attrs([(QName(u'href'), '#')]) + Attrs([('href', '#')]) To add a new attribute, use the `|` operator, where the right hand value is a sequence of `(name, value)` tuples (which includes `Attrs` instances): - >>> attrs | [(u'title', 'Bar')] - Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Bar')]) + >>> attrs | [('title', 'Bar')] + Attrs([('href', '#'), ('title', 'Bar')]) If the attributes already contain an attribute with a given name, the value of that attribute is replaced: - >>> attrs | [(u'href', 'http://example.org/')] - Attrs([(QName(u'href'), 'http://example.org/')]) + >>> attrs | [('href', 'http://example.org/')] + Attrs([('href', 'http://example.org/')]) """ __slots__ = [] - def __new__(cls, items=()): - """Create the `Attrs` instance. - - If the `items` parameter is provided, it is expected to be a sequence - of `(name, value)` tuples. - """ - return tuple.__new__(cls, [(QName(name), val) for name, val in items]) - def __contains__(self, name): """Return whether the list includes an attribute with the specified name. diff --git a/genshi/filters.py b/genshi/filters.py --- a/genshi/filters.py +++ b/genshi/filters.py @@ -19,7 +19,7 @@ from sets import ImmutableSet as frozenset import re -from genshi.core import Attrs, stripentities +from genshi.core import Attrs, QName, stripentities from genshi.core import END, START, TEXT __all__ = ['HTMLFormFiller', 'HTMLSanitizer'] @@ -102,7 +102,7 @@ elif type == 'checkbox': checked = bool(value) if checked: - attrs |= [('checked', 'checked')] + attrs |= [(QName('checked'), 'checked')] elif 'checked' in attrs: attrs -= 'checked' elif type in (None, 'hidden', 'text'): @@ -112,7 +112,7 @@ if isinstance(value, (list, tuple)): value = value[0] if value is not None: - attrs |= [('value', unicode(value))] + attrs |= [(QName('value'), unicode(value))] elif tagname == 'select': name = attrs.get('name') select_value = self.data.get(name) @@ -155,7 +155,7 @@ selected = option_value == select_value okind, (tag, attrs), opos = option_start if selected: - attrs |= [('selected', 'selected')] + attrs |= [(QName('selected'), 'selected')] elif 'selected' in attrs: attrs -= 'selected' yield okind, (tag, attrs), opos diff --git a/genshi/input.py b/genshi/input.py --- a/genshi/input.py +++ b/genshi/input.py @@ -182,7 +182,9 @@ self.expat.CurrentColumnNumber) def _handle_start(self, tag, attrib): - self._enqueue(START, (QName(tag), Attrs(zip(*[iter(attrib)] * 2)))) + attrs = Attrs([(QName(name), value) for name, value in + zip(*[iter(attrib)] * 2)]) + self._enqueue(START, (QName(tag), attrs)) def _handle_end(self, tag): self._enqueue(END, QName(tag)) @@ -315,7 +317,7 @@ value = unicode(name) elif not isinstance(value, unicode): value = value.decode(self.encoding, 'replace') - fixed_attrib.append((name, stripentities(value))) + fixed_attrib.append((QName(name), stripentities(value))) self._enqueue(START, (QName(tag), Attrs(fixed_attrib))) if tag in self._EMPTY_ELEMS: diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -15,7 +15,7 @@ import compiler -from genshi.core import Attrs, Stream +from genshi.core import Attrs, QName, Stream from genshi.path import Path from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \ EXPR, _apply_directives @@ -170,8 +170,8 @@ elif not isinstance(attrs, list): # assume it's a dict attrs = attrs.items() attrib -= [name for name, val in attrs if val is None] - attrib |= [(name, unicode(val).strip()) for name, val in attrs - if val is not None] + attrib |= [(QName(name), unicode(val).strip()) for name, val + in attrs if val is not None] yield kind, (tag, attrib), pos for event in stream: yield event