67 lines
3.5 KiB
Python
67 lines
3.5 KiB
Python
from argparse import Namespace, _SubParsersAction
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
|
|
class LsCommand(BaseCommand):
|
|
@classmethod
|
|
def register(cls, parsers: _SubParsersAction) -> None:
|
|
command = cls()
|
|
ls_parser = parsers.add_parser("ls", help="list the folder contents")
|
|
ls_parser.add_argument("-p", "--password", help="password to use when open locked folder")
|
|
ls_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
|
|
ls_parser.set_defaults(func=command.ls)
|
|
|
|
def ls(self, args: Namespace) -> None:
|
|
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
|
|
connection = self._create_connection(remote)
|
|
laboratory = self._find_laboratory(connection, laboratory_name)
|
|
folder = self._find_folder(connection, laboratory, r_path, args.password)
|
|
label = {
|
|
"type": "Type",
|
|
"acl": "Access",
|
|
"laboratory": "Laboratory",
|
|
"size": "Lock/Size",
|
|
"date": "Date",
|
|
"name": "Name",
|
|
}
|
|
length: dict[str, int] = {}
|
|
for key in label.keys():
|
|
length[key] = len(label[key])
|
|
for sub_folder in folder.sub_folders:
|
|
sub_laboratory = connection.laboratories.find_by_id(sub_folder.lab_id)
|
|
sub_laboratory_name = sub_laboratory.name if sub_laboratory is not None else "(invalid)"
|
|
length["acl"] = max(length["acl"], len(sub_folder.access_level_name))
|
|
length["laboratory"] = max(length["laboratory"], len(sub_laboratory_name))
|
|
length["size"] = max(length["size"], len(sub_folder.lock_name))
|
|
length["date"] = max(length["date"], len(sub_folder.updated_at_name))
|
|
length["name"] = max(length["name"], len(sub_folder.name))
|
|
for file in folder.files:
|
|
length["size"] = max(length["size"], len(str(file.size)))
|
|
length["date"] = max(length["date"], len(file.updated_at_name))
|
|
length["name"] = max(length["name"], len(file.name))
|
|
length["acl"] = max(length["acl"], len(folder.access_level_name))
|
|
length["laboratory"] = max(length["laboratory"], len(laboratory.name))
|
|
header = (
|
|
f"{label['type']:{length['type']}}\t{label['acl']:{length['acl']}}\t"
|
|
f"{label['laboratory']:{length['laboratory']}}\t{label['size']:{length['size']}}\t"
|
|
f"{label['date']:{length['date']}}\t{label['name']:{length['name']}}"
|
|
)
|
|
print(header)
|
|
print("-" * len(header.expandtabs()))
|
|
|
|
for sub_folder in sorted(folder.sub_folders, key=lambda x: x.name):
|
|
sub_laboratory = connection.laboratories.find_by_id(sub_folder.lab_id)
|
|
sub_laboratory_name = sub_laboratory.name if sub_laboratory is not None else "(invalid)"
|
|
print(
|
|
f"{'[d]':{length['type']}}\t{sub_folder.access_level_name:{length['acl']}}\t"
|
|
f"{sub_laboratory_name:{length['laboratory']}}\t{sub_folder.lock_name:{length['size']}}\t"
|
|
f"{sub_folder.updated_at_name:{length['date']}}\t{sub_folder.name:{length['name']}}"
|
|
)
|
|
for file in sorted(folder.files, key=lambda x: x.name):
|
|
print(
|
|
f"{'[f]':{length['type']}}\t{folder.access_level_name:{length['acl']}}\t"
|
|
f"{laboratory.name:{length['laboratory']}}\t{file.size:{length['size']}}\t"
|
|
f"{file.updated_at_name:{length['date']}}\t{file.name:{length['name']}}"
|
|
)
|