implemented to change folder access level chacl command.

This commit is contained in:
Yoshihiro OKUMURA 2023-05-11 17:00:14 +09:00
parent 995e4cc467
commit b028c86b20
Signed by: orrisroot
GPG Key ID: 470AA444C92904B2
8 changed files with 80 additions and 12 deletions

View File

@ -14,7 +14,7 @@
}, },
// Extensions - Code Spell Checker // Extensions - Code Spell Checker
"cSpell.ignoreWords": ["getframe", "pydantic"], "cSpell.ignoreWords": ["getframe", "pydantic"],
"cSpell.words": ["mdrs", "mdrsclient", "neurodata", "Neuroinformatics", "RIKEN"], "cSpell.words": ["mdrs", "mdrsclient", "neurodata", "chacl", "Neuroinformatics", "RIKEN"],
// Extensions - isort // Extensions - isort
"isort.args": ["--profile=black"], "isort.args": ["--profile=black"],
// Extensions - Prettier // Extensions - Prettier

View File

@ -1,6 +1,7 @@
import argparse import argparse
from mdrsclient.commands import ( from mdrsclient.commands import (
ChaclCommand,
ConfigCommand, ConfigCommand,
DownloadCommand, DownloadCommand,
FileMetadataCommand, FileMetadataCommand,
@ -35,6 +36,7 @@ def main() -> None:
DownloadCommand.register(parsers) DownloadCommand.register(parsers)
MvCommand.register(parsers) MvCommand.register(parsers)
RmCommand.register(parsers) RmCommand.register(parsers)
ChaclCommand.register(parsers)
MetadataCommand.register(parsers) MetadataCommand.register(parsers)
FileMetadataCommand.register(parsers) FileMetadataCommand.register(parsers)

View File

@ -81,6 +81,19 @@ class FolderApi(BaseApi):
raise UnauthorizedException("Password is incorrect.") raise UnauthorizedException("Password is incorrect.")
return True return True
def acl(self, id: str, access_level: int, recursive: bool, password: str | None) -> bool:
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
url = self.ENTRYPOINT + id + "/acl/"
data: dict[str, int | str] = {"access_level": access_level}
if password is not None:
data.update({"password": password})
if recursive is True:
data.update({"lower": 1})
token_check(self.connection)
response = self.connection.post(url, data=data)
self._raise_response_error(response)
return True
def move(self, folder: FolderSimple, folder_id: str) -> bool: def move(self, folder: FolderSimple, folder_id: str) -> bool:
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name) # print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
url = self.ENTRYPOINT + folder.id + "/move/" url = self.ENTRYPOINT + folder.id + "/move/"

View File

@ -1,3 +1,4 @@
from mdrsclient.commands.chacl import ChaclCommand
from mdrsclient.commands.config import ConfigCommand from mdrsclient.commands.config import ConfigCommand
from mdrsclient.commands.download import DownloadCommand from mdrsclient.commands.download import DownloadCommand
from mdrsclient.commands.file_metadata import FileMetadataCommand from mdrsclient.commands.file_metadata import FileMetadataCommand
@ -14,6 +15,7 @@ from mdrsclient.commands.whoami import WhoamiCommand
__all__ = [ __all__ = [
"ConfigCommand", "ConfigCommand",
"ChaclCommand",
"DownloadCommand", "DownloadCommand",
"FileMetadataCommand", "FileMetadataCommand",
"LabsCommand", "LabsCommand",

View File

@ -0,0 +1,32 @@
from argparse import Namespace, _SubParsersAction
from mdrsclient.api import FolderApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
from mdrsclient.models import FolderAccessLevel
class ChaclCommand(BaseCommand):
@classmethod
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
chacl_parser = parsers.add_parser("chacl", help="change the folder access level")
chacl_parser.add_argument("access_level", help="access level (private, cbs_open, pw_open)")
chacl_parser.add_argument("-r", "--recursive", help="change access levels recursively", action="store_true")
chacl_parser.add_argument("-p", "--password", help="password to set when access level is `pw_open`")
chacl_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
chacl_parser.set_defaults(func=command.chacl)
def chacl(self, args: Namespace) -> None:
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
r_path = r_path.rstrip("/")
access_level = FolderAccessLevel.key2id(args.access_level)
if access_level is None:
raise IllegalArgumentException(
"Invalid `access_level` parameter. must be `private`, `cbs_open` or `pw_open`."
)
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
folder = self._find_folder(connection, laboratory, r_path)
folder_api = FolderApi(connection)
folder_api.acl(folder.id, access_level, args.recursive, args.password)

View File

@ -10,7 +10,6 @@ class MkdirCommand(BaseCommand):
@classmethod @classmethod
def register(cls, parsers: _SubParsersAction) -> None: def register(cls, parsers: _SubParsersAction) -> None:
command = cls() command = cls()
# mkdir
mkdir_parser = parsers.add_parser("mkdir", help="create a new folder") mkdir_parser = parsers.add_parser("mkdir", help="create a new folder")
mkdir_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)") mkdir_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
mkdir_parser.set_defaults(func=command.mkdir) mkdir_parser.set_defaults(func=command.mkdir)

View File

@ -1,6 +1,6 @@
from mdrsclient.models.error import DRFStandardizedErrors from mdrsclient.models.error import DRFStandardizedErrors
from mdrsclient.models.file import File from mdrsclient.models.file import File
from mdrsclient.models.folder import Folder, FolderSimple from mdrsclient.models.folder import Folder, FolderAccessLevel, FolderSimple
from mdrsclient.models.laboratory import Laboratories, Laboratory from mdrsclient.models.laboratory import Laboratories, Laboratory
from mdrsclient.models.user import Token, User from mdrsclient.models.user import Token, User
@ -8,6 +8,7 @@ __all__ = [
"DRFStandardizedErrors", "DRFStandardizedErrors",
"File", "File",
"Folder", "Folder",
"FolderAccessLevel",
"FolderSimple", "FolderSimple",
"Laboratories", "Laboratories",
"Laboratory", "Laboratory",

View File

@ -1,17 +1,35 @@
from typing import Final from typing import Final, NamedTuple
from pydantic.dataclasses import dataclass from pydantic.dataclasses import dataclass
from mdrsclient.models.file import File from mdrsclient.models.file import File
from mdrsclient.models.utils import iso8601_to_user_friendly from mdrsclient.models.utils import iso8601_to_user_friendly
ACCESS_LEVEL_NAMES: Final[dict[int, str]] = {
-1: "Storage", class FolderAccessLevelItem(NamedTuple):
0: "Private", id: int
1: "CBS Open", key: str
2: "PW Open", label: str
3: "Public",
}
class FolderAccessLevel:
ACCESS_LEVELS: Final[list[FolderAccessLevelItem]] = [
FolderAccessLevelItem(-1, "storage", "Storage"),
FolderAccessLevelItem(0, "private", "Private"),
FolderAccessLevelItem(1, "cbs_open", "CBS Open"),
FolderAccessLevelItem(2, "pw_open", "PW Open"),
FolderAccessLevelItem(3, "public", "Public"),
]
@staticmethod
def key2id(key: str) -> int | None:
acl = next((x for x in FolderAccessLevel.ACCESS_LEVELS if x.key == key), None)
return acl.id if acl is not None else None
@staticmethod
def id2label(id: int) -> str | None:
acl = next((x for x in FolderAccessLevel.ACCESS_LEVELS if x.id == id), None)
return acl.label if acl is not None else None
@dataclass(frozen=True) @dataclass(frozen=True)
@ -29,7 +47,8 @@ class FolderSimple:
@property @property
def access_level_name(self) -> str: def access_level_name(self) -> str:
return ACCESS_LEVEL_NAMES[self.access_level] label = FolderAccessLevel.id2label(self.access_level)
return label if label is not None else ""
@property @property
def lock_name(self) -> str: def lock_name(self) -> str: