Commit 12ca3e2c by rfkelly0

consistently use @synchronize decorator throughout

parent 839a1f68
......@@ -19,12 +19,9 @@ class MultiFS(FS):
self.fs_sequence = []
self.fs_lookup = {}
@synchronize
def __str__(self):
self._lock.acquire()
try:
return "<MultiFS: %s>" % ", ".join(str(fs) for fs in self.fs_sequence)
finally:
self._lock.release()
return "<MultiFS: %s>" % ", ".join(str(fs) for fs in self.fs_sequence)
__repr__ = __str__
......@@ -32,6 +29,7 @@ class MultiFS(FS):
return unicode(self.__str__())
@synchronize
def addfs(self, name, fs):
"""Adds a filesystem to the MultiFS.
......@@ -39,46 +37,32 @@ class MultiFS(FS):
fs -- The filesystem to add
"""
self._lock.acquire()
try:
if name in self.fs_lookup:
raise ValueError("Name already exists.")
if name in self.fs_lookup:
raise ValueError("Name already exists.")
self.fs_sequence.append(fs)
self.fs_lookup[name] = fs
finally:
self._lock.release()
self.fs_sequence.append(fs)
self.fs_lookup[name] = fs
@synchronize
def removefs(self, name):
"""Removes a filesystem from the sequence.
name -- The name of the filesystem, as used in addfs
"""
self._lock.acquire()
try:
if name not in self.fs_lookup:
raise ValueError("No filesystem called '%s'"%name)
fs = self.fs_lookup[name]
self.fs_sequence.remove(fs)
del self.fs_lookup[name]
finally:
self._lock.release()
if name not in self.fs_lookup:
raise ValueError("No filesystem called '%s'"%name)
fs = self.fs_lookup[name]
self.fs_sequence.remove(fs)
del self.fs_lookup[name]
@synchronize
def __getitem__(self, name):
self._lock.acquire()
try:
return self.fs_lookup[name]
finally:
self._lock.release()
return self.fs_lookup[name]
@synchronize
def __iter__(self):
self._lock.acquire()
try:
return iter(self.fs_sequence[:])
finally:
self._lock.release()
return iter(self.fs_sequence[:])
def _delegate_search(self, path):
for fs in self:
......@@ -86,6 +70,7 @@ class MultiFS(FS):
return fs
return None
@synchronize
def which(self, path):
"""Retrieves the filesystem that a given path would delegate to.
Returns a tuple of the filesystem's name and the filesystem object itself.
......@@ -93,137 +78,98 @@ class MultiFS(FS):
path -- A path in MultiFS
"""
self._lock.acquire()
try:
for fs in self:
if fs.exists(path):
for fs_name, fs_object in self.fs_lookup.iteritems():
if fs is fs_object:
return fs_name, fs
raise ResourceNotFoundError(path, msg="Path does not map to any filesystem: %(path)s")
finally:
self._lock.release()
for fs in self:
if fs.exists(path):
for fs_name, fs_object in self.fs_lookup.iteritems():
if fs is fs_object:
return fs_name, fs
raise ResourceNotFoundError(path, msg="Path does not map to any filesystem: %(path)s")
@synchronize
def getsyspath(self, path, allow_none=False):
self._lock.acquire()
try:
fs = self._delegate_search(path)
if fs is not None:
return fs.getsyspath(path, allow_none=allow_none)
raise ResourceNotFoundError(path)
finally:
self._lock.release()
fs = self._delegate_search(path)
if fs is not None:
return fs.getsyspath(path, allow_none=allow_none)
raise ResourceNotFoundError(path)
@synchronize
def desc(self, path):
self._lock.acquire()
try:
if not self.exists(path):
raise ResourceNotFoundError(path)
name, fs = self.which(path)
if name is None:
return ""
return "%s, on %s (%s)" % (fs.desc(path), name, fs)
finally:
self._lock.release()
if not self.exists(path):
raise ResourceNotFoundError(path)
name, fs = self.which(path)
if name is None:
return ""
return "%s, on %s (%s)" % (fs.desc(path), name, fs)
@synchronize
def open(self, path, mode="r",**kwargs):
self._lock.acquire()
try:
for fs in self:
if fs.exists(path):
fs_file = fs.open(path, mode, **kwargs)
return fs_file
for fs in self:
if fs.exists(path):
fs_file = fs.open(path, mode, **kwargs)
return fs_file
raise ResourceNotFoundError(path)
finally:
self._lock.release()
raise ResourceNotFoundError(path)
@synchronize
def exists(self, path):
self._lock.acquire()
try:
return self._delegate_search(path) is not None
finally:
self._lock.release()
return self._delegate_search(path) is not None
@synchronize
def isdir(self, path):
self._lock.acquire()
try:
fs = self._delegate_search(path)
if fs is not None:
return fs.isdir(path)
return False
finally:
self._lock.release()
fs = self._delegate_search(path)
if fs is not None:
return fs.isdir(path)
return False
@synchronize
def isfile(self, path):
self._lock.acquire()
try:
fs = self._delegate_search(path)
if fs is not None:
return fs.isfile(path)
return False
finally:
self._lock.release()
fs = self._delegate_search(path)
if fs is not None:
return fs.isfile(path)
return False
@synchronize
def listdir(self, path="./", *args, **kwargs):
self._lock.acquire()
try:
paths = []
for fs in self:
try:
paths += fs.listdir(path, *args, **kwargs)
except FSError, e:
pass
return list(set(paths))
finally:
self._lock.release()
paths = []
for fs in self:
try:
paths += fs.listdir(path, *args, **kwargs)
except FSError, e:
pass
return list(set(paths))
@synchronize
def remove(self, path):
self._lock.acquire()
try:
for fs in self:
if fs.exists(path):
fs.remove(path)
return
raise ResourceNotFoundError(path)
finally:
self._lock.release()
for fs in self:
if fs.exists(path):
fs.remove(path)
return
raise ResourceNotFoundError(path)
@synchronize
def removedir(self, path, recursive=False):
self._lock.acquire()
try:
for fs in self:
if fs.isdir(path):
fs.removedir(path, recursive)
return
raise ResourceNotFoundError(path)
finally:
self._lock.release()
for fs in self:
if fs.isdir(path):
fs.removedir(path, recursive)
return
raise ResourceNotFoundError(path)
@synchronize
def rename(self, src, dst):
if not issamedir(src, dst):
raise ValueError("Destination path must the same directory (use the move method for moving to a different directory)")
self._lock.acquire()
try:
for fs in self:
if fs.exists(src):
fs.rename(src, dst)
return
raise ResourceNotFoundError(path)
finally:
self._lock.release()
for fs in self:
if fs.exists(src):
fs.rename(src, dst)
return
raise ResourceNotFoundError(path)
@synchronize
def getinfo(self, path):
self._lock.acquire()
try:
for fs in self:
if fs.exists(path):
return fs.getinfo(path)
raise ResourceNotFoundError(path)
finally:
self._lock.release()
for fs in self:
if fs.exists(path):
return fs.getinfo(path)
raise ResourceNotFoundError(path)
......@@ -119,59 +119,49 @@ class ZipFS(FS):
def __del__(self):
self.close()
@synchronize
def open(self, path, mode="r", **kwargs):
path = normpath(path)
self.zip_path = path
self._lock.acquire()
try:
path = normpath(path)
self.zip_path = path
if 'r' in mode:
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')")
try:
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError(path)
return StringIO(contents)
if 'r' in mode:
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')")
try:
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError(path)
return StringIO(contents)
if 'w' in mode:
dirname, filename = pathsplit(path)
if dirname:
self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True)
if 'w' in mode:
dirname, filename = pathsplit(path)
if dirname:
self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True)
self._add_resource(path)
f = _TempWriteFile(self.temp_fs, path, self._on_write_close)
self._add_resource(path)
f = _TempWriteFile(self.temp_fs, path, self._on_write_close)
return f
return f
raise ValueError("Mode must contain be 'r' or 'w'")
finally:
self._lock.release()
raise ValueError("Mode must contain be 'r' or 'w'")
@synchronize
def getcontents(self, path):
self._lock.acquire()
if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path)
try:
if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path)
try:
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError(path)
except RuntimeError:
raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read")
return contents
finally:
self._lock.release()
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError(path)
except RuntimeError:
raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read")
return contents
@synchronize
def _on_write_close(self, filename):
self._lock.acquire()
try:
sys_path = self.temp_fs.getsyspath(filename)
self.zf.write(sys_path, filename)
finally:
self._lock.release()
sys_path = self.temp_fs.getsyspath(filename)
self.zf.write(sys_path, filename)
def desc(self, path):
if self.isdir(path):
......@@ -188,40 +178,34 @@ class ZipFS(FS):
def exists(self, path):
return self._path_fs.exists(path)
def makedir(self, dirname, mode=0777, recursive=False, allow_recreate=False):
self._lock.acquire()
try:
dirname = normpath(dirname)
if self.zip_mode not in "wa":
raise OperationFailedError("create directory", path=dirname, msg="Zip file must be opened for writing ('w') or appending ('a')")
if not dirname.endswith('/'):
dirname += '/'
self._add_resource(dirname)
finally:
self._lock.release()
@synchronize
def makedir(self, dirname, recursive=False, allow_recreate=False):
dirname = normpath(dirname)
if self.zip_mode not in "wa":
raise OperationFailedError("create directory", path=dirname, msg="Zip file must be opened for writing ('w') or appending ('a')")
if not dirname.endswith('/'):
dirname += '/'
self._add_resource(dirname)
def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
return self._path_fs.listdir(path, wildcard, full, absolute, dirs_only, files_only)
@synchronize
def getinfo(self, path):
self._lock.acquire()
if not self.exists(path):
return ResourceNotFoundError(path)
path = normpath(path).lstrip('/')
try:
if not self.exists(path):
return ResourceNotFoundError(path)
path = normpath(path).lstrip('/')
try:
zi = self.zf.getinfo(path)
zinfo = dict((attrib, getattr(zi, attrib)) for attrib in dir(zi) if not attrib.startswith('_'))
except KeyError:
zinfo = {'file_size':0}
info = {'size' : zinfo['file_size'] }
if 'date_time' in zinfo:
info['created_time'] = datetime.datetime(*zinfo['date_time'])
info.update(zinfo)
return info
finally:
self._lock.release()
zi = self.zf.getinfo(path)
zinfo = dict((attrib, getattr(zi, attrib)) for attrib in dir(zi) if not attrib.startswith('_'))
except KeyError:
zinfo = {'file_size':0}
info = {'size' : zinfo['file_size'] }
if 'date_time' in zinfo:
info['created_time'] = datetime.datetime(*zinfo['date_time'])
info.update(zinfo)
return info
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