add -p option for support to open PW_OPEN folder.

This commit is contained in:
Yoshihiro OKUMURA 2023-05-10 20:10:43 +09:00
parent 97007233c6
commit 995e4cc467
Signed by: orrisroot
GPG Key ID: 470AA444C92904B2
7 changed files with 34 additions and 8 deletions

View File

@ -44,6 +44,7 @@ $ mdrs labs neurodata:
List the folder contents
```
$ mdrs ls neurodata:/NIU/Repository/
$ mdrs ls -p PW_OPEN_PASSWORD neurodata:/NIU/Repository/PW_Open/
```
### mkdir
@ -63,7 +64,8 @@ $ mdrs upload -r ./dataset neurodata:/NIU/Repository/TEST/
Download the file or folder
```
$ mdrs download neurodata:/NIU/Repository/TEST/sample.dat ./
$ mdrs download -r neurodata:/NIU/Repository/TEST/dataset ./
$ mdrs download -r neurodata:/NIU/Repository/TEST/dataset/ ./
$ mdrs download -p PW_OPEN_PASSWORD neurodata:/NIU/Repository/PW_Open/Readme.dat ./
```
### mv
@ -83,13 +85,15 @@ $ mdrs rm -r neurodata:/NIU/Repository/TEST2/dataset
### metadata
Get a folder metadata
```
$ mdrs metadata neurodata:/NIU/Repository/TEST
$ mdrs metadata neurodata:/NIU/Repository/TEST/
$ mdrs metadata -p PW_OPEN_PASSWORD neurodata:/NIU/Repository/PW_Open/
```
### file-metadata
Get the file metadata
```
$ mdrs file-metadata neurodata:/NIU/Repository/TEST/dataset/sample.dat
$ mdrs file-metadata -p PW_OPEN_PASSWORD neurodata:/NIU/Repository/PW_Open/Readme.txt
```
### help

View File

@ -5,6 +5,7 @@ from pydantic.dataclasses import dataclass
from mdrsclient.api.base import BaseApi
from mdrsclient.api.utils import token_check
from mdrsclient.exceptions import UnauthorizedException
from mdrsclient.models import Folder, FolderSimple
@ -67,6 +68,19 @@ class FolderApi(BaseApi):
self._raise_response_error(response)
return True
def auth(self, id: str, password: str) -> bool:
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
url = self.ENTRYPOINT + id + "/auth/"
data = {"password": password}
token_check(self.connection)
response = self.connection.post(url, data=data)
# stupid! always returned status code 200
self._raise_response_error(response)
data = response.json()
if data.get("status") is not True:
raise UnauthorizedException("Password is incorrect.")
return True
def move(self, folder: FolderSimple, folder_id: str) -> bool:
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
url = self.ENTRYPOINT + folder.id + "/move/"

View File

@ -35,13 +35,17 @@ class BaseCommand(ABC):
raise IllegalArgumentException(f"Laboratory `{name}` not found.")
return laboratory
def _find_folder(self, connection: MDRSConnection, laboratory: Laboratory, path: str) -> Folder:
def _find_folder(
self, connection: MDRSConnection, laboratory: Laboratory, path: str, password: str | None = None
) -> Folder:
folder_api = FolderApi(connection)
folders = folder_api.list(laboratory.id, path)
if len(folders) != 1:
raise UnexpectedException(f"Folder `{path}` not found.")
if folders[0].lock:
raise UnauthorizedException(f"Folder `{path}` is locked.")
if password is None:
raise UnauthorizedException(f"Folder `{path}` is locked.")
folder_api.auth(folders[0].id, password)
return folder_api.retrieve(folders[0].id)
def _parse_remote_host(self, path: str) -> str:

View File

@ -26,6 +26,7 @@ class DownloadCommand(BaseCommand):
download_parser.add_argument(
"-r", "--recursive", help="download folders and their contents recursive", action="store_true"
)
download_parser.add_argument("-p", "--password", help="password to use when open locked folder")
download_parser.add_argument("remote_path", help="remote file path (remote:/lab/path/file)")
download_parser.add_argument("local_path", help="local folder path (/foo/bar/)")
download_parser.set_defaults(func=command.download)
@ -40,7 +41,7 @@ class DownloadCommand(BaseCommand):
if not os.path.isdir(l_dirname):
raise IllegalArgumentException(f"Local directory `{args.local_path}` not found.")
laboratory = self._find_laboratory(connection, laboratory_name)
r_parent_folder = self._find_folder(connection, laboratory, r_dirname)
r_parent_folder = self._find_folder(connection, laboratory, r_dirname, args.password)
file = r_parent_folder.find_file(r_basename)
download_files: list[DownloadFileInfo] = []
if file is not None:

View File

@ -11,6 +11,7 @@ class FileMetadataCommand(BaseCommand):
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
file_metadata_parser = parsers.add_parser("file-metadata", help="get the file metadata")
file_metadata_parser.add_argument("-p", "--password", help="password to use when open locked folder")
file_metadata_parser.add_argument("remote_path", help="remote file path (remote:/lab/path/file)")
file_metadata_parser.set_defaults(func=command.file_metadata)
@ -21,7 +22,7 @@ class FileMetadataCommand(BaseCommand):
r_basename = os.path.basename(r_path)
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
folder = self._find_folder(connection, laboratory, r_dirname)
folder = self._find_folder(connection, laboratory, r_dirname, args.password)
file = folder.find_file(r_basename)
if file is None:
raise IllegalArgumentException(f"File `{r_basename}` not found.")

View File

@ -8,6 +8,7 @@ class LsCommand(BaseCommand):
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
ls_parser = parsers.add_parser("ls", help="list the folder contents")
ls_parser.add_argument("-p", "--password", help="password to use when open locked folder")
ls_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
ls_parser.set_defaults(func=command.ls)
@ -15,7 +16,7 @@ class LsCommand(BaseCommand):
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
folder = self._find_folder(connection, laboratory, r_path)
folder = self._find_folder(connection, laboratory, r_path, args.password)
label = {
"type": "Type",
"acl": "Access",

View File

@ -9,6 +9,7 @@ class MetadataCommand(BaseCommand):
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
metadata_parser = parsers.add_parser("metadata", help="get a folder metadata")
metadata_parser.add_argument("-p", "--password", help="password to use when open locked folder")
metadata_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
metadata_parser.set_defaults(func=command.metadata)
@ -16,7 +17,7 @@ class MetadataCommand(BaseCommand):
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
folder = self._find_folder(connection, laboratory, r_path)
folder = self._find_folder(connection, laboratory, r_path, args.password)
folder_api = FolderApi(connection)
metadata = folder_api.metadata(folder.id)
print(metadata)