1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
"""Manage a directory in a *parent* filesystem.
"""
from __future__ import print_function, unicode_literals
import typing
from .path import abspath, join, normpath, relpath
from .wrapfs import WrapFS
if typing.TYPE_CHECKING:
from typing import Text, Tuple
from .base import FS # noqa: F401
_F = typing.TypeVar("_F", bound="FS", covariant=True)
class SubFS(WrapFS[_F], typing.Generic[_F]):
"""A sub-directory on a parent filesystem.
A SubFS is a filesystem object that maps to a sub-directory of
another filesystem. This is the object that is returned by
`~fs.base.FS.opendir`.
"""
def __init__(self, parent_fs, path): # noqa: D107
# type: (_F, Text) -> None
super(SubFS, self).__init__(parent_fs)
self._sub_dir = abspath(normpath(path))
def __repr__(self):
# type: () -> Text
return "{}({!r}, {!r})".format(
self.__class__.__name__, self._wrap_fs, self._sub_dir
)
def __str__(self):
# type: () -> Text
return "{parent}{dir}".format(parent=self._wrap_fs, dir=self._sub_dir)
def delegate_fs(self):
# type: () -> _F
return self._wrap_fs
def delegate_path(self, path):
# type: (Text) -> Tuple[_F, Text]
_path = join(self._sub_dir, relpath(normpath(path)))
return self._wrap_fs, _path
class ClosingSubFS(SubFS[_F], typing.Generic[_F]):
"""A version of `SubFS` which closes its parent when closed."""
def close(self):
# type: () -> None
self.delegate_fs().close()
super(ClosingSubFS, self).close()
|