-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A Patcher class that can apply patches
- Loading branch information
Showing
4 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import unittest | ||
|
||
from lxml import etree | ||
from xmldiff.main import diff_trees | ||
from xmldiff.patch import Patcher | ||
from xmldiff.actions import (UpdateTextIn, InsertNode, MoveNode, | ||
DeleteNode, UpdateAttrib, InsertAttrib, | ||
RenameAttrib, DeleteAttrib, UpdateTextAfter, | ||
RenameNode, InsertComment) | ||
|
||
from .testing import compare_elements | ||
|
||
|
||
class PatcherTests(unittest.TestCase): | ||
|
||
patcher = Patcher() | ||
|
||
def _test(self, start, action, end): | ||
tree = etree.fromstring(start) | ||
self.patcher.handle_action(action, tree) | ||
self.assertEqual(etree.tounicode(tree), end) | ||
|
||
def test_delete_node(self): | ||
self._test('<root><deleteme/></root>', | ||
DeleteNode('/root/deleteme'), | ||
'<root/>') | ||
|
||
def test_insert_node(self): | ||
self._test('<root><anode/></root>', | ||
InsertNode('/root/anode', 'newnode', 0), | ||
'<root><anode><newnode/></anode></root>') | ||
|
||
def test_rename_node(self): | ||
self._test('<root><oldname/></root>', | ||
RenameNode('/root/oldname', 'newname'), | ||
'<root><newname/></root>') | ||
|
||
def test_move_node(self): | ||
self._test('<root><anode><moveme/></anode></root>', | ||
MoveNode('/root/anode/moveme', '/root', 1), | ||
'<root><anode/><moveme/></root>') | ||
|
||
def test_update_text_in(self): | ||
self._test('<root><anode/></root>', | ||
UpdateTextIn('/root/anode', 'New text'), | ||
'<root><anode>New text</anode></root>') | ||
|
||
def test_update_text_after(self): | ||
self._test('<root><anode/></root>', | ||
UpdateTextAfter('/root/anode', 'New text'), | ||
'<root><anode/>New text</root>') | ||
|
||
def test_update_attrib(self): | ||
self._test('<root><anode attrib="oldvalue" /></root>', | ||
UpdateAttrib('/root/anode', 'attrib', 'newvalue'), | ||
'<root><anode attrib="newvalue"/></root>') | ||
|
||
def test_delete_attrib(self): | ||
self._test('<root><anode attrib="oldvalue" /></root>', | ||
DeleteAttrib('/root/anode', 'attrib'), | ||
'<root><anode/></root>') | ||
|
||
def test_insert_attrib(self): | ||
self._test('<root><anode/></root>', | ||
InsertAttrib('/root/anode', 'attrib', 'value'), | ||
'<root><anode attrib="value"/></root>') | ||
|
||
def test_rename_attrib(self): | ||
self._test('<root><anode oldname="value"/></root>', | ||
RenameAttrib('/root/anode', 'oldname', 'newname'), | ||
'<root><anode newname="value"/></root>') | ||
|
||
def test_insert_comment(self): | ||
self._test('<root><anode/></root>', | ||
InsertComment('/root', 1, "This is a new comment"), | ||
'<root><anode/><!--This is a new comment--></root>') | ||
|
||
|
||
class DiffPatch(unittest.TestCase): | ||
|
||
def test_diff_patch(self): | ||
here = os.path.split(__file__)[0] | ||
lfile = os.path.join(here, 'test_data', 'all_actions.left.xml') | ||
rfile = os.path.join(here, 'test_data', 'all_actions.right.xml') | ||
|
||
left = etree.parse(lfile) | ||
right = etree.parse(rfile) | ||
diff = diff_trees(left, right) | ||
result = Patcher().patch(diff, left) | ||
|
||
# This example has top level comments, and lxml doesn't deal well | ||
# with that, so the trees are not EXACTLY the same, the trailing | ||
# top level comment differs, but that's OK. | ||
compare_elements(result.getroot(), right.getroot()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from copy import deepcopy | ||
from lxml import etree | ||
|
||
|
||
class Patcher(object): | ||
|
||
def patch(self, actions, tree): | ||
# Copy the tree so we don't modify the original | ||
result = deepcopy(tree) | ||
|
||
for action in actions: | ||
self.handle_action(action, result) | ||
|
||
return result | ||
|
||
def handle_action(self, action, tree): | ||
action_type = type(action) | ||
method = getattr(self, '_handle_' + action_type.__name__) | ||
method(action, tree) | ||
|
||
def _handle_DeleteNode(self, action, tree): | ||
node = tree.xpath(action.node)[0] | ||
node.getparent().remove(node) | ||
|
||
def _handle_InsertNode(self, action, tree): | ||
target = tree.xpath(action.target)[0] | ||
node = target.makeelement(action.tag) | ||
target.insert(action.position, node) | ||
|
||
def _handle_RenameNode(self, action, tree): | ||
tree.xpath(action.node)[0].tag = action.tag | ||
|
||
def _handle_MoveNode(self, action, tree): | ||
node = tree.xpath(action.node)[0] | ||
node.getparent().remove(node) | ||
target = tree.xpath(action.target)[0] | ||
target.insert(action.position, node) | ||
|
||
def _handle_UpdateTextIn(self, action, tree): | ||
tree.xpath(action.node)[0].text = action.text | ||
|
||
def _handle_UpdateTextAfter(self, action, tree): | ||
tree.xpath(action.node)[0].tail = action.text | ||
|
||
def _handle_UpdateAttrib(self, action, tree): | ||
node = tree.xpath(action.node)[0] | ||
# This should not be used to insert new attributes. | ||
assert action.name in node.attrib | ||
node.attrib[action.name] = action.value | ||
|
||
def _handle_DeleteAttrib(self, action, tree): | ||
del tree.xpath(action.node)[0].attrib[action.name] | ||
|
||
def _handle_InsertAttrib(self, action, tree): | ||
node = tree.xpath(action.node)[0] | ||
# This should not be used to update existing attributes. | ||
assert action.name not in node.attrib | ||
node.attrib[action.name] = action.value | ||
|
||
def _handle_RenameAttrib(self, action, tree): | ||
node = tree.xpath(action.node)[0] | ||
assert action.oldname in node.attrib | ||
assert action.newname not in node.attrib | ||
node.attrib[action.newname] = node.attrib[action.oldname] | ||
del node.attrib[action.oldname] | ||
|
||
def _handle_InsertComment(self, action, tree): | ||
target = tree.xpath(action.target)[0] | ||
target.insert(action.position, etree.Comment(action.text)) |