-
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.
- Loading branch information
Showing
3 changed files
with
68 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
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,59 @@ | ||
from lxml import etree | ||
|
||
|
||
class Patcher(object): | ||
|
||
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)) |