import json 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") 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) 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) print(json.dumps(metadata, ensure_ascii=False))