def compare_trees(tree1, tree2): """Generates an xmldiff from two lxml trees.""" opts = {'ratio_mode': 'accurate', 'F': 0.8} formatter = formatting.DiffFormatter(normalize=formatting.WS_BOTH, pretty_print=True) diff = diff_trees(tree1, tree2, diff_options=opts, formatter=formatter) return patch.DiffParser().parse(diff)
def patch_file(actions, tree): """Takes two filenames or streams, one with XML the other a diff""" tree = etree.parse(tree) if isinstance(actions, six.string_types): # It's a string, so it's a filename with open(actions) as f: actions = f.read() else: # We assume it's a stream actions = actions.read() actions = patch.DiffParser().parse(actions) tree = patch_tree(actions, tree) return etree.tounicode(tree)
def patch_text(actions, tree): """Takes a string with XML and a string with actions""" tree = etree.fromstring(tree) actions = patch.DiffParser().parse(actions) tree = patch_tree(actions, tree) return etree.tounicode(tree)