From b569c20b6e28f9d8a6417a6a5e0e573b429fced7 Mon Sep 17 00:00:00 2001 From: Yoshihiro OKUMURA Date: Wed, 19 Jul 2023 13:46:23 +0900 Subject: [PATCH] support case insensitive and normalized file and folder names. --- mdrsclient/api/file.py | 2 +- mdrsclient/commands/base.py | 3 ++- mdrsclient/commands/mv.py | 11 +++++------ mdrsclient/commands/upload.py | 2 +- mdrsclient/models/folder.py | 7 +++++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/mdrsclient/api/file.py b/mdrsclient/api/file.py index 1d3e196..a9777f2 100644 --- a/mdrsclient/api/file.py +++ b/mdrsclient/api/file.py @@ -68,7 +68,7 @@ class FileApi(BaseApi): def move(self, file: File, folder_id: str) -> bool: # print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name) url = self.ENTRYPOINT + file.id + "/move/" - data = {"folder": folder_id} + data = {"folder": folder_id, "name": file.name} token_check(self.connection) response = self.connection.post(url, data=data) self._raise_response_error(response) diff --git a/mdrsclient/commands/base.py b/mdrsclient/commands/base.py index 7fb9c4c..74f5278 100644 --- a/mdrsclient/commands/base.py +++ b/mdrsclient/commands/base.py @@ -1,6 +1,7 @@ import re from abc import ABC, abstractmethod from argparse import _SubParsersAction +from unicodedata import normalize from mdrsclient.api import FolderApi, LaboratoryApi from mdrsclient.config import ConfigFile @@ -39,7 +40,7 @@ class BaseCommand(ABC): self, connection: MDRSConnection, laboratory: Laboratory, path: str, password: str | None = None ) -> Folder: folder_api = FolderApi(connection) - folders = folder_api.list(laboratory.id, path) + folders = folder_api.list(laboratory.id, normalize("NFC", path)) if len(folders) != 1: raise UnexpectedException(f"Folder `{path}` not found.") if folders[0].lock: diff --git a/mdrsclient/commands/mv.py b/mdrsclient/commands/mv.py index 36fdaba..8d3bf75 100644 --- a/mdrsclient/commands/mv.py +++ b/mdrsclient/commands/mv.py @@ -1,6 +1,7 @@ import dataclasses import os from argparse import Namespace, _SubParsersAction +from unicodedata import normalize from pydantic import parse_obj_as @@ -49,12 +50,10 @@ class MvCommand(BaseCommand): if d_sub_folder is not None: raise IllegalArgumentException(f"Cannot overwrite non-folder `{d_basename}` with folder `{d_path}`.") file_api = FileApi(connection) - if s_parent_folder.id != d_parent_folder.id: - file_api.move(s_file, d_parent_folder.id) - if d_basename != s_basename: - d_file_dict = dataclasses.asdict(s_file) | {"name": d_basename} + if s_parent_folder.id != d_parent_folder.id or d_basename != s_basename: + d_file_dict = dataclasses.asdict(s_file) | {"name": normalize("NFC", d_basename)} d_file = parse_obj_as(File, d_file_dict) - file_api.update(d_file, None) + file_api.move(d_file, d_parent_folder.id) else: s_folder = s_parent_folder.find_sub_folder(s_basename) if s_folder is None: @@ -71,6 +70,6 @@ class MvCommand(BaseCommand): if s_parent_folder.id != d_parent_folder.id: folder_api.move(s_folder, d_parent_folder.id) if s_basename != d_basename: - d_folder_dict = dataclasses.asdict(s_folder) | {"name": d_basename} + d_folder_dict = dataclasses.asdict(s_folder) | {"name": normalize("NFC", d_basename)} d_folder = parse_obj_as(FolderSimple, d_folder_dict) folder_api.update(d_folder) diff --git a/mdrsclient/commands/upload.py b/mdrsclient/commands/upload.py index b5bb3b5..b71fdf1 100644 --- a/mdrsclient/commands/upload.py +++ b/mdrsclient/commands/upload.py @@ -46,7 +46,7 @@ class UploadCommand(BaseCommand): folder_map: dict[str, Folder] = {} folder_map[r_path] = folder l_basename = os.path.basename(l_path) - for dirpath, dirnames, filenames in os.walk(l_path): + for dirpath, _, filenames in os.walk(l_path): sub = l_basename if dirpath == l_path else os.path.join(l_basename, os.path.relpath(dirpath, l_path)) d_dirname = os.path.join(r_path, sub) d_basename = os.path.basename(d_dirname) diff --git a/mdrsclient/models/folder.py b/mdrsclient/models/folder.py index cabd894..409680a 100644 --- a/mdrsclient/models/folder.py +++ b/mdrsclient/models/folder.py @@ -1,4 +1,5 @@ from typing import Final, NamedTuple +from unicodedata import normalize from pydantic.dataclasses import dataclass @@ -71,7 +72,9 @@ class Folder(FolderSimple): path: str def find_sub_folder(self, name: str) -> FolderSimple | None: - return next((x for x in self.sub_folders if x.name == name), None) + _name = normalize("NFC", name).lower() + return next((x for x in self.sub_folders if x.name.lower() == _name), None) def find_file(self, name: str) -> File | None: - return next((x for x in self.files if x.name == name), None) + _name = normalize("NFC", name).lower() + return next((x for x in self.files if x.name.lower() == _name), None)