Package zeroinstall :: Package support :: Module xmltools
[frames] | no frames]

Source Code for Module zeroinstall.support.xmltools

 1  """Convenience functions for handling XML. 
 2  @since: 1.9 
 3  """ 
 4   
 5  # Copyright (C) 2012, Thomas Leonard 
 6  # See the README file for details, or visit http://0install.net. 
 7   
 8  from xml.dom import Node 
 9                   
10 -def _compare_children(a, b):
11 """@rtype: bool""" 12 ac = a.childNodes 13 bc = b.childNodes 14 15 if ac.length != bc.length: 16 return False 17 18 for i in range(ac.length): 19 if not nodes_equal(ac[i], bc[i]): 20 return False 21 22 return True
23
24 -def nodes_equal(a, b):
25 """Compare two DOM nodes. 26 Warning: only supports documents containing elements, text nodes and attributes (will crash on comments, etc). 27 @rtype: bool""" 28 if a.nodeType != b.nodeType: 29 return False 30 31 if a.nodeType == Node.ELEMENT_NODE: 32 if a.namespaceURI != b.namespaceURI: 33 return False 34 35 if a.nodeName != b.nodeName: 36 return False 37 38 a_attrs = set([(name, value) for name, value in a.attributes.itemsNS()]) 39 b_attrs = set([(name, value) for name, value in b.attributes.itemsNS()]) 40 41 if a_attrs != b_attrs: 42 #print "%s != %s" % (a_attrs, b_attrs) 43 return False 44 45 return _compare_children(a, b) 46 elif a.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): 47 return a.wholeText == b.wholeText 48 elif a.nodeType == Node.DOCUMENT_NODE: 49 return _compare_children(a, b) 50 else: 51 assert 0, ("Unknown node type", a)
52