support case insensitive and normalized file and folder names.

This commit is contained in:
Yoshihiro OKUMURA 2023-07-19 13:46:23 +09:00
parent 0f12549ec3
commit b569c20b6e
Signed by: orrisroot
GPG Key ID: 470AA444C92904B2
5 changed files with 14 additions and 11 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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)