Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch support #35

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changes
2.3 (unreleased)
----------------

- Nothing changed yet.
- Added a simple ``xmlpatch`` command and API.


2.2 (2018-10-12)
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ To use it from the command-line, just run ``xmldiff`` with two input files::

$ xmldiff file1.xml file2.xml

As a library::
There is also a command to patch a file with the output from the ``xmldiff`` command::

$ xmldiff file.diff file1.xml

There is a simple API for using ``xmldiff`` as a library::

from lxml import etree
from xmldiff import main, formatting
Expand All @@ -40,6 +44,9 @@ As a library::

There is also a method ``diff_trees()`` that take two lxml trees,
and a method ``diff_texts()`` that will take strings containing XML.
Similarily, there is ``patch_file()`` ``patch_text()`` and ``patch_tree()``::

result = main.diff_file('file.diff', 'file1.xml')


Changes from ``xmldiff`` 0.6/1.x
Expand Down
38 changes: 35 additions & 3 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Python API
==========

Main API
--------
Main diffing API
----------------

Using ``xmldiff`` from Python is very easy,
you just import and call one of the three main API methods.
Expand Down Expand Up @@ -85,7 +85,6 @@ Parameters
If no formatter is specified the function will return a list of edit actions,
see `The Edit Script`_.


Result
......

Expand Down Expand Up @@ -435,3 +434,36 @@ Example:
>>> right = '<document><!-- A comment --><node>Content</node></document>'
>>> main.diff_texts(left, right)
[InsertComment(target='/document[1]', position=0, text=' A comment ')]


The patching API
----------------

There is also an API to patch files using the diff output:

.. doctest::
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE

>>> from xmldiff import main
>>> print(main.patch_file("../tests/test_data/insert-node.diff",
... "../tests/test_data/insert-node.left.html"))
<body>
<div id="id">
<p>Simple text</p>
</div>
</body>

On the same line as for the patch API there are three methods:

* ``xmldiff.main.patch_file()`` takes as input paths to files, or file streams,
and returns a string with the resulting XML.

* ``xmldiff.main.patch_text()`` takes as input Unicode strings,
and returns a string with the resulting XML.

* ``xmldiff.main.patch_tree()`` takes as input one edit script,
(ie a list of actions, see above) and one ``lxml`` tree,
and returnes a patched ``lxml`` tree.

They all return a string with the patched XML tree.
There are currently no configuration parameters for these commands.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
test_suite='tests',
entry_points={
'console_scripts': [
'xmldiff = xmldiff.main:run',
'xmldiff = xmldiff.main:diff_command',
'xmlpatch = xmldiff.main:patch_command',
],
},
)
4 changes: 4 additions & 0 deletions tests/test_data/insert-node.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[update-text, /body/div[1], "\n "]
[insert, /body/div[1], p, 0]
[update-text, /body/div/p[1], "Simple text"]
[update-text-after, /body/div/p[1], "\n "]
9 changes: 5 additions & 4 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from io import open
from lxml import etree
from xmldiff import utils
from xmldiff.diff import (Differ, UpdateTextIn, InsertNode, MoveNode,
DeleteNode, UpdateAttrib, InsertAttrib, RenameAttrib,
DeleteAttrib, UpdateTextAfter, RenameNode,
InsertComment)
from xmldiff.diff import Differ
from xmldiff.actions import (UpdateTextIn, InsertNode, MoveNode,
DeleteNode, UpdateAttrib, InsertAttrib,
RenameAttrib, DeleteAttrib, UpdateTextAfter,
RenameNode, InsertComment)

from .testing import compare_elements

Expand Down
Loading