python-patch
python-patch copied to clipboard
Support patching strings
At the moment, only file patching is implemented. Refactor to allow string
patching.
Original issue reported on code.google.com by [email protected] on 25 Jul 2008 at 1:36
python-patch is a great idea. After struggling with the resolve function in the
difflib module i found this - thankfully.
Ideally a function 'patch' should be added to difflib and a new script
'patch.py'
created which calls difflib.patch() and should accompany the
Tools/scripts/diff.py
script.
Original comment by [email protected] on 24 Mar 2009 at 4:49
I also think it is great idea. =)
http://bugs.python.org/issue2057
Original comment by [email protected] on 25 Mar 2009 at 7:36
About patching strings. There is a low-level internal API that applies hunks to
stream, but it is not perfect, because you need to get hunks and apply them
explicitly. Patch file usually contain modifications for several files, each is
patched with a set of hunks and if you do not want to think about hunks - you
still
need to select which source file your string represents to select appropriate
hunk
set.
So, the API coud be like:
{{{
import patch
p = patch.fromfile("my.patch")
h = p.gethunks(filename="filename")
# or p.gethunks(index=0)
outstr = p.patchstream("my.file", h)
}}}
or it is also possible to add more high-level FileInfo() class.
{{{
import patch
p = patch.fromfile("my.patch")
files = p.getfiles()
output = files[0].patchstring(input)
}}}
Original comment by [email protected] on 25 Aug 2009 at 2:36
@adamchainz ^