Awesome-LLMOps

Форк
0
/
generate-star-badges.py 
66 строк · 1.9 Кб
1
#!/usr/bin/env python3
2

3
import sys
4
import shutil
5

6
filename = "README.md"
7
filename_backup = "README.md.backup"
8

9

10
def is_link_line(line) -> bool:
11
    """Return true if the line is a link line."""
12
    if len(line) < 3 or line[0:3] != "- [":
13
        return False
14
    return True
15

16
def is_github_project(line) -> bool:
17
    if "https://github.com" in line:
18
        return True
19
    return False
20

21
def contains_star_badge(line) -> bool:
22
    if "https://img.shields.io/github/stars" in line:
23
        return True
24
    return False
25

26

27
def generate_badge_link(line) -> str:
28
    first_right_middle_bracket = line.find("]")
29
    # The text should be `](https://github.com/<>/<>)`
30
    right_bracket = line[first_right_middle_bracket:].find(")") + first_right_middle_bracket
31
    project = line[first_right_middle_bracket+2+19:right_bracket]
32
    print("The project handle of this line is " + project)
33
    badge_link = " ![](https://img.shields.io/github/stars/" + project + ".svg?style=social)"
34
    if line[right_bracket+1] != " ":
35
        badge_link += " "
36
    newline = line[:right_bracket+1] + badge_link + line[right_bracket+1:]
37
    print("The new line is " + newline)
38
    return newline
39

40

41
def generate_star_badge(line) -> str:
42
    """Add the GitHub star badge if it does not exist."""
43
    if not is_link_line(line) or not is_github_project(line):
44
        "Return other lines unchanged."
45
        return line
46
    if contains_star_badge(line):
47
        return line
48
    print("This line does not contain the star badge: " + line)
49
    return generate_badge_link(line)
50

51

52
def main() -> int:
53
    """Echo the input arguments to standard output"""
54
    lines = []
55
    with open(filename, "r") as f:
56
        for line in f:
57
            lines.append(generate_star_badge(line))
58
    shutil.copyfile(filename, filename_backup)
59
    with open(filename, "w") as f:
60
        for line in lines:
61
            f.write(line)
62
    return 0
63

64

65
if __name__ == '__main__':
66
    sys.exit(main())  # next section explains the use of sys.exit
67

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.