-
Notifications
You must be signed in to change notification settings - Fork 0
/
badges.py
334 lines (267 loc) · 18.6 KB
/
badges.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from __future__ import annotations
import math
import posixpath
from dataclasses import dataclass
from functools import wraps
from pathlib import Path
from typing import Any, Callable, Literal, TypeAlias, TypedDict
import requests
__all__ = [
"STYLES",
"Color",
"Name",
"Url",
"Create",
"sheild_io_link",
"badge",
"Badges",
"collect_and_write",
"Parameters",
"PRESETS"
]
LiteralAlias: TypeAlias = type(Literal[""])
PRESETS = {
"made_with_python": """\
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="189.75"
height="28" role="img" aria-label="MADE WITH: PYTHON">
<title>MADE WITH: PYTHON</title>
<g shape-rendering="crispEdges">
<rect width="88.25" height="28" fill="#ef4041" />
<rect x="88.25" width="102.5" height="28" fill="#c1282d" />
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif"
text-rendering="geometricPrecision" font-size="100">
<text transform="scale(.1)" x="434.25" y="175" textLength="712.5" fill="#fff">MADE WITH</text>
<text transform="scale(.1)" x="1260" y="175" textLength="535" fill="#fff" font-weight="bold">
PYTHON </text>
<image xmlns="http://www.w3.org/2000/svg" x="164" y="7" width="14" height="14" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="data:image/svg+xml;base64,PHN2ZyBmaWxsPSJ3aGl0ZSIgcm9sZT0iaW1nIiB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHRpdGxlPlB5dGhvbjwvdGl0bGU+PHBhdGggZD0iTTE0LjI1LjE4bC45LjIuNzMuMjYuNTkuMy40NS4zMi4zNC4zNC4yNS4zNC4xNi4zMy4xLjMuMDQuMjYuMDIuMi0uMDEuMTNWOC41bC0uMDUuNjMtLjEzLjU1LS4yMS40Ni0uMjYuMzgtLjMuMzEtLjMzLjI1LS4zNS4xOS0uMzUuMTQtLjMzLjEtLjMuMDctLjI2LjA0LS4yMS4wMkg4Ljc3bC0uNjkuMDUtLjU5LjE0LS41LjIyLS40MS4yNy0uMzMuMzItLjI3LjM1LS4yLjM2LS4xNS4zNy0uMS4zNS0uMDcuMzItLjA0LjI3LS4wMi4yMXYzLjA2SDMuMTdsLS4yMS0uMDMtLjI4LS4wNy0uMzItLjEyLS4zNS0uMTgtLjM2LS4yNi0uMzYtLjM2LS4zNS0uNDYtLjMyLS41OS0uMjgtLjczLS4yMS0uODgtLjE0LTEuMDUtLjA1LTEuMjMuMDYtMS4yMi4xNi0xLjA0LjI0LS44Ny4zMi0uNzEuMzYtLjU3LjQtLjQ0LjQyLS4zMy40Mi0uMjQuNC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xNC41LS4yMS40MS0uMjguMzMtLjMyLjI3LS4zNS4yLS4zNi4xNS0uMzYuMS0uMzUuMDctLjMyLjA0LS4yOC4wMi0uMjFWNi4wN2gyLjA5bC4xNC4wMXptLTYuNDcgMTQuMjVsLS4yMy4zMy0uMDguNDEuMDguNDEuMjMuMzMuMzMuMjMuNDEuMDguNDEtLjA4LjMzLS4yMy4yMy0uMzMuMDgtLjQxLS4wOC0uNDEtLjIzLS4zMy0uMzMtLjIzLS40MS0uMDgtLjQxLjA4eiIvPjwvc3ZnPg=="/>
</g>
</svg>\
""",
"made_with_rust": """\
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="189.75"
height="28" role="img" aria-label="MADE WITH: PYTHON">
<title>MADE WITH: PYTHON</title>
<g shape-rendering="crispEdges">
<rect width="88.25" height="28" fill="#ef4041" />
<rect x="88.25" width="75.5" height="28" fill="#c1282d" />
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif"
text-rendering="geometricPrecision" font-size="100">
<text transform="scale(.1)" x="434.25" y="175" textLength="712.5" fill="#fff">MADE WITH</text>
<text transform="scale(.1)" x="1150" y="175" textLength="350" fill="#fff" font-weight="bold">
RUST </text>
<image xmlns="http://www.w3.org/2000/svg" x="140" y="7" width="14" height="14" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="data:image/svg+xml;base64,PHN2ZyBmaWxsPSJ3aGl0ZSIgcm9sZT0iaW1nIiB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHRpdGxlPlJ1c3Q8L3RpdGxlPjxwYXRoIGQ9Ik0yMy44MzQ2IDExLjcwMzNsLTEuMDA3My0uNjIzNmExMy43MjY4IDEzLjcyNjggMCAwMC0uMDI4My0uMjkzNmwuODY1Ni0uODA2OWEuMzQ4My4zNDgzIDAgMDAtLjExNTQtLjU3OGwtMS4xMDY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MjUuODcxN3ptLTkuMzAwNi05LjU5OTNhLjcxMjguNzEyOCAwIDExLjk4NCAxLjAzMTYuNzEzNy43MTM3IDAgMDEtLjk4NC0xLjAzMTZ6bTguMzM4OSA2LjcxYS43MTA3LjcxMDcgMCAwMS45Mzk1LS4zNjI1LjcxMzcuNzEzNyAwIDExLS45NDA1LjM2MzV6Ii8+PC9zdmc+"/>
</g>
</svg>\
"""
}
STYLES: LiteralAlias = Literal[
"plastic", "flat", "flat-square", "for-the-badge", "social"
]
class Parameters(TypedDict, total=False):
style: STYLES
"""Customize the button with the following styles:
- plastic
- flat
- flat-square
- for-the-badge
- social
Example: `?style=flat-square`
"""
label: str
"""Override the default left-hand-side text (URL-Encoding needed for spaces or special characters!)
Example: `?label=healthinesses`
URL-Encoding: https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding
"""
logo: str
"""Insert one of the named logos from (bitcoin, dependabot, gitlab, npm, paypal, serverfault,
stackexchange, superuser, telegram, travis) or simple-icons. Simple-icons are referenced using icon
slugs which can be found on the simple-icons site or in the slugs.md file in the simple-icons repository.
Example: `?logo=appveyor`
Simple-icons: https://simpleicons.org/
slugs.md: https://github.com/simple-icons/simple-icons/blob/develop/slugs.md
"""
logoData: str
"""Insert custom logo image (≥ 14px high). There is a limit on the total size of request headers
we can accept (8192 bytes). From a practical perspective, this means the base64-encoded image text
is limited to somewhere slightly under 8192 bytes depending on the rest of the request header.
Example: `?logo=data:image/png;base64,...`
"""
logoColor: str
"""Set the color of the logo (hex, rgb, rgba, hsl, hsla and css named colors supported).
Supported for named logos and Shields logos but not for custom logos. For multicolor Shields
logos, the corresponding named logo will be used and colored.
Example: `?logoColor=violet`
"""
logoWidth: int
"""Set the horizontal space to give to the logo
Example: `?logoWidth=40`
"""
link: str
"""Specify what clicking on the left/right of a badge should do. Note that this only works
when integrating your badge in an `<object>` HTML tag, but not an `<img>` tag or a markup
language.
Example: `?link=http://left&link=http://right`
"""
labelColor: str
"""Set background of the left part (hex, rgb, rgba, hsl, hsla and css named colors supported).
The legacy name "colorA" is also supported.
Example: `?labelColor=abcdef`
"""
color: str
"""Set background of the right part (hex, rgb, rgba, hsl, hsla and css named colors supported).
The legacy name "colorB" is also supported.
Example: `?color=fedcba`
"""
cacheSeconds: int
"""Set the HTTP cache lifetime (rules are applied to infer a default value on a per-badge basis,
any values specified below the default will be ignored). The legacy name "maxAge" is also supported.
Example: `?cacheSeconds=3600`
"""
def _validate_parameters(parameters: Parameters):
if "style" in parameters and parameters["style"] not in STYLES.__args__:
raise ValueError(
f"Unkown badge style {parameters['style']!r}. Expected one of: {', '.join(STYLES.__args__)}"
)
@dataclass
class Color:
RED = "FF0000"
RED_ORANGE = "FF5300"
ORANGE = "FFA700"
ORANGE_YELLOW = "FFD200"
YELLOW = "FFF400"
YELLOW_LIGHT_GREEN = "D1FF00"
LIGHT_GREEN = "A3FF00"
LIGHT_GREEN_GREEN = "52C000"
GREEN = "2CBA00"
All = [
RED,
RED_ORANGE,
ORANGE,
ORANGE_YELLOW,
YELLOW,
YELLOW_LIGHT_GREEN,
LIGHT_GREEN,
LIGHT_GREEN_GREEN,
GREEN,
]
@staticmethod
def percentage(score: float) -> str:
idx = math.ceil((len(Color.All) - 1) * score)
return Color.All[idx]
Name = str
Url = str
def badge(*cargs: Any, **ckwargs: Any):
"""Create a badge method. Mainly for the Badges class to allow for data to be passed
to the method when it is called by the class. This is not needed if the method doesn't
need to take arguments/data.
Args:
*cargs (Any): Positional arguments used when calling the badge method
**ckwargs (Any): Keyword arguments used when calling the badge method
Example:
import sample
@badge(project="sample") # <- This data is used later when the badge is called
def get_version_link(project: str) -> list[tuple[str, str]]:
return [
(
"version",
create_link(
create_label(f"version", sample.__version__, "white"),
logo="aiohttp"
)
),
(
"project_name",
create_link(
create_label(f"project", project, "white"),
)
)
]
"""
def badge_wrapper(func: Callable[..., list[tuple[Name, Url]]]):
@wraps(func)
def badge_caller() -> list[tuple[Name, Url]]:
return func(*cargs, **ckwargs)
return badge_caller
return badge_wrapper
def sheild_io_link(base: str, params: Parameters | None = None) -> str:
"""Create link using shields.io
Args:
base (str): Base url for the sheild.io badge that you want that goes after the
`https://img.shields.io/`. Ex. 'badge/<label>-<message>-<color>'
params (Parameters): The extra defining paramegers to pass into the url
"""
extras = ""
if params is not None and len(params) > 0:
_validate_parameters(params)
extras = "?" + "&".join(f"{key}={value}" for key, value in params.items())
return f"https://img.shields.io/{posixpath.normpath(base).strip('/')}{extras}"
def collect_and_write(out_dir: str, links: list[tuple[Name, Url]]):
path = Path(out_dir)
if not path.is_dir():
raise FileNotFoundError(f"No directory {path.as_posix()!r} found")
for link in links:
with (path / link[0]).with_suffix(".svg").open("wb") as output:
print("Generating badge:", (path / link[0]).with_suffix(".svg"))
data = requests.get(link[1]).content
output.write(data)
class Create:
@staticmethod
def badge(label: str, message: str, color: str):
return f"badge/{label.replace(' ', '_')}-{message.replace(' ', '_')}-{color}"
class Badges:
def __init__(
self,
*badges: Callable[..., list[tuple[Name, Url]] | tuple[Name, Url]],
):
self.badges: list[
Callable[..., list[tuple[Name, Url]] | tuple[Name, Url]]
] = list(badges)
def callback(
self, *badge_callbacks: Callable[..., list[tuple[Name, Url]] | tuple[Name, Url]]
):
"""Add a badge callback to the manager.
Args:
badge_callbacks (Callable[..., list[tuple[Name, Url]]]): A callback that creates tuples
of name to url relations. Signatures look like (...) -> list[tuple[str, str]] and each
item in the returned list has a name, the name of the output file, and a url, the
constructed sheilds.io url. These callbacks need to be decorated with
`@badge(<...arg>, <...key=arg>)` if you want to pass data to the callback.
"""
self.badges.extend(badge_callbacks)
def badge(self, name: str, base: str, params: Parameters | None = None):
"""Create a new badge and add it to the callbacks."""
def new_badge() -> tuple[Name, Url]:
return (
name,
sheild_io_link(
base,
params,
),
)
self.badges.append(new_badge)
return self
def collect(self, _path: str):
"""Collect the name and urls for each badge, generate the svg, and write it to a file.
Args:
path (str): Path to the base directory where each badge should be written to.
"""
if _path is None:
raise ValueError("Must provide an output directory for the badge svg files")
path = Path(_path)
if not path.is_dir():
raise FileNotFoundError(f"No directory {path.as_posix()!r} found")
for badge in self.badges:
result = badge()
if isinstance(result, list):
for link in result:
with (path / link[0]).with_suffix(".svg").open("wb") as output:
print("Generating badge:", (path / link[0]).with_suffix(".svg"))
data = requests.get(link[1]).content
output.write(data)
else:
with (path / result[0]).with_suffix(".svg").open("wb") as output:
print("Generating badge:", (path / result[0]).with_suffix(".svg"))
data = requests.get(result[1]).content
output.write(data)