implemented --exclude argument for download subcommand.
This commit is contained in:
@ -1 +1 @@
|
||||
1.3.6
|
||||
1.3.7
|
||||
|
@ -9,7 +9,7 @@ from mdrsclient.api import FilesApi, FoldersApi
|
||||
from mdrsclient.commands.base import BaseCommand
|
||||
from mdrsclient.connection import MDRSConnection
|
||||
from mdrsclient.exceptions import IllegalArgumentException, UnexpectedException
|
||||
from mdrsclient.models import File
|
||||
from mdrsclient.models import File, Folder, Laboratory
|
||||
from mdrsclient.settings import CONCURRENT
|
||||
|
||||
|
||||
@ -32,6 +32,9 @@ class DownloadCommand(BaseCommand):
|
||||
download_parser.add_argument(
|
||||
"-r", "--recursive", help="download folders and their contents recursive", action="store_true"
|
||||
)
|
||||
download_parser.add_argument(
|
||||
"-e", "--exclude", help="exclude to download path matched file or folders", action="append"
|
||||
)
|
||||
download_parser.add_argument("-p", "--password", help="password to use when open locked folder")
|
||||
download_parser.add_argument("remote_path", help="remote file path (remote:/lab/path/file)")
|
||||
download_parser.add_argument("local_path", help="local folder path (/foo/bar/)")
|
||||
@ -43,10 +46,13 @@ class DownloadCommand(BaseCommand):
|
||||
local_path = str(args.local_path)
|
||||
is_recursive = bool(args.recursive)
|
||||
password = str(args.password) if args.password else None
|
||||
cls.download(remote_path, local_path, is_recursive, password)
|
||||
excludes = list(map(lambda x: str(x).rstrip("/").lower(), args.exclude)) if args.exclude is not None else []
|
||||
cls.download(remote_path, local_path, is_recursive, password, excludes)
|
||||
|
||||
@classmethod
|
||||
def download(cls, remote_path: str, local_path: str, is_recursive: bool, password: str | None) -> None:
|
||||
def download(
|
||||
cls, remote_path: str, local_path: str, is_recursive: bool, password: str | None, excludes: list[str]
|
||||
) -> None:
|
||||
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
|
||||
r_path = r_path.rstrip("/")
|
||||
r_dirname = os.path.dirname(r_path)
|
||||
@ -59,6 +65,8 @@ class DownloadCommand(BaseCommand):
|
||||
r_parent_folder = cls._find_folder(connection, laboratory, r_dirname, password)
|
||||
file = r_parent_folder.find_file(r_basename)
|
||||
if file is not None:
|
||||
if cls.__check_excludes(excludes, laboratory, r_parent_folder, file):
|
||||
return
|
||||
context = DownloadContext(False, [])
|
||||
l_path = os.path.join(l_dirname, r_basename)
|
||||
context.files.append(DownloadFileInfo(file, l_path))
|
||||
@ -70,26 +78,40 @@ class DownloadCommand(BaseCommand):
|
||||
if not is_recursive:
|
||||
raise IllegalArgumentException(f"Cannot download `{r_path}`: Is a folder.")
|
||||
folder_api = FoldersApi(connection)
|
||||
cls.__multiple_download_pickup_recursive_files(connection, folder_api, folder.id, l_dirname)
|
||||
cls.__multiple_download_pickup_recursive_files(
|
||||
connection, folder_api, laboratory, folder.id, l_dirname, excludes
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __multiple_download_pickup_recursive_files(
|
||||
cls, connection: MDRSConnection, folder_api: FoldersApi, folder_id: str, basedir: str
|
||||
cls,
|
||||
connection: MDRSConnection,
|
||||
folder_api: FoldersApi,
|
||||
laboratory: Laboratory,
|
||||
folder_id: str,
|
||||
basedir: str,
|
||||
excludes: list[str],
|
||||
) -> None:
|
||||
context = DownloadContext(False, [])
|
||||
folder = folder_api.retrieve(folder_id)
|
||||
dirname = os.path.join(basedir, folder.name)
|
||||
if cls.__check_excludes(excludes, laboratory, folder, None):
|
||||
return
|
||||
if not os.path.exists(dirname):
|
||||
os.makedirs(dirname)
|
||||
print(dirname)
|
||||
for file in folder.files:
|
||||
if cls.__check_excludes(excludes, laboratory, folder, file):
|
||||
continue
|
||||
path = os.path.join(dirname, file.name)
|
||||
context.files.append(DownloadFileInfo(file, path))
|
||||
cls.__multiple_download(connection, context)
|
||||
if context.hasError:
|
||||
raise UnexpectedException("Some files failed to download.")
|
||||
for sub_folder in folder.sub_folders:
|
||||
cls.__multiple_download_pickup_recursive_files(connection, folder_api, sub_folder.id, dirname)
|
||||
cls.__multiple_download_pickup_recursive_files(
|
||||
connection, folder_api, laboratory, sub_folder.id, dirname, excludes
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __multiple_download(cls, connection: MDRSConnection, context: DownloadContext) -> None:
|
||||
@ -108,3 +130,8 @@ class DownloadCommand(BaseCommand):
|
||||
return False
|
||||
print(info.path)
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def __check_excludes(cls, excludes: list[str], laboratory: Laboratory, folder: Folder, file: File | None) -> bool:
|
||||
path = f"/{laboratory.name}{folder.path}{file.name if file is not None else ""}".rstrip("/").lower()
|
||||
return path in excludes
|
||||
|
Reference in New Issue
Block a user