Multi Filesystem

A MultiFS is a filesystem composed of a sequence of other filesystems, where the directory structure of each overlays the previous filesystem in the sequence.

One use for such a filesystem would be to selectively override a set of files, to customize behavior. For example, to create a filesystem that could be used to theme a web application. We start with the following directories:

`-- templates
    |-- snippets
    |   `-- panel.html
    |-- index.html
    |-- profile.html
    `-- base.html

`-- theme
    |-- snippets
    |   |-- widget.html
    |   `-- extra.html
    |-- index.html
    `-- theme.html

And we want to create a single filesystem that will load a file from templates/ only if it isn’t found in theme/. Here’s how we could do that:

from fs.osfs import OSFS
from fs.multifs import MultiFS

theme_fs = MultiFS()
theme_fs.add_fs('templates', OSFS('templates'))
theme_fs.add_fs('theme', OSFS('theme'))

Now we have a theme_fs filesystem that presents a single view of both directories:

|-- snippets
|   |-- panel.html
|   |-- widget.html
|   `-- extra.html
|-- index.html
|-- profile.html
|-- base.html
`-- theme.html
class fs.multifs.MultiFS(auto_close=True)

A filesystem that delegates to a sequence of other filesystems.

Operations on the MultiFS will try each ‘child’ filesystem in order, until it succeeds. In effect, creating a filesystem that combines the files and dirs of its children.

add_fs(name, fs, write=False, priority=0)

Add a filesystem to the MultiFS.

Parameters:
  • name (str) – A unique name to refer to the filesystem being added.
  • fs (FS or str) – The filesystem (instance or URL) to add.
  • write (bool, optional) – If this value is True, then the fs will be used as the writeable FS (defaults to False).
  • priority (int, optional) – An integer that denotes the priority 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.
get_fs(name)

Get a filesystem from its name.

Parameters:name (str) – The name of a filesystem previously added.
Returns:the filesystem added as name previously.
Return type:FS
Raises:KeyError – If no filesystem with given name could be found.
iterate_fs()

Get iterator that returns (name, fs) in priority order.

which(path, mode=u'r')

Get a tuple of (name, fs) that the given path would map to.

Parameters:
  • path (str) – A path on the filesystem.
  • mode (str) – An io.open mode.