2023-06-06 22:12:44 +09:00
|
|
|
import json
|
2023-07-19 21:47:47 +09:00
|
|
|
from argparse import Namespace
|
|
|
|
from typing import Any
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-06-06 19:59:55 +09:00
|
|
|
from pydantic.dataclasses import dataclass
|
|
|
|
|
|
|
|
from mdrsclient.api import FolderApi
|
2023-05-01 20:00:32 +09:00
|
|
|
from mdrsclient.commands.base import BaseCommand
|
2023-06-06 19:59:55 +09:00
|
|
|
from mdrsclient.connection import MDRSConnection
|
|
|
|
from mdrsclient.exceptions import UnauthorizedException
|
2023-06-06 22:12:44 +09:00
|
|
|
from mdrsclient.models import File, Folder, FolderSimple, Laboratory
|
2023-06-06 19:59:55 +09:00
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
frozen = True
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(config=Config)
|
|
|
|
class LsCommandContext:
|
|
|
|
prefix: str
|
|
|
|
connection: MDRSConnection
|
|
|
|
laboratory: Laboratory
|
|
|
|
password: str
|
2023-06-06 22:12:44 +09:00
|
|
|
is_json: bool
|
2023-06-06 19:59:55 +09:00
|
|
|
is_quick: bool
|
|
|
|
is_recursive: bool
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
class LsCommand(BaseCommand):
|
|
|
|
@classmethod
|
2023-07-19 21:47:47 +09:00
|
|
|
def register(cls, parsers: Any) -> None:
|
2023-05-10 18:17:35 +09:00
|
|
|
ls_parser = parsers.add_parser("ls", help="list the folder contents")
|
2023-05-10 20:10:43 +09:00
|
|
|
ls_parser.add_argument("-p", "--password", help="password to use when open locked folder")
|
2023-06-06 22:12:44 +09:00
|
|
|
ls_parser.add_argument("-J", "--json", help="turn on json output", action="store_true")
|
2023-06-06 19:59:55 +09:00
|
|
|
ls_parser.add_argument(
|
|
|
|
"-q",
|
|
|
|
"--quick",
|
|
|
|
help="don't output header row. this option is forced if the -r option is specified",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2023-06-06 22:12:44 +09:00
|
|
|
ls_parser.add_argument("-r", "--recursive", help="list the folder contents recursive", action="store_true")
|
2023-05-10 18:17:35 +09:00
|
|
|
ls_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
|
2023-07-19 21:47:47 +09:00
|
|
|
ls_parser.set_defaults(func=cls.func)
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def func(cls, args: Namespace) -> None:
|
|
|
|
remote_path = str(args.remote_path)
|
|
|
|
password = str(args.password) if args.password else None
|
2023-06-06 22:12:44 +09:00
|
|
|
is_json = bool(args.json)
|
2023-06-06 19:59:55 +09:00
|
|
|
is_recursive = bool(args.recursive)
|
|
|
|
is_quick = bool(args.quick) if not is_recursive else True
|
2023-07-19 21:47:47 +09:00
|
|
|
cls.ls(remote_path, password, is_json, is_recursive, is_quick)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ls(cls, remote_path: str, password: str | None, is_json: bool, is_recursive: bool, is_quick: bool) -> None:
|
|
|
|
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
|
|
|
|
connection = cls._create_connection(remote)
|
|
|
|
laboratory = cls._find_laboratory(connection, laboratory_name)
|
|
|
|
context = LsCommandContext(
|
2023-06-06 19:59:55 +09:00
|
|
|
f"{remote}:/{laboratory_name}",
|
|
|
|
connection,
|
|
|
|
laboratory,
|
2023-07-19 21:47:47 +09:00
|
|
|
password if password is not None else "",
|
2023-06-06 22:12:44 +09:00
|
|
|
is_json,
|
2023-06-06 19:59:55 +09:00
|
|
|
is_quick,
|
|
|
|
is_recursive,
|
|
|
|
)
|
2023-07-19 21:47:47 +09:00
|
|
|
folder = cls._find_folder(connection, laboratory, r_path, password)
|
|
|
|
if context.is_json:
|
|
|
|
cls._ls_json(context, folder)
|
2023-06-06 22:12:44 +09:00
|
|
|
else:
|
2023-07-19 21:47:47 +09:00
|
|
|
cls._ls_plain(context, folder)
|
2023-06-06 19:59:55 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def _ls_json(cls, context: LsCommandContext, folder: Folder) -> None:
|
|
|
|
print(json.dumps(cls._folder2dict(context, folder), ensure_ascii=False))
|
2023-06-06 22:12:44 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def _ls_plain(cls, context: LsCommandContext, folder: Folder) -> None:
|
2023-05-01 20:00:32 +09:00
|
|
|
label = {
|
|
|
|
"type": "Type",
|
|
|
|
"acl": "Access",
|
|
|
|
"laboratory": "Laboratory",
|
|
|
|
"size": "Lock/Size",
|
|
|
|
"date": "Date",
|
|
|
|
"name": "Name",
|
|
|
|
}
|
|
|
|
length: dict[str, int] = {}
|
|
|
|
for key in label.keys():
|
2023-07-19 21:47:47 +09:00
|
|
|
length[key] = len(label[key]) if not context.is_quick else 0
|
2023-05-01 20:00:32 +09:00
|
|
|
for sub_folder in folder.sub_folders:
|
2023-12-07 14:46:57 +09:00
|
|
|
sub_laboratory = context.connection.laboratories.find_by_id(sub_folder.laboratory_id)
|
2023-05-01 20:00:32 +09:00
|
|
|
sub_laboratory_name = sub_laboratory.name if sub_laboratory is not None else "(invalid)"
|
|
|
|
length["acl"] = max(length["acl"], len(sub_folder.access_level_name))
|
|
|
|
length["laboratory"] = max(length["laboratory"], len(sub_laboratory_name))
|
|
|
|
length["size"] = max(length["size"], len(sub_folder.lock_name))
|
|
|
|
length["date"] = max(length["date"], len(sub_folder.updated_at_name))
|
|
|
|
length["name"] = max(length["name"], len(sub_folder.name))
|
|
|
|
for file in folder.files:
|
|
|
|
length["size"] = max(length["size"], len(str(file.size)))
|
|
|
|
length["date"] = max(length["date"], len(file.updated_at_name))
|
|
|
|
length["name"] = max(length["name"], len(file.name))
|
|
|
|
length["acl"] = max(length["acl"], len(folder.access_level_name))
|
2023-07-19 21:47:47 +09:00
|
|
|
length["laboratory"] = max(length["laboratory"], len(context.laboratory.name))
|
2023-05-01 20:00:32 +09:00
|
|
|
header = (
|
|
|
|
f"{label['type']:{length['type']}}\t{label['acl']:{length['acl']}}\t"
|
|
|
|
f"{label['laboratory']:{length['laboratory']}}\t{label['size']:{length['size']}}\t"
|
|
|
|
f"{label['date']:{length['date']}}\t{label['name']:{length['name']}}"
|
|
|
|
)
|
2023-06-06 19:59:55 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
if context.is_recursive:
|
|
|
|
print(f"{context.prefix}{folder.path}:")
|
2023-06-06 19:59:55 +09:00
|
|
|
print(f"total {sum(f.size for f in folder.files)}")
|
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
if not context.is_quick:
|
2023-06-06 19:59:55 +09:00
|
|
|
print(header)
|
|
|
|
print("-" * len(header.expandtabs()))
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
for sub_folder in sorted(folder.sub_folders, key=lambda x: x.name):
|
2023-12-07 14:46:57 +09:00
|
|
|
sub_laboratory_name = cls._laboratory_name(context, sub_folder.laboratory_id)
|
2023-05-01 20:00:32 +09:00
|
|
|
print(
|
|
|
|
f"{'[d]':{length['type']}}\t{sub_folder.access_level_name:{length['acl']}}\t"
|
|
|
|
f"{sub_laboratory_name:{length['laboratory']}}\t{sub_folder.lock_name:{length['size']}}\t"
|
|
|
|
f"{sub_folder.updated_at_name:{length['date']}}\t{sub_folder.name:{length['name']}}"
|
|
|
|
)
|
|
|
|
for file in sorted(folder.files, key=lambda x: x.name):
|
|
|
|
print(
|
|
|
|
f"{'[f]':{length['type']}}\t{folder.access_level_name:{length['acl']}}\t"
|
2023-07-19 21:47:47 +09:00
|
|
|
f"{context.laboratory.name:{length['laboratory']}}\t{file.size:{length['size']}}\t"
|
2023-05-01 20:00:32 +09:00
|
|
|
f"{file.updated_at_name:{length['date']}}\t{file.name:{length['name']}}"
|
|
|
|
)
|
2023-06-06 19:59:55 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
if context.is_recursive:
|
2023-06-06 19:59:55 +09:00
|
|
|
print("")
|
|
|
|
for sub_folder in sorted(folder.sub_folders, key=lambda x: x.name):
|
2023-07-19 21:47:47 +09:00
|
|
|
folder_api = FolderApi(context.connection)
|
2023-06-06 19:59:55 +09:00
|
|
|
try:
|
|
|
|
if sub_folder.lock:
|
2023-07-19 21:47:47 +09:00
|
|
|
folder_api.auth(sub_folder.id, context.password)
|
2023-06-06 19:59:55 +09:00
|
|
|
folder = folder_api.retrieve(sub_folder.id)
|
2023-07-19 21:47:47 +09:00
|
|
|
cls._ls_plain(context, folder)
|
2023-06-06 19:59:55 +09:00
|
|
|
except UnauthorizedException:
|
|
|
|
pass
|
2023-06-06 22:12:44 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def _folder2dict(cls, context: LsCommandContext, folder: Folder | FolderSimple) -> dict[str, Any]:
|
|
|
|
data: dict[str, Any] = {
|
2023-06-06 22:12:44 +09:00
|
|
|
"id": folder.id,
|
|
|
|
"pid": folder.pid,
|
|
|
|
"name": folder.name,
|
|
|
|
"access_level": folder.access_level_name,
|
|
|
|
"lock": folder.lock,
|
2023-12-07 14:46:57 +09:00
|
|
|
"laboratory": cls._laboratory_name(context, folder.laboratory_id),
|
2023-06-06 22:12:44 +09:00
|
|
|
"description": folder.description,
|
|
|
|
"created_at": folder.created_at,
|
|
|
|
"updated_at": folder.updated_at,
|
|
|
|
}
|
|
|
|
if isinstance(folder, Folder):
|
2023-07-19 21:47:47 +09:00
|
|
|
folder_api = FolderApi(context.connection)
|
2023-12-07 14:46:57 +09:00
|
|
|
print(folder.name)
|
2023-06-06 22:12:44 +09:00
|
|
|
data["metadata"] = folder_api.metadata(folder.id)
|
2023-07-19 21:47:47 +09:00
|
|
|
if context.is_recursive:
|
|
|
|
sub_folders: list[dict[str, Any]] = []
|
2023-06-06 22:12:44 +09:00
|
|
|
for sub_folder in sorted(folder.sub_folders, key=lambda x: x.name):
|
|
|
|
try:
|
|
|
|
if sub_folder.lock:
|
2023-07-19 21:47:47 +09:00
|
|
|
folder_api.auth(sub_folder.id, context.password)
|
2023-06-06 22:12:44 +09:00
|
|
|
folder2 = folder_api.retrieve(sub_folder.id)
|
2023-07-19 21:47:47 +09:00
|
|
|
sub_folders.append(cls._folder2dict(context, folder2))
|
2023-06-06 22:12:44 +09:00
|
|
|
except UnauthorizedException:
|
|
|
|
pass
|
2023-07-19 21:47:47 +09:00
|
|
|
data["sub_folders"] = sub_folders
|
2023-06-06 22:12:44 +09:00
|
|
|
else:
|
2023-06-07 11:05:10 +09:00
|
|
|
data["sub_folders"] = list(
|
2023-07-19 21:47:47 +09:00
|
|
|
map(lambda x: cls._folder2dict(context, x), sorted(folder.sub_folders, key=lambda x: x.name))
|
2023-06-06 22:12:44 +09:00
|
|
|
)
|
2023-07-19 21:47:47 +09:00
|
|
|
data["files"] = list(map(lambda x: cls._file2dict(context, x), sorted(folder.files, key=lambda x: x.name)))
|
2023-06-06 22:12:44 +09:00
|
|
|
return data
|
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def _file2dict(cls, context: LsCommandContext, file: File) -> dict[str, Any]:
|
|
|
|
data: dict[str, Any] = {
|
2023-06-06 22:12:44 +09:00
|
|
|
"id": file.id,
|
|
|
|
"name": file.name,
|
|
|
|
"type": file.type,
|
|
|
|
"size": file.size,
|
|
|
|
# "thumbnail": file.thumbnail,
|
|
|
|
"description": file.description,
|
|
|
|
"metadata": file.metadata,
|
2023-07-19 21:47:47 +09:00
|
|
|
"download_url": f"{context.connection.url}/v2/{file.download_url}",
|
2023-06-06 22:12:44 +09:00
|
|
|
"created_at": file.created_at,
|
|
|
|
"updated_at": file.updated_at,
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def _laboratory_name(cls, context: LsCommandContext, laboratory_id: int) -> str:
|
|
|
|
laboratory = context.connection.laboratories.find_by_id(laboratory_id)
|
2023-06-06 22:12:44 +09:00
|
|
|
return laboratory.name if laboratory is not None else "(invalid)"
|