Cached download from URL. Parameters ---------- url: URL. Google Drive URL is also supported. path: Output filename. Default is basename of URL. quiet: Suppress terminal output. Default is False. postprocess: Function called with filename as p
(
url: str | None = None,
path: str | None = None,
quiet: bool = False,
postprocess: Callable[[str], object] | None = None,
hash: str | None = None,
**kwargs: Unpack[_DownloadKwargs],
)
| 40 | |
| 41 | |
| 42 | def cached_download( |
| 43 | url: str | None = None, |
| 44 | path: str | None = None, |
| 45 | quiet: bool = False, |
| 46 | postprocess: Callable[[str], object] | None = None, |
| 47 | hash: str | None = None, |
| 48 | **kwargs: Unpack[_DownloadKwargs], |
| 49 | ) -> str: |
| 50 | """Cached download from URL. |
| 51 | |
| 52 | Parameters |
| 53 | ---------- |
| 54 | url: |
| 55 | URL. Google Drive URL is also supported. |
| 56 | path: |
| 57 | Output filename. Default is basename of URL. |
| 58 | quiet: |
| 59 | Suppress terminal output. Default is False. |
| 60 | postprocess: |
| 61 | Function called with filename as postprocess. |
| 62 | hash: |
| 63 | Hash value of file in the format of {algorithm}:{hash_value} |
| 64 | such as sha256:abcdef.... Supported algorithms: md5, sha1, sha256, sha512. |
| 65 | kwargs: |
| 66 | Keyword arguments to be passed to `download`. |
| 67 | |
| 68 | Returns |
| 69 | ------- |
| 70 | path: |
| 71 | Output filename. |
| 72 | |
| 73 | Raises |
| 74 | ------ |
| 75 | ValueError |
| 76 | If url is not specified when path is not specified. |
| 77 | DownloadError |
| 78 | If the download fails. |
| 79 | """ |
| 80 | if path is None: |
| 81 | if url is None: |
| 82 | raise ValueError("url must be specified when path is not specified") |
| 83 | path = ( |
| 84 | url.replace("/", "-SLASH-") |
| 85 | .replace(":", "-COLON-") |
| 86 | .replace("=", "-EQUAL-") |
| 87 | .replace("?", "-QUESTION-") |
| 88 | ) |
| 89 | path = osp.join(cache_root, path) |
| 90 | |
| 91 | # check existence |
| 92 | if osp.exists(path) and not hash: |
| 93 | if not quiet: |
| 94 | print(f"File exists: {path}", file=sys.stderr) |
| 95 | return path |
| 96 | elif osp.exists(path) and hash: |
| 97 | try: |
| 98 | _assert_filehash(path=path, hash=hash, quiet=quiet) |
| 99 | return path |
nothing calls this directly
no test coverage detected
searching dependent graphs…