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

33 lines
1.5 KiB
Python
Raw Normal View History

import json
2023-05-10 18:17:35 +09:00
import os
from argparse import Namespace, _SubParsersAction
from mdrsclient.api import FileApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
class FileMetadataCommand(BaseCommand):
@classmethod
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")
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=command.file_metadata)
def file_metadata(self, args: Namespace) -> None:
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
r_path = r_path.rstrip("/")
r_dirname = os.path.dirname(r_path)
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, args.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.")
file_api = FileApi(connection)
metadata = file_api.metadata(file)
2023-06-06 17:04:32 +09:00
print(json.dumps(metadata, ensure_ascii=False))