diff options
author | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
---|---|---|
committer | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
commit | 3ea4efe11b1a7f31c355103608950379d52fdec5 (patch) | |
tree | 5547f10f29e28894ae70ff5b97e858456f94103a /fs/multifs.py | |
parent | c6391b6f5dcf2a30d4657b5808f9f668fbc02de3 (diff) | |
download | pyfilesystem-git-3ea4efe11b1a7f31c355103608950379d52fdec5.tar.gz |
Change of api (fs.open, fs.setcontent, fs.getcontents) to support io module in Py2.6+ and Py3
Diffstat (limited to 'fs/multifs.py')
-rw-r--r-- | fs/multifs.py | 84 |
1 files changed, 42 insertions, 42 deletions
diff --git a/fs/multifs.py b/fs/multifs.py index 227e0d3..cac6cf5 100644 --- a/fs/multifs.py +++ b/fs/multifs.py @@ -15,17 +15,17 @@ to *theme* a web application. We start with the following directories:: `-- templates |-- snippets - | `-- panel.html + | `-- panel.html |-- index.html |-- profile.html `-- base.html - + `-- theme |-- snippets | |-- widget.html | `-- extra.html |-- index.html - `-- theme.html + `-- theme.html And we want to create a single filesystem that looks for files in `templates` if they don't exist in `theme`. We can do this with the following code:: @@ -36,29 +36,29 @@ they don't exist in `theme`. We can do this with the following code:: themed_template_fs.addfs('templates', OSFS('templates')) themed_template_fs.addfs('theme', OSFS('themes')) - -Now we have a `themed_template_fs` FS object presents a single view of both -directories:: - + +Now we have a `themed_template_fs` FS object presents a single view of both +directories:: + |-- snippets | |-- panel.html | |-- widget.html | `-- extra.html - |-- index.html + |-- index.html |-- profile.html |-- base.html `-- theme.html A MultiFS is generally read-only, and any operation that may modify data -(including opening files for writing) will fail. However, you can set a +(including opening files for writing) will fail. However, you can set a writeable fs with the `setwritefs` method -- which does not have to be one of the FS objects set with `addfs`. The reason that only one FS object is ever considered for write access is that otherwise it would be ambiguous as to which filesystem you would want to modify. If you need to be able to modify more than one FS in the MultiFS, -you can always access them directly. +you can always access them directly. """ @@ -76,7 +76,7 @@ class MultiFS(FS): it succeeds. In effect, creating a filesystem that combines the files and dirs of its children. """ - + _meta = { 'virtual': True, 'read_only' : False, 'unicode_paths' : True, @@ -85,9 +85,9 @@ class MultiFS(FS): def __init__(self, auto_close=True): """ - - :param auto_close: If True the child filesystems will be closed when the MultiFS is closed - + + :param auto_close: If True the child filesystems will be closed when the MultiFS is closed + """ super(MultiFS, self).__init__(thread_synchronize=_thread_synchronize_default) @@ -95,7 +95,7 @@ class MultiFS(FS): self.fs_sequence = [] self.fs_lookup = {} self.fs_priorities = {} - self.writefs = None + self.writefs = None @synchronize def __str__(self): @@ -117,19 +117,19 @@ class MultiFS(FS): for fs in self.fs_sequence: fs.close() if self.writefs is not None: - self.writefs.close() + self.writefs.close() # Discard any references del self.fs_sequence[:] self.fs_lookup.clear() self.fs_priorities.clear() self.writefs = None super(MultiFS, self).close() - + def _priority_sort(self): """Sort filesystems by priority order""" priority_order = sorted(self.fs_lookup.keys(), key=lambda n:self.fs_priorities[n], reverse=True) - self.fs_sequence = [self.fs_lookup[name] for name in priority_order] - + self.fs_sequence = [self.fs_lookup[name] for name in priority_order] + @synchronize def addfs(self, name, fs, write=False, priority=0): """Adds a filesystem to the MultiFS. @@ -141,19 +141,19 @@ class MultiFS(FS): :param priority: A number that gives the priorty of the filesystem being added. Filesystems will be searched in descending priority order and then by the reverse order they were added. So by default, the most recently added filesystem will be looked at first - + """ if name in self.fs_lookup: raise ValueError("Name already exists.") - + priority = (priority, len(self.fs_sequence)) self.fs_priorities[name] = priority self.fs_sequence.append(fs) self.fs_lookup[name] = fs - - self._priority_sort() - + + self._priority_sort() + if write: self.setwritefs(fs) @@ -162,16 +162,16 @@ class MultiFS(FS): """Sets the filesystem to use when write access is required. Without a writeable FS, any operations that could modify data (including opening files for writing / appending) will fail. - + :param fs: An FS object that will be used to open writeable files - + """ - self.writefs = fs - - @synchronize + self.writefs = fs + + @synchronize def clearwritefs(self): """Clears the writeable filesystem (operations that modify the multifs will fail)""" - self.writefs = None + self.writefs = None @synchronize def removefs(self, name): @@ -209,7 +209,7 @@ class MultiFS(FS): :param path: A path in MultiFS """ - if 'w' in mode or '+' in mode or 'a' in mode: + if 'w' in mode or '+' in mode or 'a' in mode: return self.writefs for fs in self: if fs.exists(path): @@ -238,14 +238,14 @@ class MultiFS(FS): return "%s, on %s (%s)" % (fs.desc(path), name, fs) @synchronize - def open(self, path, mode="r", **kwargs): + def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): if 'w' in mode or '+' in mode or 'a' in mode: if self.writefs is None: raise OperationFailedError('open', path=path, msg="No writeable FS set") - return self.writefs.open(path, mode) + return self.writefs.open(path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, line_buffering=line_buffering, **kwargs) for fs in self: if fs.exists(path): - fs_file = fs.open(path, mode, **kwargs) + fs_file = fs.open(path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, line_buffering=line_buffering, **kwargs) return fs_file raise ResourceNotFoundError(path) @@ -280,34 +280,34 @@ class MultiFS(FS): @synchronize def makedir(self, path, recursive=False, allow_recreate=False): if self.writefs is None: - raise OperationFailedError('makedir', path=path, msg="No writeable FS set") - self.writefs.makedir(path, recursive=recursive, allow_recreate=allow_recreate) + raise OperationFailedError('makedir', path=path, msg="No writeable FS set") + self.writefs.makedir(path, recursive=recursive, allow_recreate=allow_recreate) @synchronize def remove(self, path): if self.writefs is None: - raise OperationFailedError('remove', path=path, msg="No writeable FS set") - self.writefs.remove(path) + raise OperationFailedError('remove', path=path, msg="No writeable FS set") + self.writefs.remove(path) @synchronize def removedir(self, path, recursive=False, force=False): if self.writefs is None: raise OperationFailedError('removedir', path=path, msg="No writeable FS set") if normpath(path) in ('', '/'): - raise RemoveRootError(path) - self.writefs.removedir(path, recursive=recursive, force=force) + raise RemoveRootError(path) + self.writefs.removedir(path, recursive=recursive, force=force) @synchronize def rename(self, src, dst): if self.writefs is None: raise OperationFailedError('rename', path=src, msg="No writeable FS set") - self.writefs.rename(src, dst) + self.writefs.rename(src, dst) @synchronize def settimes(self, path, accessed_time=None, modified_time=None): if self.writefs is None: raise OperationFailedError('settimes', path=path, msg="No writeable FS set") - self.writefs.settimes(path, accessed_time, modified_time) + self.writefs.settimes(path, accessed_time, modified_time) @synchronize def getinfo(self, path): |