mdrs-client-python/mdrsclient/commands/file_metadata.py

40 lines
1.7 KiB
Python
Raw Permalink Normal View History

import json
2023-05-10 18:17:35 +09:00
import os
from argparse import Namespace
from typing import Any
2023-05-10 18:17:35 +09:00
2023-12-12 20:05:46 +09:00
from mdrsclient.api import FilesApi
2023-05-10 18:17:35 +09:00
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
class FileMetadataCommand(BaseCommand):
@classmethod
def register(cls, parsers: Any) -> None:
2023-05-10 18:17:35 +09:00
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")
2023-05-10 18:17:35 +09:00
file_metadata_parser.add_argument("remote_path", help="remote file path (remote:/lab/path/file)")
file_metadata_parser.set_defaults(func=cls.func)
2023-05-10 18:17:35 +09:00
@classmethod
def func(cls, args: Namespace) -> None:
remote_path = str(args.remote_path)
password = str(args.password) if args.password else None
cls.file_metadata(remote_path, password)
@classmethod
def file_metadata(cls, remote_path: str, password: str | None) -> None:
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
2023-05-10 18:17:35 +09:00
r_path = r_path.rstrip("/")
r_dirname = os.path.dirname(r_path)
r_basename = os.path.basename(r_path)
connection = cls._create_connection(remote)
laboratory = cls._find_laboratory(connection, laboratory_name)
folder = cls._find_folder(connection, laboratory, r_dirname, password)
2023-05-10 18:17:35 +09:00
file = folder.find_file(r_basename)
if file is None:
raise IllegalArgumentException(f"File `{r_basename}` not found.")
2023-12-12 20:05:46 +09:00
file_api = FilesApi(connection)
2023-05-10 18:17:35 +09:00
metadata = file_api.metadata(file)
2023-06-06 17:04:32 +09:00
print(json.dumps(metadata, ensure_ascii=False))