def diff(source1, source2, dest, config): IDS = config["IDS"] REQUIRED_ATTRS = config["REQUIRED_ATTRS"] (fname1, need_cleanup1) = process_source(source1) (fname2, need_cleanup2) = process_source(source2) try: p = parser.Parser() parsed1 = p.parse_file(open(fname1)) p = parser.Parser() parsed2 = p.parse_file(open(fname2)) dest = open(dest, "w") diffed = parsed1.diff(parsed2, path="", ids=IDS, required=REQUIRED_ATTRS) del parsed1, parsed2 writer = xmlwriter.XMLWriter(dest, encoding="utf-8") writer.start_document() saxxml.diff_tree2xml(writer, diffed, required_attrs=REQUIRED_ATTRS) writer.end_document() except Exception: raise finally: if need_cleanup1: os.remove(fname1) if need_cleanup2: os.remove(fname2)
# at one pair attributes, output will contain also elements or attributes # from first source which are specified in REQUIRED_ATTRS. REQUIRED_ATTRS = {"group": ["id"], "category": ["id"], "environment": ["id"]} # Now do a diff thing. Path parameter is needed because diff method is called # recursively. So we start with empty path diffed = parsed1.diff(parsed2, path="", ids=IDS, required=REQUIRED_ATTRS) # Remove original structures, because they are changed and useless now. # And also because this example and whole tool is memory green. del parsed1, parsed2 writer = xmlwriter.XMLWriter(sys.stdout, encoding="utf-8") writer.start_document() # write output to stdout. In saxxml module is also method tree2xml # rendering ordinary xml. # So you can call - but not at this point becaused parsed1 is already deleted # saxxml.tree2xml(writer, parsed1) # to render xml from parsed structure saxxml.diff_tree2xml(writer, diffed, required_attrs=REQUIRED_ATTRS) writer.end_document()
# only elements and attributes which are different. But of course you want # to know at least id of group that contains different sub-elements. # REQUIRED_ATTRS ensure you, that if specified objects are different at least # at one pair attributes, output will contain also elements or attributes # from first source which are specified in REQUIRED_ATTRS. REQUIRED_ATTRS = {"group": ["id"], "category": ["id"], "environment": ["id"]} # Now do a diff thing. Path parameter is needed because diff method is called # recursively. So we start with empty path diffed = parsed1.diff(parsed2, path="", ids=IDS, required=REQUIRED_ATTRS) # Remove original structures, because they are changed and useless now. # And also because this example and whole tool is memory green. del parsed1, parsed2 writer = xmlwriter.XMLWriter(sys.stdout, encoding="utf-8") writer.start_document() # write output to stdout. In saxxml module is also method tree2xml # rendering ordinary xml. # So you can call - but not at this point becaused parsed1 is already deleted # saxxml.tree2xml(writer, parsed1) # to render xml from parsed structure saxxml.diff_tree2xml(writer, diffed, required_attrs=REQUIRED_ATTRS) writer.end_document()