Skip to content

Upload

py_moodle.upload

File uploading module for Moodle CLI using the webservice endpoint.

This method requires a webservice token with 'core_files_upload' capability.

Classes:

Name Description
MoodleUploadError

Exception for webservice upload errors.

ProgressTracker

A file-like object that tracks the progress of reads and calls a callback.

Functions:

Name Description
upload_file_webservice

Uploads a file to the user's private draft area using webservice/upload.php

Classes

MoodleUploadError

Bases: Exception

Exception for webservice upload errors.

ProgressTracker

ProgressTracker(
    file_path: str,
    progress_callback: Optional[
        Callable[[int], None]
    ] = None,
)

A file-like object that tracks the progress of reads and calls a callback. This is used to monitor file uploads with a progress bar.

Methods:

Name Description
read

Attributes:

Name Type Description
size
read_so_far
callback

Attributes

size instance-attribute
size = getsize(file_path)
read_so_far instance-attribute
read_so_far = 0
callback instance-attribute
callback = progress_callback

Functions

read
read(size=-1)

Functions

upload_file_webservice

upload_file_webservice(
    base_url: str,
    token: str,
    file_path: str,
    timeout: tuple = (30, 3600),
    progress_callback: Optional[
        Callable[[int], None]
    ] = None,
) -> int

Uploads a file to the user's private draft area using webservice/upload.php and returns its draft itemid.

Parameters:

Name Type Description Default
base_url str

The base URL of the Moodle instance.

required
token str

A valid Moodle webservice token.

required
file_path str

The local path to the file to upload.

required
timeout tuple

Request timeout in seconds. default (30, 3600) 30s to connect, 1h to upload

(30, 3600)
progress_callback Optional[Callable[[int], None]]

Optional function to call with bytes uploaded.

None

Returns:

Type Description
int

The integer itemid of the newly created draft area.

Raises:

Type Description
MoodleUploadError

If the upload fails or the response is invalid.