# HG changeset patch # User cmlenz # Date 1154884247 0 # Node ID b9a0031d4bbb1bd3e4b1a0c53914d675284b7edb # Parent 0288dedce3d2692717a9a3d92041f1aac3355d0f Minor cleanup and performance improvement for the builder module. diff --git a/markup/builder.py b/markup/builder.py --- a/markup/builder.py +++ b/markup/builder.py @@ -11,7 +11,7 @@ # individuals. For the exact contribution history, see the revision # history and logs, available at http://markup.edgewall.org/log/. -from markup.core import Attributes, Namespace, QName, Stream, escape +from markup.core import Attributes, Namespace, QName, Stream, START, END, TEXT __all__ = ['Fragment', 'Element', 'tag'] @@ -29,8 +29,7 @@ return Fragment()(self, other) def __call__(self, *args): - for arg in args: - self.append(arg) + map(self.append, args) return self def __iter__(self): @@ -52,15 +51,12 @@ # whether it is iterable for better performance self.children.append(node) elif isinstance(node, Fragment): - self.children += node.children + self.children.extend(node.children) elif node is not None: try: - children = iter(node) + map(self.append, iter(node)) except TypeError: self.children.append(node) - else: - for child in children: - self.append(child) def _generate(self): for child in self.children: @@ -70,7 +66,7 @@ else: if not isinstance(child, basestring): child = unicode(child) - yield Stream.TEXT, child, (None, -1, -1) + yield TEXT, child, (None, -1, -1) def generate(self): """Return a markup event stream for the fragment.""" @@ -162,14 +158,19 @@ Fragment.__init__(self) self.tag = QName(tag_) self.attrib = Attributes() - self(**attrib) + for attr, value in attrib.items(): + if value is not None: + if not isinstance(value, basestring): + value = unicode(value) + self.attrib.append((QName(attr.rstrip('_').replace('_', '-')), + value)) def __call__(self, *args, **kwargs): for attr, value in kwargs.items(): - if value is None: - continue - attr = attr.rstrip('_').replace('_', '-') - self.attrib.set(attr, escape(value)) + if value is not None: + if not isinstance(value, basestring): + value = unicode(value) + self.attrib.set(attr.rstrip('_').replace('_', '-'), value) Fragment.__call__(self, *args) return self @@ -177,15 +178,10 @@ return '<%s "%s">' % (self.__class__.__name__, self.tag) def _generate(self): - attrib = Attributes() - for attr, value in self.attrib: - if not isinstance(value, basestring): - value = unicode(value) - attrib.append((attr, value)) - yield Stream.START, (self.tag, attrib), (None, -1, -1) + yield START, (self.tag, self.attrib), (None, -1, -1) for kind, data, pos in Fragment._generate(self): yield kind, data, pos - yield Stream.END, self.tag, (None, -1, -1) + yield END, self.tag, (None, -1, -1) def generate(self): """Return a markup event stream for the fragment.""" diff --git a/markup/core.py b/markup/core.py --- a/markup/core.py +++ b/markup/core.py @@ -197,7 +197,9 @@ """Return whether the list includes an attribute with the specified name. """ - return name in [attr for attr, _ in self] + for attr, _ in self: + if attr == name: + return True def get(self, name, default=None): """Return the value of the attribute with the specified name, or the diff --git a/markup/eval.py b/markup/eval.py --- a/markup/eval.py +++ b/markup/eval.py @@ -143,7 +143,6 @@ try: return obj[key] except (KeyError, IndexError, TypeError), e: - pass if isinstance(key, basestring): try: return getattr(obj, key) diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -1064,7 +1064,7 @@ for dirname in search_path: filepath = os.path.join(dirname, filename) try: - fileobj = file(filepath, 'rt') + fileobj = open(filepath, 'U') try: tmpl = Template(fileobj, basedir=dirname, filename=filename) tmpl.filters.append(IncludeFilter(self))