Path: /utilities/util_download_handler.py
This module provides a download_file function to reliably download files from a given URL into a temporary Talon directory, with built-in retry logic and user-facing error popups.
Download required installer or resource files during Talon setup or update processes.
Fetch external assets with automatic retry and SSL verification for robustness.
util_ssl.create_ssl_context for secure HTTP(S) connections
util_logger.logger and util_error_popup.show_error_popup for logging and error reporting
Parameter | Type | Description | Default |
---|---|---|---|
url | string | The HTTP(S) URL of the file to download. | — |
dest_name | string | None | Optional custom filename to save as; if omitted, derived from the URL path. | None |
retries | int | Number of times to retry the download on failure. | 3 |
from utilities.util_download_handler import download_file
# Download with default retries and inferred filename
success = download_file("https://example.com/assets.zip")
# Download with custom filename and 5 retries
success = download_file(
url="https://example.com/data.json",
dest_name="data.json",
retries=5
)
When invoked, download_file creates a %TEMP%/talon
directory if it doesn’t exist, determines the target filename (from dest_name or URL path), and builds an SSL context using create_ssl_context. It then attempts to download the file via urllib.request.urlopen, writing the response to disk. It logs each attempt and on success returns True. If all retries fail, it logs an error, shows an error popup, and returns False.
Directory creation and filename parsing errors are caught and reported via logger.error with a popup (allow_continue=True). Download errors on each attempt are logged as warnings; after the final failed retry, an error is logged and a blocking popup is shown, then the function returns False.
Uses a trusted certifi certificate bundle for SSL, falling back to the system store on failure. Only HTTP(S) URLs are supported by urllib. No additional validation is performed on dest_name, so callers should ensure it does not contain unsafe path components.
Logs include:
- INFO: “Attempt {n}/{retries}: Downloading {url} to {path}” and “Successfully downloaded …”
- WARNING: “Download attempt {n} failed: {error}”
- ERROR: Directory creation failures or final download failure after all retries.
Unit tests can mock urllib.request.urlopen to simulate success and failure, verifying return values and logged messages. Manual validation involves calling download_file with valid and invalid URLs and checking that files appear in the temp directory or that error popups are shown.
Author: Raven Development Team