From 976040831c81dea5c8e0b8249ef6e681d7ee6d5c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 8 Mar 2019 13:34:00 +0000 Subject: Refactored, input files are now arguments --- xmldiff.py | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/xmldiff.py b/xmldiff.py index d0a5b08..3449053 100644 --- a/xmldiff.py +++ b/xmldiff.py @@ -1,38 +1,24 @@ #!/usr/bin/python import xml.etree.ElementTree +from sys import argv, exit -ours = xml.etree.ElementTree.parse('ours.xml').getroot().find('events') -theirs = xml.etree.ElementTree.parse('theirs.xml').getroot().find('events') +if len(argv) < 3: + print 'Usage: xmldiff.py ' + exit(1) -ours_set = set([x.find('handle').text for x in ours]) -theirs_set = set([x.find('handle').text for x in theirs]) - -only_ours = ours_set.difference(theirs_set) -only_theirs = theirs_set.difference(ours_set) - -ev_diff = ours_set.symmetric_difference(theirs_set) -ev_intersection = ours_set.intersection(theirs_set) - -print 'ours:' -print ' ' + str(len(ours)) + ' events in total' -print ' ' + str(len(only_ours)) + ' only here' - -print 'theirs:' -print ' ' + str(len(theirs)) + ' events in total' -print ' ' + str(len(only_theirs)) + ' only here' - -print 'in both: ' + str(len(ev_intersection)) -print - -def ensurekey(dictionary, key, init): +# Helper functions to reduce duplication +def ensureKey(dictionary, key, init): if key not in dictionary: dictionary[key] = init() def add(dictionary, key, value, index): - ensurekey(dictionary, key, lambda: [None, None]) + ensureKey(dictionary, key, lambda: [None, None]) dictionary[key][index] = value +ours = xml.etree.ElementTree.parse(argv[1]).getroot().find('events') +theirs = xml.etree.ElementTree.parse(argv[2]).getroot().find('events') + # Collect every event on either side byHandle = {} for e in ours: @@ -63,7 +49,7 @@ for key, value in byHandle.items(): match = True for sekey, sevalue in subelements.items(): - ensurekey(differences, key, list) + ensureKey(differences, key, list) if sevalue[0] is None: differences[key].append(sekey + ' only in ours') match = False -- cgit v1.2.3