mdrs-client-python/mdrsclient/commands/mkdir.py

36 lines
1.5 KiB
Python
Raw Normal View History

2023-05-10 18:17:35 +09:00
import os
from argparse import Namespace
from typing import Any
from unicodedata import normalize
2023-05-10 18:17:35 +09:00
from mdrsclient.api import FolderApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
class MkdirCommand(BaseCommand):
@classmethod
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/)")
mkdir_parser.set_defaults(func=cls.func)
2023-05-10 18:17:35 +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)
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.")
folder_api = FolderApi(connection)
folder_api.create(normalize("NFC", r_basename), parent_folder.id)