diff --git a/src/mio/parser.py b/src/mio/parser.py index 582871a804b5c5d8e95b9b929d42c6c4e2d68ae4..daeb5dbd89f5341d9dcdfe63fe79c479dc5cda4b 100755 --- a/src/mio/parser.py +++ b/src/mio/parser.py @@ -136,58 +136,6 @@ def xml_escape(s): return result -class AttrSortWrapper: - """Wrapper class for sorting attributes - - [see Node._xml(...)]""" - def __init__(self, node, sort): - self.node = node - self.sort = sort - - def __call__(self, a, b): - """ - Return the sort order between attributes 'a' and 'b'. - - If sort returns None, a standard Python comparison is used. - """ - result = None - if self.sort: - result = self.sort(self.node, a, b) - if result == None: - if a < b: - result = -1 - elif a > b: - result = 1 - else: - result = 0 - return result - -class TagSortWrapper: - """Wrapper class for sorting tags - - [see Node._xml(...)]""" - def __init__(self, parent, sort): - self.parent = parent - self.sort = sort - - def __call__(self, a, b): - """ - Return the sort order between tags 'a' and 'b'. - - If sort returns None, a standard Python comparison is used. - """ - result = None - if self.sort: - result = self.sort(self.parent, a, b) - if result == None: - if a._tag < b._tag: - result = -1 - elif a._tag > b._tag: - result = 1 - else: - result = 0 - return result - class ChildAccessor: """Helper class for iterating over xml trees @@ -374,14 +322,13 @@ class Node: else: return AttributeAccessor(self, attr) - def _xml(self, indent=0, attr_sort=None, tag_sort=None, width=80): + def _xml(self, indent=0, attr_key=None, node_key=None, width=80): """Generate a prettyprinted xml of the tree rooted in this node""" result = "" line = "%s<%s" % (" " * indent, self._tag) blanks = " " * len(line) for k in sorted(self._attribute.keys(), - key=(attr_sort and AttrSortWrapper(self, attr_sort) - or None)): + key=attr_key and (lambda a: attr_key(self, a))): s = " %s='%s'" % (k, xml_escape(self._attribute[k])) if len(line) + len(s) > width: result += "%s\n" % line @@ -396,12 +343,9 @@ class Node: result += '\n'.join(self._char) result += '\n' pass - print("YYY", (tag_sort and TagSortWrapper(self, tag_sort) - or None), "\n") for c in sorted(self._children, - key=(tag_sort and TagSortWrapper(self, tag_sort) - or None)): - result += c._xml(indent + 1, attr_sort, tag_sort, width) + key=node_key and (lambda c: node_key(self, c))): + result += c._xml(indent + 1, attr_key, node_key, width) result += "%s</%s>\n" % (" " * indent, self._tag) return result @@ -453,6 +397,9 @@ class Comment: self._tag = '' self._attribute = { '': content} + def __lt__(self, other): + return self._tag < other._tag + def __copy__(self): """Copy self""" result = Comment(self._content, self._line) @@ -462,7 +409,7 @@ class Comment: """Copy node and all children""" return self.__copy__() - def _xml(self, indent=0, attr_sort=None, tag_sort=None, width=80): + def _xml(self, indent=0, attr_key=None, node_key=None, width=80): """Generate a prettyprinted xml of the tree rooted in this node""" result = "%s<!--" % (" " * indent) result += self._attribute['']