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 62 63 64 65 66 67 68 69 70 71 72
|
# coding: utf-8
"""``AppFS`` opener definition.
"""
from __future__ import absolute_import, print_function, unicode_literals
import typing
from .base import Opener
from .errors import OpenerError
from .registry import registry
if typing.TYPE_CHECKING:
from typing import Text, Union
from ..appfs import _AppFS
from ..subfs import SubFS
from .parse import ParseResult
@registry.install
class AppFSOpener(Opener):
"""``AppFS`` opener."""
protocols = ["userdata", "userconf", "sitedata", "siteconf", "usercache", "userlog"]
_protocol_mapping = None
def open_fs(
self,
fs_url, # type: Text
parse_result, # type: ParseResult
writeable, # type: bool
create, # type: bool
cwd, # type: Text
):
# type: (...) -> Union[_AppFS, SubFS[_AppFS]]
from .. import appfs
from ..subfs import ClosingSubFS
if self._protocol_mapping is None:
self._protocol_mapping = {
"userdata": appfs.UserDataFS,
"userconf": appfs.UserConfigFS,
"sitedata": appfs.SiteDataFS,
"siteconf": appfs.SiteConfigFS,
"usercache": appfs.UserCacheFS,
"userlog": appfs.UserLogFS,
}
fs_class = self._protocol_mapping[parse_result.protocol]
resource, delim, path = parse_result.resource.partition("/")
tokens = resource.split(":", 3)
if len(tokens) == 2:
appname, author = tokens
version = None
elif len(tokens) == 3:
appname, author, version = tokens
else:
raise OpenerError(
"resource should be <appname>:<author> "
"or <appname>:<author>:<version>"
)
app_fs = fs_class(appname, author=author, version=version, create=create)
if delim:
if create:
app_fs.makedir(path, recreate=True)
return app_fs.opendir(path, factory=ClosingSubFS)
return app_fs
|