forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_zenodo_svg.py
142 lines (128 loc) · 3.92 KB
/
cache_zenodo_svg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import urllib.request
from io import BytesIO
import os
from pathlib import Path
def download_or_cache(url, version):
"""
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.
"""
cache_dir = _get_xdg_cache_dir()
if cache_dir is not None: # Try to read from cache.
try:
data = (cache_dir / version).read_bytes()
except IOError:
pass
else:
return BytesIO(data)
with urllib.request.urlopen(
urllib.request.Request(url, headers={"User-Agent": ""})
) as req:
data = req.read()
if cache_dir is not None: # Try to cache the downloaded file.
try:
cache_dir.mkdir(parents=True, exist_ok=True)
with open(cache_dir / version, "xb") as fout:
fout.write(data)
except IOError:
pass
return BytesIO(data)
def _get_xdg_cache_dir():
"""
Return the XDG cache directory.
See
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
"""
cache_dir = os.environ.get("XDG_CACHE_HOME")
if not cache_dir:
cache_dir = os.path.expanduser("~/.cache")
if cache_dir.startswith("~/"): # Expansion failed.
return None
return Path(cache_dir, "matplotlib")
if __name__ == "__main__":
data = {
"v3.4.3": "5194481",
"v3.4.2": "4743323",
"v3.4.1": "4649959",
"v3.4.0": "4638398",
"v3.3.4": "4475376",
"v3.3.3": "4268928",
"v3.3.2": "4030140",
"v3.3.1": "3984190",
"v3.3.0": "3948793",
"v3.2.2": "3898017",
"v3.2.1": "3714460",
"v3.2.0": "3695547",
"v3.1.3": "3633844",
"v3.1.2": "3563226",
"v3.1.1": "3264781",
"v3.1.0": "2893252",
"v3.0.3": "2577644",
"v3.0.2": "1482099",
"v3.0.1": "1482098",
"v2.2.5": "3633833",
"v3.0.0": "1420605",
"v2.2.4": "2669103",
"v2.2.3": "1343133",
"v2.2.2": "1202077",
"v2.2.1": "1202050",
"v2.2.0": "1189358",
"v2.1.2": "1154287",
"v2.1.1": "1098480",
"v2.1.0": "1004650",
"v2.0.2": "573577",
"v2.0.1": "570311",
"v2.0.0": "248351",
"v1.5.3": "61948",
"v1.5.2": "56926",
"v1.5.1": "44579",
"v1.5.0": "32914",
"v1.4.3": "15423",
"v1.4.2": "12400",
"v1.4.1": "12287",
"v1.4.0": "11451",
}
doc_dir = Path(__file__).parent.parent.absolute() / "doc"
target_dir = doc_dir / "_static/zenodo_cache"
citing = doc_dir / "citing.rst"
target_dir.mkdir(exist_ok=True, parents=True)
header = []
footer = []
with open(citing, "r") as fin:
target = header
for ln in fin:
if target is not None:
target.append(ln.rstrip())
if ln.strip() == ".. START OF AUTOGENERATED":
target.extend(["", ""])
target = None
if ln.strip() == ".. END OF AUTOGENERATED":
target = footer
target.append(ln)
with open(citing, "w") as fout:
fout.write("\n".join(header))
for version, doi in data.items():
svg_path = target_dir / f"{doi}.svg"
if not svg_path.exists():
url = f"https://zenodo.org/badge/doi/10.5281/zenodo.{doi}.svg"
payload = download_or_cache(url, f"{doi}.svg")
with open(svg_path, "xb") as svgout:
svgout.write(payload.read())
fout.write(
f"""
{version}
.. image:: _static/zenodo_cache/{doi}.svg
:target: https://doi.org/10.5281/zenodo.{doi}"""
)
fout.write("\n\n")
fout.write("\n".join(footer))
fout.write('\n')