Files
mdrs-client-python/mdrsclient/commands/file_metadata.py
T
orrisroot 5bdf837941 feat(doi): add DOI-based path access for commands
Support accessing repositories using DOI strings with optional subpaths
across ls, download, metadata, and file-metadata commands.

- Implement GET v3/doi/{id}/ API model and client calls
- Parse and resolve DOI paths into respective folder and files
- Extract common folder and file resolution logic to shared helpers
- Update README with example DOI-based shell commands
2026-06-12 01:28:40 +09:00

38 lines
1.6 KiB
Python

import json
import os
from argparse import Namespace
from typing import Any
from mdrsclient.api import FilesApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
from mdrsclient.models.file import find_file
class FileMetadataCommand(BaseCommand):
@classmethod
def register(cls, parsers: Any) -> None:
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=cls.func)
@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 = remote_path.split(":", 1)[0] if ":" in remote_path else ""
connection = cls._create_connection(remote)
folder, laboratory, r_basename = cls._resolve_file(connection, remote_path, password)
files = cls._find_files(connection, folder.id)
file = find_file(files, r_basename)
if file is None:
raise IllegalArgumentException(f"File `{r_basename}` not found.")
file_api = FilesApi(connection)
metadata = file_api.metadata(file)
print(json.dumps(metadata, ensure_ascii=False))