2023-07-19 21:47:47 +09:00
|
|
|
from argparse import Namespace
|
|
|
|
from typing import Any
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-12-12 20:05:46 +09:00
|
|
|
from mdrsclient.api import LaboratoriesApi
|
2023-05-01 20:00:32 +09:00
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
|
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
class LabsCommand(BaseCommand):
|
|
|
|
@classmethod
|
2023-07-19 21:47:47 +09:00
|
|
|
def register(cls, parsers: Any) -> None:
|
2023-05-10 18:17:35 +09:00
|
|
|
labs_parser = parsers.add_parser("labs", help="list all laboratories")
|
|
|
|
labs_parser.add_argument("remote", help="label of remote host")
|
2023-07-19 21:47:47 +09:00
|
|
|
labs_parser.set_defaults(func=cls.func)
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def func(cls, args: Namespace) -> None:
|
|
|
|
remote = str(args.remote)
|
|
|
|
cls.labs(remote)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def labs(cls, remote: str) -> None:
|
|
|
|
remote = cls._parse_remote_host(remote)
|
|
|
|
connection = cls._create_connection(remote)
|
2023-12-12 20:05:46 +09:00
|
|
|
laboratory_api = LaboratoriesApi(connection)
|
2023-05-01 20:00:32 +09:00
|
|
|
laboratories = laboratory_api.list()
|
2023-05-09 13:08:50 +09:00
|
|
|
connection.laboratories = laboratories
|
2023-05-01 20:00:32 +09:00
|
|
|
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 = (
|
2023-06-04 18:53:21 +09:00
|
|
|
# f"{label['id']:{length['id']}}\t{label['name']:{length['name']}}\t"
|
|
|
|
f"{label['name']:{length['name']}}\t"
|
2023-05-01 20:00:32 +09:00
|
|
|
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(
|
2023-06-04 18:53:21 +09:00
|
|
|
# f"{laboratory.id:{length['id']}}\t{laboratory.name:{length['name']}}\t"
|
|
|
|
f"{laboratory.name:{length['name']}}\t"
|
2023-05-01 20:00:32 +09:00
|
|
|
f"{laboratory.pi_name:{length['pi_name']}}\t{laboratory.full_name:{length['full_name']}}"
|
|
|
|
)
|