Skip to content
Snippets Groups Projects
Commit 0e910d37 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Adjust sorting from cmp= to key= syntax

parent 324a38be
No related branches found
No related tags found
No related merge requests found
...@@ -136,58 +136,6 @@ def xml_escape(s): ...@@ -136,58 +136,6 @@ def xml_escape(s):
return result 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: class ChildAccessor:
"""Helper class for iterating over xml trees """Helper class for iterating over xml trees
...@@ -374,14 +322,13 @@ class Node: ...@@ -374,14 +322,13 @@ class Node:
else: else:
return AttributeAccessor(self, attr) 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""" """Generate a prettyprinted xml of the tree rooted in this node"""
result = "" result = ""
line = "%s<%s" % (" " * indent, self._tag) line = "%s<%s" % (" " * indent, self._tag)
blanks = " " * len(line) blanks = " " * len(line)
for k in sorted(self._attribute.keys(), for k in sorted(self._attribute.keys(),
key=(attr_sort and AttrSortWrapper(self, attr_sort) key=attr_key and (lambda a: attr_key(self, a))):
or None)):
s = " %s='%s'" % (k, xml_escape(self._attribute[k])) s = " %s='%s'" % (k, xml_escape(self._attribute[k]))
if len(line) + len(s) > width: if len(line) + len(s) > width:
result += "%s\n" % line result += "%s\n" % line
...@@ -396,12 +343,9 @@ class Node: ...@@ -396,12 +343,9 @@ class Node:
result += '\n'.join(self._char) result += '\n'.join(self._char)
result += '\n' result += '\n'
pass pass
print("YYY", (tag_sort and TagSortWrapper(self, tag_sort)
or None), "\n")
for c in sorted(self._children, for c in sorted(self._children,
key=(tag_sort and TagSortWrapper(self, tag_sort) key=node_key and (lambda c: node_key(self, c))):
or None)): result += c._xml(indent + 1, attr_key, node_key, width)
result += c._xml(indent + 1, attr_sort, tag_sort, width)
result += "%s</%s>\n" % (" " * indent, self._tag) result += "%s</%s>\n" % (" " * indent, self._tag)
return result return result
...@@ -453,6 +397,9 @@ class Comment: ...@@ -453,6 +397,9 @@ class Comment:
self._tag = '' self._tag = ''
self._attribute = { '': content} self._attribute = { '': content}
def __lt__(self, other):
return self._tag < other._tag
def __copy__(self): def __copy__(self):
"""Copy self""" """Copy self"""
result = Comment(self._content, self._line) result = Comment(self._content, self._line)
...@@ -462,7 +409,7 @@ class Comment: ...@@ -462,7 +409,7 @@ class Comment:
"""Copy node and all children""" """Copy node and all children"""
return self.__copy__() 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""" """Generate a prettyprinted xml of the tree rooted in this node"""
result = "%s<!--" % (" " * indent) result = "%s<!--" % (" " * indent)
result += self._attribute[''] result += self._attribute['']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment