rclone-python

repository·master·Indexed 42 days ago

https://github.com/johannes11833/rclone_python

A Python wrapper for the rclone CLI that enables integration of cloud storage and file synchronization capabilities into Python applications. It provides methods for managing remotes, copying files, deleting or purging directories, listing files via rclone.ls(), and generating file hashes using HashTypes.

Tokens
2K
Snippets
14
Records
14
Agent score
3%

What's inside rclone-python

  1. Use a custom Rich progressbar

    master

    You can pass a rich.progress.Progress instance to transfer operations (like copy) via the pbar argument to customize the visual output.

    from rclone_python import rclone
    from rich.progress import (
        Progress,
        TextColumn,
        BarColumn,
        TaskProgressColumn,
        TransferSpeedColumn,
    )
    
    pbar = Progress(
        TextColumn("[progress.description]{task.description}"),
        BarColumn(),
        TaskProgressColumn(),
        TransferSpeedColumn(),
    )
    
    rclone.copy("data", "box:rclone_test/data1", pbar=pbar)
  2. Purge (remove) entire directories

    master

    Use rclone.purge() to remove an entire directory including all files, subdirectories, and the directory itself.

    from rclone_python import rclone
    
    # remove the entire test_dir folder (and all files contained in it and it's subdirectories) on onedrive
    rclone.purge('onedrive:test_dir')
  3. Generate file hashes

    master

    Use rclone.hash() to generate hashes for files. Pass a hash type from HashTypes (e.g., HashTypes.sha1) and the path to the file or remote path.

    from rclone_python import rclone
    from rclone_python.hash_types import HashTypes
    
    print(rclone.hash(HashTypes.sha1, "box:data")
  4. Copy files between remotes or local paths

    master

    Use rclone.copy(src, dst, ...) to copy files. You can pass additional rclone arguments via the args list and use ignore_existing=True to skip files that already exist at the destination.

    from rclone_python import rclone
    
    # copy all file in the test_dir on OneDrive to the local data folder.
    rclone.copy('onedrive:data', 'data', ignore_existing=True, args=['--create-empty-src-dirs'])
  5. Create a new rclone remote

    master

    You can create a new remote connection using rclone.create_remote(). You must provide a name for the remote and a type from RemoteTypes. For many cloud providers, you can also provide a client_id and client_secret.

    from rclone_python import rclone
    from rclone_python.remote_types import RemoteTypes
    
    # Using default client credentials
    rclone.create_remote('onedrive', RemoteTypes.onedrive)
    
    # Using custom client credentials
    rclone.create_remote('onedrive', RemoteTypes.onedrive, client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET')
  6. Configure a custom rclone config file

    master

    By default, the wrapper uses the system's rclone configuration. You can specify a custom configuration file using rclone.set_config_file(path). All subsequent commands will use this file.

    import pathlib
    from rclone_python import rclone
    
    CONFIG_FILE = pathlib.Path(__file__).parent / ".rclone.conf"
    
    rclone.set_config_file(CONFIG_FILE)
  7. Check if source and destination match

    master

    Use rclone.check(src, dst) to compare files in a source and destination. It returns a tuple containing a boolean (True if they match) and a list of differences.

    Difference symbols:

    • = path: identical in both
    • - path: missing in source (only in destination)
    • + path: missing in destination (only in source)
    • * path: present in both but different
    • ! path: error reading or hashing
    from rclone_python import rclone
    
    print(rclone.check("data", "box:data"))
  8. Delete files or directories

    master

    Use rclone.delete() to remove specific files or directories. Note that when deleting a directory, only the files within the directory (and its subdirectories) are deleted; the folder structure itself remains.

    from rclone_python import rclone
    
    # delete a specific file on onedrive
    rclone.delete('onedrive:data/video1.mp4')
  9. Use the rclone client for remote operations

    master

    The rclone object is the primary entrypoint for interacting with rclone. It provides methods to manage remotes, list files, copy data, delete files, and calculate hashes. Most methods accept remote paths in the format remote_name:path/to/file.

    from rclone_python import rclone, remote_types
    from rclone_python.hash_types import HashTypes
    
    # Create a new remote
    rclone.create_remote("box", remote_types.RemoteTypes.box)
    
    # List remotes
    print(rclone.get_remotes())
    
    # List files with specific filters
    print(rclone.ls("box:data", max_depth=1, files_only=True))
    
    # Copy a remote directory to a local folder
    rclone.copy("box:data", "data")
    
    # Copy a local file to a remote destination
    rclone.copy("some_local_folder/image.jpg", "box:new_image.jpg")
    
    # Get a file hash
    print(rclone.hash(HashTypes.sha1, "box:data"))