From b2793adac357189a98cd57a6b8c2f1a47adf5afb Mon Sep 17 00:00:00 2001 From: Anish Sane Date: Wed, 31 Jul 2024 18:45:40 +0530 Subject: [PATCH 1/2] Add support for rclone (backend) copyid --- rclone_python/rclone.py | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/rclone_python/rclone.py b/rclone_python/rclone.py index e2d964a..9566b6d 100644 --- a/rclone_python/rclone.py +++ b/rclone_python/rclone.py @@ -154,6 +154,46 @@ def copy( pbar=pbar, ) +def copyid( + remote_name: str, + file_id: str, + out_path: str, + ignore_existing=False, + show_progress=True, + listener: Callable[[Dict], None] = None, + args=None, + pbar=None, +): + """ + Copies a file based on id to a destination path. + :param remote_name: Specify the remote with 'remote_name' + :param file_id: The source file id + :param out_path: The destination path to use. Specify the remote with 'remote_name:path_on_remote' + :param ignore_existing: If True, all existing files are ignored and not overwritten. + :param show_progress: If true, show a progressbar. + :param listener: An event-listener that is called with every update of rclone. + :param args: List of additional arguments/ flags. + :param pbar: Optional progress bar for integration with custom TUI + """ + if args is None: + args = [] + + if not remote_name.endswith(":"): + remote_name += ':' + + _rclone_transfer_operation( + [remote_name, file_id], + out_path, + ignore_existing=ignore_existing, + command="rclone backend copyid", + command_descr="Copying", + show_progress=show_progress, + listener=listener, + args=args, + pbar=pbar, + file_title=f"ID:{file_id}", + ) + def copyto( in_path: str, @@ -580,7 +620,7 @@ def __init__(self, description, error_msg): @__check_installed def _rclone_transfer_operation( - in_path: str, + in_path: Union[str, list], out_path: str, command: str, command_descr: str, @@ -589,11 +629,12 @@ def _rclone_transfer_operation( listener: Callable[[Dict], None] = None, args=None, pbar=None, + file_title=None, ): """Executes the rclone transfer operation (e.g. copyto, move, ...) and displays the progress of every individual file. Args: - in_path (str): The source path to use. Specify the remote with 'remote_name:path_on_remote' + in_path (str|list): The source path to use. Specify the remote with 'remote_name:path_on_remote' out_path (str): The destination path to use. Specify the remote with 'remote_name:path_on_remote' command (str): The rclone command to execute (e.g. rclone copyto) command_descr (str): The description to this command that should be displayed. @@ -606,7 +647,13 @@ def _rclone_transfer_operation( if args is None: args = [] - prog_title = f"{command_descr} [bold magenta]{utils.shorten_filepath(in_path, 20)}[/bold magenta] to [bold magenta]{utils.shorten_filepath(out_path, 20)}" + in_path_str = in_path + if isinstance(in_path, list): + in_path_str = ' '.join(in_path) + in_path = '" "'.join(in_path) + + file_title = file_title or utils.shorten_filepath(in_path_str, 20) + prog_title = f"{command_descr} [bold magenta]{file_title}[/bold magenta] to [bold magenta]{utils.shorten_filepath(out_path, 20)}" # add global rclone flags if ignore_existing: @@ -636,6 +683,6 @@ def _rclone_transfer_operation( else: _, err = process.communicate() raise RcloneException( - description=f"{command_descr} from {in_path} to {out_path} failed", + description=f"{command_descr} from {in_path_str} to {out_path} failed", error_msg=err.decode("utf-8"), ) From d46a50690db97b753ff40732cd96593d3ec26225 Mon Sep 17 00:00:00 2001 From: Anish Sane Date: Wed, 31 Jul 2024 19:35:15 +0530 Subject: [PATCH 2/2] Add support for multiple IDs in copyid function --- rclone_python/rclone.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rclone_python/rclone.py b/rclone_python/rclone.py index 9566b6d..0a0fcab 100644 --- a/rclone_python/rclone.py +++ b/rclone_python/rclone.py @@ -156,7 +156,7 @@ def copy( def copyid( remote_name: str, - file_id: str, + file_id: Union[str, list], out_path: str, ignore_existing=False, show_progress=True, @@ -181,8 +181,12 @@ def copyid( if not remote_name.endswith(":"): remote_name += ':' + file_title = file_id + if isinstance(file_id, str): + file_id = [file_id] + _rclone_transfer_operation( - [remote_name, file_id], + [remote_name] + file_id, out_path, ignore_existing=ignore_existing, command="rclone backend copyid", @@ -191,7 +195,7 @@ def copyid( listener=listener, args=args, pbar=pbar, - file_title=f"ID:{file_id}", + file_title=f"ID:{file_title}", ) @@ -648,12 +652,14 @@ def _rclone_transfer_operation( args = [] in_path_str = in_path + out_path_str = out_path if isinstance(in_path, list): in_path_str = ' '.join(in_path) - in_path = '" "'.join(in_path) + out_path = '" "'.join([j for i in in_path[1:] for j in (i, out_path)]) + in_path = in_path[0] file_title = file_title or utils.shorten_filepath(in_path_str, 20) - prog_title = f"{command_descr} [bold magenta]{file_title}[/bold magenta] to [bold magenta]{utils.shorten_filepath(out_path, 20)}" + prog_title = f"{command_descr} [bold magenta]{file_title}[/bold magenta] to [bold magenta]{utils.shorten_filepath(out_path_str, 20)}" # add global rclone flags if ignore_existing: @@ -683,6 +689,6 @@ def _rclone_transfer_operation( else: _, err = process.communicate() raise RcloneException( - description=f"{command_descr} from {in_path_str} to {out_path} failed", + description=f"{command_descr} from {in_path_str} to {out_path_str} failed", error_msg=err.decode("utf-8"), )