fixed type errors when pylance type checking mode is strict.

This commit is contained in:
2023-07-19 21:47:47 +09:00
parent 23025bd679
commit 08d8a0626a
24 changed files with 387 additions and 236 deletions

View File

@ -1,6 +1,7 @@
import os
from argparse import Namespace, _SubParsersAction
from argparse import Namespace
from concurrent.futures import ThreadPoolExecutor
from typing import Any
from pydantic.dataclasses import dataclass
@ -20,28 +21,35 @@ class UploadFileInfo:
class UploadCommand(BaseCommand):
@classmethod
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
def register(cls, parsers: Any) -> None:
upload_parser = parsers.add_parser("upload", help="upload the file or directory")
upload_parser.add_argument(
"-r", "--recursive", help="upload directories and their contents recursive", action="store_true"
)
upload_parser.add_argument("local_path", help="local file path (/foo/bar/data.txt)")
upload_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
upload_parser.set_defaults(func=command.upload)
upload_parser.set_defaults(func=cls.func)
def upload(self, args: Namespace) -> None:
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
l_path = os.path.realpath(args.local_path)
@classmethod
def func(cls, args: Namespace) -> None:
local_path = str(args.local_path)
remote_path = str(args.remote_path)
is_recursive = bool(args.recursive)
cls.upload(local_path, remote_path, is_recursive)
@classmethod
def upload(cls, local_path: str, remote_path: str, is_recursive: bool) -> None:
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
l_path = os.path.realpath(local_path)
if not os.path.exists(l_path):
raise IllegalArgumentException(f"File or directory `{args.local_path}` not found.")
connection = self._create_connection(remote)
laboratory = self._find_laboratory(connection, laboratory_name)
folder = self._find_folder(connection, laboratory, r_path)
raise IllegalArgumentException(f"File or directory `{local_path}` not found.")
connection = cls._create_connection(remote)
laboratory = cls._find_laboratory(connection, laboratory_name)
folder = cls._find_folder(connection, laboratory, r_path)
infos: list[UploadFileInfo] = []
if os.path.isdir(l_path):
if not args.recursive:
raise IllegalArgumentException(f"Cannot upload `{args.local_path}`: Is a directory.")
if not is_recursive:
raise IllegalArgumentException(f"Cannot upload `{local_path}`: Is a directory.")
folder_api = FolderApi(connection)
folder_map: dict[str, Folder] = {}
folder_map[r_path] = folder
@ -53,7 +61,7 @@ class UploadCommand(BaseCommand):
# prepare destination parent path
d_parent_dirname = os.path.dirname(d_dirname)
if folder_map.get(d_parent_dirname) is None:
folder_map[d_parent_dirname] = self._find_folder(connection, laboratory, d_parent_dirname)
folder_map[d_parent_dirname] = cls._find_folder(connection, laboratory, d_parent_dirname)
# prepare destination path
if folder_map.get(d_dirname) is None:
d_folder = folder_map[d_parent_dirname].find_sub_folder(d_basename)
@ -70,14 +78,16 @@ class UploadCommand(BaseCommand):
infos.append(UploadFileInfo(folder_map[d_dirname], os.path.join(dirpath, filename)))
else:
infos.append(UploadFileInfo(folder, l_path))
self.__multiple_upload(connection, infos)
cls.__multiple_upload(connection, infos)
def __multiple_upload(self, connection: MDRSConnection, infos: list[UploadFileInfo]) -> None:
@classmethod
def __multiple_upload(cls, connection: MDRSConnection, infos: list[UploadFileInfo]) -> None:
file_api = FileApi(connection)
with ThreadPoolExecutor(max_workers=CONCURRENT) as pool:
pool.map(lambda x: self.__multiple_upload_worker(file_api, x), infos)
pool.map(lambda x: cls.__multiple_upload_worker(file_api, x), infos)
def __multiple_upload_worker(self, file_api: FileApi, info: UploadFileInfo) -> None:
@classmethod
def __multiple_upload_worker(cls, file_api: FileApi, info: UploadFileInfo) -> None:
basename = os.path.basename(info.path)
file = info.folder.find_file(basename)
try: