patchutils.py: Implement function to read patch from string buffer.

This commit is contained in:
Sebastian Lackner 2016-01-06 14:31:44 +01:00
parent 88ddac5ca3
commit 3f57e8259b

View File

@ -19,6 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
import cStringIO as StringIO
import collections
import difflib
import email.header
@ -85,11 +86,15 @@ class PatchObject(object):
i -= len(buf)
class _FileReader(object):
def __init__(self, filename):
def __init__(self, filename, content=None):
self.filename = filename
self.fp = open(self.filename)
self.peeked = None
if content is not None:
self.fp = StringIO.StringIO(content)
else:
self.fp = open(filename)
def close(self):
self.fp.close()
@ -128,7 +133,7 @@ class _FileReader(object):
tmp, self.peeked = self.peeked, None
return tmp[1]
def read_patch(filename):
def read_patch(filename, content=None):
"""Iterates over all patches contained in a file, and returns PatchObject objects."""
def _read_single_patch(fp, header, oldname=None, newname=None):
@ -308,7 +313,7 @@ def read_patch(filename):
return subject, 1
header = {}
with _FileReader(filename) as fp:
with _FileReader(filename, content) as fp:
while True:
line = fp.peek()
if line is None: