Commit 12ca3e2c by rfkelly0

consistently use @synchronize decorator throughout

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