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()
__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.")
self.fs_sequence.append(fs)
self.fs_lookup[name] = fs
finally:
self._lock.release()
@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()
@synchronize
def __getitem__(self, name):
self._lock.acquire()
try:
return self.fs_lookup[name]
finally:
self._lock.release()
@synchronize
def __iter__(self):
self._lock.acquire()
try:
return iter(self.fs_sequence[:])
finally:
self._lock.release()
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,30 +78,22 @@ 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()
@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()
@synchronize
def desc(self, path):
self._lock.acquire()
try:
if not self.exists(path):
raise ResourceNotFoundError(path)
......@@ -124,52 +101,36 @@ class MultiFS(FS):
if name is None:
return ""
return "%s, on %s (%s)" % (fs.desc(path), name, fs)
finally:
self._lock.release()
@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
raise ResourceNotFoundError(path)
finally:
self._lock.release()
@synchronize
def exists(self, path):
self._lock.acquire()
try:
return self._delegate_search(path) is not None
finally:
self._lock.release()
@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()
@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()
@synchronize
def listdir(self, path="./", *args, **kwargs):
self._lock.acquire()
try:
paths = []
for fs in self:
try:
......@@ -178,52 +139,37 @@ class MultiFS(FS):
pass
return list(set(paths))
finally:
self._lock.release()
@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()
@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()
@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()
@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()
......@@ -119,10 +119,8 @@ class ZipFS(FS):
def __del__(self):
self.close()
@synchronize
def open(self, path, mode="r", **kwargs):
self._lock.acquire()
try:
path = normpath(path)
self.zip_path = path
......@@ -146,12 +144,9 @@ class ZipFS(FS):
return f
raise ValueError("Mode must contain be 'r' or 'w'")
finally:
self._lock.release()
@synchronize
def getcontents(self, path):
self._lock.acquire()
try:
if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path)
......@@ -162,16 +157,11 @@ class ZipFS(FS):
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()
@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()
def desc(self, path):
if self.isdir(path):
......@@ -188,26 +178,22 @@ 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:
@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)
finally:
self._lock.release()
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()
try:
if not self.exists(path):
return ResourceNotFoundError(path)
path = normpath(path).lstrip('/')
......@@ -221,7 +207,5 @@ class ZipFS(FS):
info['created_time'] = datetime.datetime(*zinfo['date_time'])
info.update(zinfo)
return info
finally:
self._lock.release()
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