36cad6db52
To improve the tool's portability as a Python library, the core logic has been decoupled from the CLI interface. This allows developers to programmatically interact with MDRS without relying on CLI-specific argument parsing or local file-based caches. - Introduce `MdrsClient` service layer to handle core operations. - Abstract authentication state using `CacheInterface` and `InMemoryCache`. - Migrate all CLI commands to utilize `MdrsClient` for execution. - Separate `Doi` data model from API responses and move to `models/doi.py`. - Update `README.md` to include Python API usage examples. - Bump package version to 1.3.17.
47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
from argparse import Namespace
|
|
from typing import Any
|
|
|
|
from mdrsclient.api import LaboratoriesApi
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
|
|
class LabsCommand(BaseCommand):
|
|
@classmethod
|
|
def register(cls, parsers: Any) -> None:
|
|
labs_parser = parsers.add_parser("labs", help="list all laboratories")
|
|
labs_parser.add_argument("remote", help="label of remote host")
|
|
labs_parser.set_defaults(func=cls.func)
|
|
|
|
@classmethod
|
|
def func(cls, args: Namespace) -> None:
|
|
remote = str(args.remote)
|
|
cls.labs(remote)
|
|
|
|
@classmethod
|
|
def labs(cls, remote: str) -> None:
|
|
remote_host = cls._parse_remote_host(remote)
|
|
from mdrsclient.client import MdrsClient
|
|
|
|
client = MdrsClient.from_remote(remote_host)
|
|
laboratories = client.get_laboratories()
|
|
label = {"id": "ID", "name": "Name", "pi_name": "PI", "full_name": "Laboratory"}
|
|
length: dict[str, int] = {}
|
|
for key in label.keys():
|
|
length[key] = len(label[key])
|
|
for laboratory in laboratories:
|
|
length["id"] = max(length["id"], len(str(laboratory.id)))
|
|
length["name"] = max(length["name"], len(laboratory.name))
|
|
length["pi_name"] = max(length["pi_name"], len(laboratory.pi_name))
|
|
length["full_name"] = max(length["full_name"], len(laboratory.full_name))
|
|
header = (
|
|
f"{label['name']:{length['name']}}\t"
|
|
f"{label['pi_name']:{length['pi_name']}}\t{label['full_name']:{length['full_name']}}"
|
|
)
|
|
print(header)
|
|
print("-" * len(header.expandtabs()))
|
|
for laboratory in laboratories:
|
|
print(
|
|
f"{laboratory.name:{length['name']}}\t"
|
|
f"{laboratory.pi_name:{length['pi_name']}}\t{laboratory.full_name:{length['full_name']}}"
|
|
)
|