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() return "<MultiFS: %s>" % ", ".join(str(fs) for fs in self.fs_sequence)
try:
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() if name in self.fs_lookup:
try: raise ValueError("Name already exists.")
if name in self.fs_lookup:
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() if name not in self.fs_lookup:
try: raise ValueError("No filesystem called '%s'"%name)
if name not in self.fs_lookup: fs = self.fs_lookup[name]
raise ValueError("No filesystem called '%s'"%name) self.fs_sequence.remove(fs)
fs = self.fs_lookup[name] del self.fs_lookup[name]
self.fs_sequence.remove(fs)
del self.fs_lookup[name]
finally:
self._lock.release()
@synchronize
def __getitem__(self, name): def __getitem__(self, name):
self._lock.acquire() return self.fs_lookup[name]
try:
return self.fs_lookup[name]
finally:
self._lock.release()
@synchronize
def __iter__(self): def __iter__(self):
self._lock.acquire() return iter(self.fs_sequence[:])
try:
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,137 +78,98 @@ class MultiFS(FS): ...@@ -93,137 +78,98 @@ class MultiFS(FS):
path -- A path in MultiFS path -- A path in MultiFS
""" """
self._lock.acquire() for fs in self:
try: if fs.exists(path):
for fs in self: for fs_name, fs_object in self.fs_lookup.iteritems():
if fs.exists(path): if fs is fs_object:
for fs_name, fs_object in self.fs_lookup.iteritems(): return fs_name, fs
if fs is fs_object: raise ResourceNotFoundError(path, msg="Path does not map to any filesystem: %(path)s")
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): def getsyspath(self, path, allow_none=False):
self._lock.acquire() fs = self._delegate_search(path)
try: if fs is not None:
fs = self._delegate_search(path) return fs.getsyspath(path, allow_none=allow_none)
if fs is not None: raise ResourceNotFoundError(path)
return fs.getsyspath(path, allow_none=allow_none)
raise ResourceNotFoundError(path)
finally:
self._lock.release()
@synchronize
def desc(self, path): def desc(self, path):
self._lock.acquire() if not self.exists(path):
try: raise ResourceNotFoundError(path)
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()
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): def open(self, path, mode="r",**kwargs):
self._lock.acquire() for fs in self:
try: if fs.exists(path):
for fs in self: fs_file = fs.open(path, mode, **kwargs)
if fs.exists(path): return fs_file
fs_file = fs.open(path, mode, **kwargs)
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() return self._delegate_search(path) is not None
try:
return self._delegate_search(path) is not None
finally:
self._lock.release()
@synchronize
def isdir(self, path): def isdir(self, path):
self._lock.acquire() fs = self._delegate_search(path)
try: if fs is not None:
fs = self._delegate_search(path) return fs.isdir(path)
if fs is not None: return False
return fs.isdir(path)
return False
finally:
self._lock.release()
@synchronize
def isfile(self, path): def isfile(self, path):
self._lock.acquire() fs = self._delegate_search(path)
try: if fs is not None:
fs = self._delegate_search(path) return fs.isfile(path)
if fs is not None: return False
return fs.isfile(path)
return False
finally:
self._lock.release()
@synchronize
def listdir(self, path="./", *args, **kwargs): def listdir(self, path="./", *args, **kwargs):
self._lock.acquire() paths = []
try: for fs in self:
paths = [] try:
for fs in self: paths += fs.listdir(path, *args, **kwargs)
try: except FSError, e:
paths += fs.listdir(path, *args, **kwargs) pass
except FSError, e:
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() for fs in self:
try: if fs.exists(path):
for fs in self: fs.remove(path)
if fs.exists(path): return
fs.remove(path) raise ResourceNotFoundError(path)
return
raise ResourceNotFoundError(path)
finally:
self._lock.release()
@synchronize
def removedir(self, path, recursive=False): def removedir(self, path, recursive=False):
self._lock.acquire() for fs in self:
try: if fs.isdir(path):
for fs in self: fs.removedir(path, recursive)
if fs.isdir(path): return
fs.removedir(path, recursive) raise ResourceNotFoundError(path)
return
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() for fs in self:
try: if fs.exists(src):
for fs in self: fs.rename(src, dst)
if fs.exists(src): return
fs.rename(src, dst) raise ResourceNotFoundError(path)
return
raise ResourceNotFoundError(path)
finally:
self._lock.release()
@synchronize
def getinfo(self, path): def getinfo(self, path):
self._lock.acquire() for fs in self:
try: if fs.exists(path):
for fs in self: return fs.getinfo(path)
if fs.exists(path): raise ResourceNotFoundError(path)
return fs.getinfo(path)
raise ResourceNotFoundError(path)
finally:
self._lock.release()
...@@ -119,59 +119,49 @@ class ZipFS(FS): ...@@ -119,59 +119,49 @@ 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):
path = normpath(path)
self.zip_path = path
self._lock.acquire() if 'r' in mode:
try: if self.zip_mode not in 'ra':
path = normpath(path) raise OperationFailedError("open file", path=path, msg="Zip file must be opened for reading ('r') or appending ('a')")
self.zip_path = path try:
contents = self.zf.read(path)
if 'r' in mode: except KeyError:
if self.zip_mode not in 'ra': raise ResourceNotFoundError(path)
raise OperationFailedError("open file", path=path, msg="Zip file must be opened for reading ('r') or appending ('a')") return StringIO(contents)
try:
contents = self.zf.read(path)
except KeyError:
raise ResourceNotFoundError(path)
return StringIO(contents)
if 'w' in mode: if 'w' in mode:
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)
self._add_resource(path) self._add_resource(path)
f = _TempWriteFile(self.temp_fs, path, self._on_write_close) f = _TempWriteFile(self.temp_fs, path, self._on_write_close)
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() if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path)
try: try:
if not self.exists(path): contents = self.zf.read(path)
raise ResourceNotFoundError(path) except KeyError:
path = normpath(path) raise ResourceNotFoundError(path)
try: except RuntimeError:
contents = self.zf.read(path) raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read")
except KeyError: return contents
raise ResourceNotFoundError(path)
except RuntimeError: @synchronize
raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read")
return contents
finally:
self._lock.release()
def _on_write_close(self, filename): def _on_write_close(self, filename):
self._lock.acquire() sys_path = self.temp_fs.getsyspath(filename)
try: self.zf.write(sys_path, filename)
sys_path = self.temp_fs.getsyspath(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,40 +178,34 @@ class ZipFS(FS): ...@@ -188,40 +178,34 @@ 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() if not self.exists(path):
return ResourceNotFoundError(path)
path = normpath(path).lstrip('/')
try: try:
if not self.exists(path): zi = self.zf.getinfo(path)
return ResourceNotFoundError(path) zinfo = dict((attrib, getattr(zi, attrib)) for attrib in dir(zi) if not attrib.startswith('_'))
path = normpath(path).lstrip('/') except KeyError:
try: zinfo = {'file_size':0}
zi = self.zf.getinfo(path) info = {'size' : zinfo['file_size'] }
zinfo = dict((attrib, getattr(zi, attrib)) for attrib in dir(zi) if not attrib.startswith('_')) if 'date_time' in zinfo:
except KeyError: info['created_time'] = datetime.datetime(*zinfo['date_time'])
zinfo = {'file_size':0} info.update(zinfo)
info = {'size' : zinfo['file_size'] } return info
if 'date_time' in zinfo:
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