Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ rclone_python/__pycache__/
rclone_python/scripts/__pycache__
tests/__pycache__
.vscode/
*.conf
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions rclone_python/rclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
36 changes: 35 additions & 1 deletion rclone_python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ 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 not self._initialized:
self.config_path = config_path
self.__class__._initialized = True


def args2string(args: List[str]) -> str:
# separate flags/ named arguments by a space
if args:
Expand All @@ -43,9 +62,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}")

Expand Down Expand Up @@ -101,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()
Expand Down
Loading