changeset 403:228907abb726 trunk

Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
author cmlenz
date Wed, 21 Feb 2007 09:51:43 +0000
parents c199e9b95884
children ee17693d2976
files genshi/builder.py genshi/core.py genshi/filters.py genshi/input.py genshi/template/directives.py
diffstat 5 files changed, 21 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- 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]
 
 
--- 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.
--- 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
--- 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:
--- 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
Copyright (C) 2012-2017 Edgewall Software