Created
October 15, 2012 15:22
-
-
Save avidal/3893065 to your computer and use it in GitHub Desktop.
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
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