From a5331198fc55f40b0eea26e11570b03f0bd38593 Mon Sep 17 00:00:00 2001 From: Hendrik Huyskens Date: Wed, 2 Jul 2025 12:08:54 +0200 Subject: [PATCH 1/3] Implement support for custom rclone config path --- README.md | 13 +++++++++++++ rclone_python/rclone.py | 5 +++++ rclone_python/utils.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 90b5dff..98337ac 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,19 @@ print(rclone.check("data", "box:data")) (False, [('*', 'video1.webm'), ('=', 'video2.webm'), ('=', 'video2.webm')]) ``` +### Custom config file +You can define a custom file which rclone shall use by setting it up before running any command. +Example with a config file named ".rclone.conf" in current working directory: +```python +import pathlib +from rclone_python import rclone + +CONFIG_FILE = pathlib.Path(__file__).parent / ".rclone.conf" + +rclone.set_config_file(CONFIG_FILE) +# All upcoming rclone commands will use custom config file +``` + ## Custom Progressbar You can use your own rich progressbar with all transfer operations. This allows you to customize the columns to be displayed. diff --git a/rclone_python/rclone.py b/rclone_python/rclone.py index bf8c87f..b0242d9 100644 --- a/rclone_python/rclone.py +++ b/rclone_python/rclone.py @@ -25,6 +25,11 @@ def wrapper(*args, **kwargs): return wrapper +def set_config_file(config_file: str): + """Change the config file used by this wrapper.""" + utils.Config(config_file) + + def set_log_level(level: int): """Change the log level of this wrapper. diff --git a/rclone_python/utils.py b/rclone_python/utils.py index 897306d..c0c66e7 100644 --- a/rclone_python/utils.py +++ b/rclone_python/utils.py @@ -28,6 +28,26 @@ def __init__(self, description, error_msg): # ---------------------------------------------------------------------------- # +class Config: + """Config set up as singleton""" + _instance = None + _initialized = False + config_path = None + + def __new__(cls, *args, **kwargs): + """Create a new instance of the Config class if it doesn't exist yet.""" + if cls._instance is None: + cls._instance = super().__new__(cls) + return cls._instance + + def __init__(self, config_path: Union[str, Path] = None): + if self._initialized: + logger.warning("Config already initialized.") + else: + self.config_path = config_path + self.__class__._initialized = True + + def args2string(args: List[str]) -> str: # separate flags/ named arguments by a space if args: @@ -43,9 +63,18 @@ def run_rclone_cmd( encoding="utf-8", raise_errors: bool = True, ) -> Union[Tuple[str, str], Tuple[int, str, str]]: + + # Set the config path if defined by the user, + # otherwise the default rclone config path is used: + config = Config() + if config.config_path is not None: + base_command = f"rclone --config={config.config_path}" + else: + base_command = "rclone" + # add optional arguments and flags to the command args_str = args2string(args) - full_command = f"rclone {command} {args_str}" + full_command = f"{base_command} {command} {args_str}" logger.debug(f"Running command: {full_command}") From b8fe31dcff540b25537e455846954392aa150551 Mon Sep 17 00:00:00 2001 From: Hendrik Huyskens Date: Wed, 2 Jul 2025 12:27:55 +0200 Subject: [PATCH 2/3] Fix logging warning for custom config --- rclone_python/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rclone_python/utils.py b/rclone_python/utils.py index c0c66e7..66d6426 100644 --- a/rclone_python/utils.py +++ b/rclone_python/utils.py @@ -41,9 +41,7 @@ def __new__(cls, *args, **kwargs): return cls._instance def __init__(self, config_path: Union[str, Path] = None): - if self._initialized: - logger.warning("Config already initialized.") - else: + if not self._initialized: self.config_path = config_path self.__class__._initialized = True From d010b28703152c39bee8b3103f1ab80c6380a734 Mon Sep 17 00:00:00 2001 From: Johannes <24914225+Johannes11833@users.noreply.github.com> Date: Fri, 4 Jul 2025 16:06:17 +0200 Subject: [PATCH 3/3] Add custom config to transfer operations command as well --- .gitignore | 1 + rclone_python/utils.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index b73f323..b2bed16 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ rclone_python/__pycache__/ rclone_python/scripts/__pycache__ tests/__pycache__ .vscode/ +*.conf \ No newline at end of file diff --git a/rclone_python/utils.py b/rclone_python/utils.py index 66d6426..84f4408 100644 --- a/rclone_python/utils.py +++ b/rclone_python/utils.py @@ -30,6 +30,7 @@ def __init__(self, description, error_msg): class Config: """Config set up as singleton""" + _instance = None _initialized = False config_path = None @@ -128,6 +129,12 @@ def rclone_progress( subprocesses = {} errors = [] + # Set the config path if defined by the user, + # otherwise the default rclone config path is used: + config = Config() + if config.config_path is not None: + command += f' --config="{config.config_path}"' + if show_progress: if pbar is None: pbar = create_progress_bar()