Debian Codegraph @

utils/create_badge.py

raw symbols SCIP
1 import requests
2 from subprocess import check_output
3
4
5 def get_score(command: str) -> float:
6 """Get pylint score"""
7
8 output = check_output(command, shell=True).decode("utf-8")
9 start = output.find("Your code has been rated at ")
10 if start == -1:
11 raise ValueError(f'Could not find quality score in "{output.rstrip()}".')
12
13 start += len("Your code has been rated at ")
14 end = start + output[start:].find("/")
15 score = float(output[start:end])
16
17 return score
18
19
20 def get_color(score: float) -> str:
21 """Get color for shield"""
22
23 if score < 6:
24 return "critical"
25
26 elif score < 8:
27 return "orange"
28
29 elif score < 9:
30 return "yellow"
31
32 elif score < 9.5:
33 return "yellowgreen"
34
35 else:
36 return "brightgreen"
37
38
39 def create_link(label: str, score: float) -> str:
40 """Create link using shields.io"""
41
42 label = label.replace(" ", "_")
43 color = get_color(score)
44 return f"https://img.shields.io/badge/{label}-{score}-{color}"
45
46
47 def write_quality_badge(command: str, output_file: str) -> None:
48 """Write badge for code quality"""
49
50 score = get_score("make lint")
51 link = create_link("code quality", score)
52
53 with open(output_file, "wb") as output:
54 data = requests.get(link).content
55 output.write(data)
56
57
58 def write_version_badge(output_file: str) -> None:
59 """Write badge for version badge"""
60
61 from pytermgui import __version__ as version
62
63 link = f"https://img.shields.io/badge/pypi_package-{version}-bright_green"
64
65 with open(output_file, "wb") as output:
66 data = requests.get(link).content
67 output.write(data)
68
69
70 def main() -> None:
71 """Main method"""
72
73 write_quality_badge("make lint", "assets/badges/quality.svg")
74 write_version_badge("assets/badges/version.svg")
75
76
77 if __name__ == "__main__":
78 main()