tonic.download_utils#

Module Contents#

Functions#

calculate_md5(→ str)

check_md5(→ bool)

check_integrity(→ bool)

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#

USER_AGENT

T

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: Optional[str] = None) bool[source]#
Parameters:
  • fpath (str) –

  • md5 (Optional[str]) –

Return type:

bool

tonic.download_utils.download_url(url: str, root: str, filename: Optional[str] = None, md5: Optional[str] = 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: Optional[str] = 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: Optional[str] = None, filename: Optional[str] = None, md5: Optional[str] = None, remove_finished: bool = False) None[source]#
Parameters:
  • url (str) –

  • download_root (str) –

  • extract_root (Optional[str]) –

  • filename (Optional[str]) –

  • md5 (Optional[str]) –

  • remove_finished (bool) –

Return type:

None

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

iterable (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#