2023-05-10 18:17:35 +09:00
|
|
|
import os
|
2023-07-19 21:47:47 +09:00
|
|
|
from argparse import Namespace
|
|
|
|
from typing import Any
|
2023-07-19 14:43:16 +09:00
|
|
|
from unicodedata import normalize
|
2023-05-10 18:17:35 +09:00
|
|
|
|
2023-12-12 20:05:46 +09:00
|
|
|
from mdrsclient.api import FoldersApi
|
2023-05-10 18:17:35 +09:00
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
from mdrsclient.exceptions import IllegalArgumentException
|
|
|
|
|
|
|
|
|
|
|
|
class MkdirCommand(BaseCommand):
|
|
|
|
@classmethod
|
2023-07-19 21:47:47 +09:00
|
|
|
def register(cls, parsers: Any) -> None:
|
2023-05-10 18:17:35 +09:00
|
|
|
mkdir_parser = parsers.add_parser("mkdir", help="create a new folder")
|
|
|
|
mkdir_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
|
2023-07-19 21:47:47 +09:00
|
|
|
mkdir_parser.set_defaults(func=cls.func)
|
2023-05-10 18:17:35 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def func(cls, args: Namespace) -> None:
|
|
|
|
remote_path = str(args.remote_path)
|
|
|
|
cls.mkdir(remote_path)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def mkdir(cls, remote_path: str) -> None:
|
|
|
|
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
|
2023-05-10 18:17:35 +09:00
|
|
|
r_path = r_path.rstrip("/")
|
|
|
|
r_dirname = os.path.dirname(r_path)
|
|
|
|
r_basename = os.path.basename(r_path)
|
2023-07-19 21:47:47 +09:00
|
|
|
connection = cls._create_connection(remote)
|
|
|
|
laboratory = cls._find_laboratory(connection, laboratory_name)
|
|
|
|
parent_folder = cls._find_folder(connection, laboratory, r_dirname)
|
2023-05-10 18:17:35 +09:00
|
|
|
if parent_folder.find_sub_folder(r_basename) is not None or parent_folder.find_file(r_basename) is not None:
|
|
|
|
raise IllegalArgumentException(f"Cannot create folder `{r_path}`: File exists.")
|
2023-12-12 20:05:46 +09:00
|
|
|
folder_api = FoldersApi(connection)
|
2023-07-19 14:43:16 +09:00
|
|
|
folder_api.create(normalize("NFC", r_basename), parent_folder.id)
|