MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / download_or_cache

Function download_or_cache

tools/cache_zenodo_svg.py:7–46  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

5
6
7def 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
49def _get_xdg_cache_dir():

Callers 1

Calls 2

_get_xdg_cache_dirFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…