File: create_badge.py

package info (click to toggle)
pytermgui 7.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,888 kB
  • sloc: python: 12,931; makefile: 40; sh: 37
file content (78 lines) | stat: -rw-r--r-- 1,852 bytes parent folder | download
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
import requests
from subprocess import check_output


def get_score(command: str) -> float:
    """Get pylint score"""

    output = check_output(command, shell=True).decode("utf-8")
    start = output.find("Your code has been rated at ")
    if start == -1:
        raise ValueError(f'Could not find quality score in "{output.rstrip()}".')

    start += len("Your code has been rated at ")
    end = start + output[start:].find("/")
    score = float(output[start:end])

    return score


def get_color(score: float) -> str:
    """Get color for shield"""

    if score < 6:
        return "critical"

    elif score < 8:
        return "orange"

    elif score < 9:
        return "yellow"

    elif score < 9.5:
        return "yellowgreen"

    else:
        return "brightgreen"


def create_link(label: str, score: float) -> str:
    """Create link using shields.io"""

    label = label.replace(" ", "_")
    color = get_color(score)
    return f"https://img.shields.io/badge/{label}-{score}-{color}"


def write_quality_badge(command: str, output_file: str) -> None:
    """Write badge for code quality"""

    score = get_score("make lint")
    link = create_link("code quality", score)

    with open(output_file, "wb") as output:
        data = requests.get(link).content
        output.write(data)


def write_version_badge(output_file: str) -> None:
    """Write badge for version badge"""

    from pytermgui import __version__ as version

    link = f"https://img.shields.io/badge/pypi_package-{version}-bright_green"

    with open(output_file, "wb") as output:
        data = requests.get(link).content
        output.write(data)


def main() -> None:
    """Main method"""

    write_quality_badge("make lint", "assets/badges/quality.svg")
    write_version_badge("assets/badges/version.svg")


if __name__ == "__main__":
    main()