mdrs-client-python/mdrsclient/commands/laboratory.py
2023-05-01 20:00:32 +09:00

43 lines
1.9 KiB
Python

from argparse import Namespace, _SubParsersAction
from mdrsclient.api import LaboratoryApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.commands.utils import create_session, parse_remote_host
class LaboratoryCommand(BaseCommand):
@staticmethod
def register(top_level_subparsers: _SubParsersAction) -> None:
# labs
lls_parser = top_level_subparsers.add_parser("labs", help="list all laboratories")
lls_parser.add_argument("remote", help="Label of remote host")
lls_parser.set_defaults(func=LaboratoryCommand.list)
@staticmethod
def list(args: Namespace) -> None:
remote = parse_remote_host(args.remote)
session = create_session(remote)
laboratory_api = LaboratoryApi(session)
laboratories = laboratory_api.list()
session.laboratories = 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['id']:{length['id']}}\t{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.id:{length['id']}}\t{laboratory.name:{length['name']}}\t"
f"{laboratory.pi_name:{length['pi_name']}}\t{laboratory.full_name:{length['full_name']}}"
)