Convert all warnings to strings
[0mirror.git] / xmltools.py
blobb2a5d5b1fc4935d7dae5c97075f5b3d69d8665f0
1 # Copyright (C) 2011, Thomas Leonard
2 # See the COPYING file for details, or visit http://0install.net.
4 from xml.dom import Node
6 def childNodes(parent, namespaceURI = None, localName = None):
7 for x in parent.childNodes:
8 if x.nodeType != Node.ELEMENT_NODE: continue
9 if namespaceURI is not None and x.namespaceURI != namespaceURI: continue
11 if localName is None or x.localName == localName:
12 yield x
14 def nodesEqual(a, b):
15 assert a.nodeType == Node.ELEMENT_NODE
16 assert b.nodeType == Node.ELEMENT_NODE
18 if a.namespaceURI != b.namespaceURI:
19 return False
21 if a.nodeName != b.nodeName:
22 return False
24 a_attrs = set(["%s %s" % (name, value) for name, value in a.attributes.itemsNS()])
25 b_attrs = set(["%s %s" % (name, value) for name, value in b.attributes.itemsNS()])
27 if a_attrs != b_attrs:
28 #print "%s != %s" % (a_attrs, b_attrs)
29 return False
31 a_children = list(childNodes(a))
32 b_children = list(childNodes(b))
34 if len(a_children) != len(b_children):
35 return False
37 for a_child, b_child in zip(a_children, b_children):
38 if not nodesEqual(a_child, b_child):
39 return False
41 return True