Commit ee47991d by willmcgugan@gmail.com

Added closing context manager to files returned by open

parent 0dc13500
......@@ -8,6 +8,7 @@ A FS object that represents the contents of a Zip file
import datetime
import os.path
from contextlib import closing
from fs.base import *
from fs.path import *
......@@ -32,7 +33,6 @@ class ZipNotFoundError(CreateFailedError):
class _TempWriteFile(object):
"""Proxies a file object and calls a callback when the file is closed."""
def __init__(self, fs, filename, close_callback):
......@@ -54,9 +54,14 @@ class _TempWriteFile(object):
def flush(self):
self._file.flush()
def __enter__(self):
return self
class _ExceptionProxy(object):
def __exit__(self, type, value, traceback):
self.close()
class _ExceptionProxy(object):
"""A placeholder for an object that may no longer be used."""
def __getattr__(self, name):
......@@ -70,7 +75,6 @@ class _ExceptionProxy(object):
class ZipFS(FS):
"""A FileSystem that represents a zip file."""
_meta = { 'thread_safe' : True,
......@@ -177,7 +181,6 @@ class ZipFS(FS):
return self.read_only
return super(ZipFS, self).getmeta(meta_name, default)
def close(self):
"""Finalizes the zip file so that it can be read.
No further operations will work after this method is called."""
......@@ -202,7 +205,7 @@ class ZipFS(FS):
contents = self.zf.read(self._encode_path(path))
except KeyError:
raise ResourceNotFoundError(path)
return StringIO(contents)
return closing(StringIO(contents))
if 'w' in mode:
if self.zip_mode not in 'wa':
......@@ -215,7 +218,6 @@ class ZipFS(FS):
self._add_resource(path)
f = _TempWriteFile(self.temp_fs, path, self._on_write_close)
return f
raise ValueError("Mode must contain be 'r' or 'w'")
......
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