Commit 69aed4c7 by willmcgugan

Fixed meta form zip files, and added some more error handling

parent d4a910f9
...@@ -71,8 +71,8 @@ class ZipFS(FS): ...@@ -71,8 +71,8 @@ class ZipFS(FS):
_meta = { 'virtual' : False, _meta = { 'virtual' : False,
'read_only' : False, 'read_only' : False,
'unicode_paths' : os.path.supports_unicode_filenames, 'unicode_paths' : True,
'case_insensitive_paths' : os.path.normcase('Aa') == 'aa', 'case_insensitive_paths' : False,
} }
def __init__(self, zip_file, mode="r", compression="deflated", allow_zip_64=False, encoding="CP437", thread_synchronize=True): def __init__(self, zip_file, mode="r", compression="deflated", allow_zip_64=False, encoding="CP437", thread_synchronize=True):
...@@ -114,7 +114,6 @@ class ZipFS(FS): ...@@ -114,7 +114,6 @@ class ZipFS(FS):
details=ioe) details=ioe)
self.zip_path = str(zip_file) self.zip_path = str(zip_file)
self.temp_fs = None self.temp_fs = None
if mode in 'wa': if mode in 'wa':
self.temp_fs = tempfs.TempFS() self.temp_fs = tempfs.TempFS()
...@@ -123,7 +122,7 @@ class ZipFS(FS): ...@@ -123,7 +122,7 @@ class ZipFS(FS):
if mode in 'ra': if mode in 'ra':
self._parse_resource_list() self._parse_resource_list()
self._meta['read_only'] = self.zip_mode != 'w' self.read_only = mode == 'r'
def __str__(self): def __str__(self):
return "<ZipFS: %s>" % self.zip_path return "<ZipFS: %s>" % self.zip_path
...@@ -147,6 +146,12 @@ class ZipFS(FS): ...@@ -147,6 +146,12 @@ class ZipFS(FS):
f = self._path_fs.open(path, 'w') f = self._path_fs.open(path, 'w')
f.close() f.close()
def getmeta(self, meta_name, default=Ellipsis):
if meta_name == 'read_only':
return self.read_only
return super(ZipFS, self).getmeta(meta_name, default)
def close(self): def close(self):
"""Finalizes the zip file so that it can be read. """Finalizes the zip file so that it can be read.
No further operations will work after this method is called.""" No further operations will work after this method is called."""
...@@ -161,7 +166,9 @@ class ZipFS(FS): ...@@ -161,7 +166,9 @@ 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 file", path=path, msg="Zip file must be opened for reading ('r') or appending ('a')") raise OperationFailedError("open file",
path=path,
msg="Zip file must be opened for reading ('r') or appending ('a')")
try: try:
contents = self.zf.read(path.encode(self.encoding)) contents = self.zf.read(path.encode(self.encoding))
except KeyError: except KeyError:
...@@ -169,6 +176,10 @@ class ZipFS(FS): ...@@ -169,6 +176,10 @@ class ZipFS(FS):
return StringIO(contents) return StringIO(contents)
if 'w' in mode: if 'w' in mode:
if self.zip_mode not in 'wa':
raise OperationFailedError("open file",
path=path,
msg="Zip file must be opened for writing ('w') or appending ('a')")
dirname, filename = pathsplit(path) dirname, filename = pathsplit(path)
if dirname: if dirname:
self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True) self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True)
......
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