Get bytes from the given url or local cache. Parameters ---------- url : str The url to download. sha : str The sha256 of the file. Returns ------- BytesIO The file loaded into memory.
(url, version)
| 5 | |
| 6 | |
| 7 | def download_or_cache(url, version): |
| 8 | """ |
| 9 | Get bytes from the given url or local cache. |
| 10 | |
| 11 | Parameters |
| 12 | ---------- |
| 13 | url : str |
| 14 | The url to download. |
| 15 | sha : str |
| 16 | The sha256 of the file. |
| 17 | |
| 18 | Returns |
| 19 | ------- |
| 20 | BytesIO |
| 21 | The file loaded into memory. |
| 22 | """ |
| 23 | cache_dir = _get_xdg_cache_dir() |
| 24 | |
| 25 | if cache_dir is not None: # Try to read from cache. |
| 26 | try: |
| 27 | data = (cache_dir / version).read_bytes() |
| 28 | except OSError: |
| 29 | pass |
| 30 | else: |
| 31 | return BytesIO(data) |
| 32 | |
| 33 | with urllib.request.urlopen( |
| 34 | urllib.request.Request(url, headers={"User-Agent": ""}) |
| 35 | ) as req: |
| 36 | data = req.read() |
| 37 | |
| 38 | if cache_dir is not None: # Try to cache the downloaded file. |
| 39 | try: |
| 40 | cache_dir.mkdir(parents=True, exist_ok=True) |
| 41 | with open(cache_dir / version, "xb") as fout: |
| 42 | fout.write(data) |
| 43 | except OSError: |
| 44 | pass |
| 45 | |
| 46 | return BytesIO(data) |
| 47 | |
| 48 | |
| 49 | def _get_xdg_cache_dir(): |
no test coverage detected
searching dependent graphs…