Skip to content

Instantly share code, notes, and snippets.

@avidal
Created October 15, 2012 15:22
Show Gist options
  • Save avidal/3893065 to your computer and use it in GitHub Desktop.
Save avidal/3893065 to your computer and use it in GitHub Desktop.
import subprocess
from StringIO import StringIO
class XmpFile(object):
def __init__(self, path, flags, *mode):
# Store the path as an instance property for later
self.path = "." + path
self.file = os.fdopen(open(self.path, flags, *mode),
flag2mode(flags))
self.fd = self.file.fileno()
def _find_patch(self):
# Tries to find a .rdiff file that exists next to self.file
# Returns the filename if one exists, otherwise returns None
if os.path.exists(self.path + ".rdiff"):
return self.path + ".rdiff"
return None
def read(self, length, offset):
patchfile = self._find_patch()
fileobj = self.file
# If a patchfile exists, apply it with rdiff and set the fileobj to the stdout
if patchfile:
stdout = subprocess.check_output(['rdiff', self.path, patchfile])
fileobj = StringIO(stdout)
fileobj.seek(offset)
return fileobj.read(length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment