Awesome-LLMOps
66 строк · 1.9 Кб
1#!/usr/bin/env python3
2
3import sys4import shutil5
6filename = "README.md"7filename_backup = "README.md.backup"8
9
10def is_link_line(line) -> bool:11"""Return true if the line is a link line."""12if len(line) < 3 or line[0:3] != "- [":13return False14return True15
16def is_github_project(line) -> bool:17if "https://github.com" in line:18return True19return False20
21def contains_star_badge(line) -> bool:22if "https://img.shields.io/github/stars" in line:23return True24return False25
26
27def generate_badge_link(line) -> str:28first_right_middle_bracket = line.find("]")29# The text should be `](https://github.com/<>/<>)`30right_bracket = line[first_right_middle_bracket:].find(")") + first_right_middle_bracket31project = line[first_right_middle_bracket+2+19:right_bracket]32print("The project handle of this line is " + project)33badge_link = " ![](https://img.shields.io/github/stars/" + project + ".svg?style=social)"34if line[right_bracket+1] != " ":35badge_link += " "36newline = line[:right_bracket+1] + badge_link + line[right_bracket+1:]37print("The new line is " + newline)38return newline39
40
41def generate_star_badge(line) -> str:42"""Add the GitHub star badge if it does not exist."""43if not is_link_line(line) or not is_github_project(line):44"Return other lines unchanged."45return line46if contains_star_badge(line):47return line48print("This line does not contain the star badge: " + line)49return generate_badge_link(line)50
51
52def main() -> int:53"""Echo the input arguments to standard output"""54lines = []55with open(filename, "r") as f:56for line in f:57lines.append(generate_star_badge(line))58shutil.copyfile(filename, filename_backup)59with open(filename, "w") as f:60for line in lines:61f.write(line)62return 063
64
65if __name__ == '__main__':66sys.exit(main()) # next section explains the use of sys.exit67