tonic.download_utils#

Module Contents#

Functions#

calculate_md5(→ str)

check_md5(→ bool)

check_integrity(→ bool)

download_file_from_google_drive(→ None)

Download a file from Google Drive.

download_url(→ None)

Download a file from a url and place it in root.

extract_archive(→ str)

Extract an archive.

download_and_extract_archive(→ None)

iterable_to_str(→ str)

list_files(→ list[str])

List all files ending with a suffix at a given root

Attributes#

tonic.download_utils.USER_AGENT = 'tonic'#
tonic.download_utils.calculate_md5(fpath: str, chunk_size: int = 1024 * 1024) str[source]#
Parameters:
  • fpath (str) –

  • chunk_size (int) –

Return type:

str

tonic.download_utils.check_md5(fpath: str, md5: str, **kwargs: Any) bool[source]#
Parameters:
  • fpath (str) –

  • md5 (str) –

  • kwargs (Any) –

Return type:

bool

tonic.download_utils.check_integrity(fpath: str, md5: str | None = None) bool[source]#
Parameters:
  • fpath (str) –

  • md5 (str | None) –

Return type:

bool

tonic.download_utils.download_file_from_google_drive(file_id: str, root: str, filename: str | None = None, md5: str | None = None) None[source]#

Download a file from Google Drive.

Parameters:
  • file_id (str) – Google Drive file ID

  • root (str) – Directory to download to

  • filename (str | None) – Name to save the file as (optional)

  • md5 (str | None) – MD5 checksum to verify (optional)

Return type:

None

tonic.download_utils.download_url(url: str, root: str, filename: str | None = None, md5: str | None = None, max_redirect_hops: int = 3) None[source]#

Download a file from a url and place it in root.

Parameters:
  • url (str) – URL to download file from

  • root (str) – Directory to place downloaded file in

  • filename (str, optional) – Name to save the file under. If None, use the basename of the URL

  • md5 (str, optional) – MD5 checksum of the download. If None, do not check

  • max_redirect_hops (int, optional) – Maximum number of redirect hops allowed

Return type:

None

tonic.download_utils.extract_archive(from_path: str, to_path: str | None = None, remove_finished: bool = False) str[source]#

Extract an archive.

The archive type and a possible compression is automatically detected from the file name. If the file is compressed but not an archive the call is dispatched to decompress().

Parameters:
  • from_path (str) – Path to the file to be extracted.

  • to_path (str) – Path to the directory the file will be extracted to. If omitted, the directory of the file is used.

  • remove_finished (bool) – If True, remove the file after the extraction.

Returns:

Path to the directory the file was extracted to.

Return type:

(str)

tonic.download_utils.download_and_extract_archive(url: str, download_root: str, extract_root: str | None = None, filename: str | None = None, md5: str | None = None, remove_finished: bool = False) None[source]#
Parameters:
  • url (str) –

  • download_root (str) –

  • extract_root (str | None) –

  • filename (str | None) –

  • md5 (str | None) –

  • remove_finished (bool) –

Return type:

None

tonic.download_utils.iterable_to_str(iterable: collections.abc.Iterable) str[source]#
Parameters:

iterable (collections.abc.Iterable) –

Return type:

str

tonic.download_utils.list_files(root: str, suffix: str, prefix: bool = False) list[str][source]#

List all files ending with a suffix at a given root :param root: Path to directory whose folders need to be listed :type root: str :param suffix: Suffix of the files to match, e.g. ‘.png’ or (‘.jpg’, ‘.png’).

It uses the Python “str.endswith” method and is passed directly

Parameters:
  • prefix (bool, optional) – If true, prepends the path to each result, otherwise only returns the name of the files found

  • root (str) –

  • suffix (str or tuple) –

Return type:

list[str]

tonic.download_utils.T#