Commit fb5a4cc1 by willmcgugan

Work in progress for the zip filesystem

parent 7f5736e6
...@@ -352,6 +352,21 @@ class FS(object): ...@@ -352,6 +352,21 @@ class FS(object):
if f is not None: if f is not None:
f.close() 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): def opendir(self, path):
if not self.exists(path): if not self.exists(path):
raise ResourceNotFoundError("NO_DIR", path) raise ResourceNotFoundError("NO_DIR", path)
......
...@@ -52,6 +52,9 @@ class ZipFS(FS): ...@@ -52,6 +52,9 @@ class ZipFS(FS):
self.temp_fs = tempfs.TempFS() self.temp_fs = tempfs.TempFS()
def close(self): 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: if self.zf is not None:
self.zf.close() self.zf.close()
self.zf = None self.zf = None
...@@ -66,7 +69,10 @@ class ZipFS(FS): ...@@ -66,7 +69,10 @@ class ZipFS(FS):
if 'r' in mode: if 'r' in mode:
if self.zip_mode not in 'ra': 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')") 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) return StringIO(contents)
if 'w' in mode: if 'w' in mode:
...@@ -87,12 +93,10 @@ class ZipFS(FS): ...@@ -87,12 +93,10 @@ class ZipFS(FS):
if __name__ == "__main__": if __name__ == "__main__":
def test(): def test():
zfs = ZipFS("t.zip", "w") zfs = ZipFS("t.zip", "w")
f = zfs.open("t.txt", 'w') zfs.createfile("t.txt", "Hello, World!")
f.write("Hello, World!")
f.close()
zfs.close() zfs.close()
rfs = ZipFS("t.zip", 'r') rfs = ZipFS("t.zip", 'r')
print rfs.getcontents("t.txt") print rfs.getcontents("t.txt")
print rfs.getcontents("w.txt")
test() test()
#zfs.close() #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