Commit fb5a4cc1 by willmcgugan

Work in progress for the zip filesystem

parent 7f5736e6
......@@ -352,6 +352,21 @@ class FS(object):
if f is not None:
f.close()
def createfile(self, path, data):
"""A convenience method to create a new file from a string.
path -- Path of the file to create
data -- A string containing the contents of the file
"""
f = None
try:
f = self.open(path, 'wb')
f.write(data)
finally:
if f is not None:
f.close()
def opendir(self, path):
if not self.exists(path):
raise ResourceNotFoundError("NO_DIR", path)
......
......@@ -52,6 +52,9 @@ class ZipFS(FS):
self.temp_fs = tempfs.TempFS()
def close(self):
"""Finalizes the zip file so that it can be read.
No further operations will work after this method is called."""
if self.zf is not None:
self.zf.close()
self.zf = None
......@@ -66,7 +69,10 @@ class ZipFS(FS):
if 'r' in mode:
if self.zip_mode not in 'ra':
raise OperationFailedError("OPEN_FAILED", path=path, msg="Zip file must be opened for reading ('r') or appending ('a')")
contents = self.zf.read(path)
try:
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError("NO_FILE", path)
return StringIO(contents)
if 'w' in mode:
......@@ -87,12 +93,10 @@ class ZipFS(FS):
if __name__ == "__main__":
def test():
zfs = ZipFS("t.zip", "w")
f = zfs.open("t.txt", 'w')
f.write("Hello, World!")
f.close()
zfs.createfile("t.txt", "Hello, World!")
zfs.close()
rfs = ZipFS("t.zip", 'r')
print rfs.getcontents("t.txt")
print rfs.getcontents("w.txt")
test()
#zfs.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment