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

30 lines
1.4 KiB
Python

import os
from argparse import Namespace, _SubParsersAction
from unicodedata import normalize
from mdrsclient.api import FolderApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.exceptions import IllegalArgumentException
class MkdirCommand(BaseCommand):
@classmethod
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
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=command.mkdir)
def mkdir(self, args: Namespace) -> None:
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
r_path = r_path.rstrip("/")
r_dirname = os.path.dirname(r_path)
r_basename = os.path.basename(r_path)
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
parent_folder = self._find_folder(connection, laboratory, r_dirname)
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)