Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/planemo/xml/diff.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
1 | |
2 def diff(x1, x2, reporter=None): | |
3 return 0 if xml_compare(x1, x2, reporter) else 1 | |
4 | |
5 | |
6 # From | |
7 # bitbucket.org/ianb/formencode/src/tip/formencode/doctest_xml_compare.py | |
8 # with (PSF license) | |
9 def xml_compare(x1, x2, reporter=None): | |
10 if reporter is None: | |
11 def reporter(x): | |
12 return None | |
13 | |
14 if x1.tag != x2.tag: | |
15 reporter('Tags do not match: %s and %s' % (x1.tag, x2.tag)) | |
16 return False | |
17 for name, value in x1.attrib.items(): | |
18 if x2.attrib.get(name) != value: | |
19 reporter('Attributes do not match: %s=%r, %s=%r' | |
20 % (name, value, name, x2.attrib.get(name))) | |
21 return False | |
22 for name in x2.attrib.keys(): | |
23 if name not in x1.attrib: | |
24 reporter('x2 has an attribute x1 is missing: %s' | |
25 % name) | |
26 return False | |
27 if not text_compare(x1.text, x2.text): | |
28 reporter('text: %r != %r' % (x1.text, x2.text)) | |
29 return False | |
30 if not text_compare(x1.tail, x2.tail): | |
31 reporter('tail: %r != %r' % (x1.tail, x2.tail)) | |
32 return False | |
33 return _compare_children(x1, x2, reporter) | |
34 | |
35 | |
36 def _compare_children(x1, x2, reporter): | |
37 cl1 = x1.getchildren() | |
38 cl2 = x2.getchildren() | |
39 if len(cl1) != len(cl2): | |
40 reporter('children length differs, %i != %i' | |
41 % (len(cl1), len(cl2))) | |
42 return False | |
43 i = 0 | |
44 for c1, c2 in zip(cl1, cl2): | |
45 i += 1 | |
46 if not xml_compare(c1, c2, reporter=reporter): | |
47 reporter('children %i do not match: %s' | |
48 % (i, c1.tag)) | |
49 return False | |
50 return True | |
51 | |
52 | |
53 def text_compare(t1, t2): | |
54 if not t1 and not t2: | |
55 return True | |
56 return (t1 or '').strip() == (t2 or '').strip() |