comparison env/lib/python3.9/site-packages/planemo/xml/diff.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1
2 def diff(x1, x2, reporter=None):
3 """Return 0 if and only if the XML has the same content."""
4 compare = xml_compare(x1, x2, reporter)
5 return_val = 0 if compare else 1
6 return return_val
7
8
9 # From
10 # bitbucket.org/ianb/formencode/src/tip/formencode/doctest_xml_compare.py
11 # with (PSF license)
12 def xml_compare(x1, x2, reporter=None):
13 if reporter is None:
14 def reporter(x):
15 return None
16
17 if x1.tag != x2.tag:
18 reporter('Tags do not match: %s and %s\n' % (x1.tag, x2.tag))
19 return False
20 for name, value in x1.attrib.items():
21 if x2.attrib.get(name) != value:
22 reporter('Attributes do not match: %s=%r, %s=%r\n'
23 % (name, value, name, x2.attrib.get(name)))
24 return False
25 for name in x2.attrib.keys():
26 if name not in x1.attrib:
27 reporter('x2 has an attribute x1 is missing: %s\n'
28 % name)
29 return False
30 if not text_compare(x1.text, x2.text):
31 reporter('text: %r != %r\n' % (x1.text, x2.text))
32 return False
33 if not text_compare(x1.tail, x2.tail):
34 reporter('tail: %r != %r\n' % (x1.tail, x2.tail))
35 return False
36 return _compare_children(x1, x2, reporter)
37
38
39 def _compare_children(x1, x2, reporter):
40 cl1 = list(x1)
41 cl2 = list(x2)
42 if len(cl1) != len(cl2):
43 reporter('children length differs, %i != %i\n'
44 % (len(cl1), len(cl2)))
45 return False
46 i = 0
47 for c1, c2 in zip(cl1, cl2):
48 i += 1
49 if not xml_compare(c1, c2, reporter=reporter):
50 reporter('children %i do not match: %s\n'
51 % (i, c1.tag))
52 return False
53 return True
54
55
56 def text_compare(t1, t2):
57 if not t1 and not t2:
58 return True
59 return (t1 or '').strip() == (t2 or '').strip()