|
| 1 | +import re |
| 2 | +import polib |
| 3 | +import glob |
| 4 | +import requests |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def entry_check(pofile: polib.POFile) -> str: |
| 10 | + ''' |
| 11 | + Check the po file with how many entries are translated or not. |
| 12 | + ''' |
| 13 | + |
| 14 | + lines_tranlated = len(pofile.translated_entries()) |
| 15 | + lines_untranlated = len(pofile.untranslated_entries()) |
| 16 | + |
| 17 | + if lines_tranlated == 0: |
| 18 | + result = "❌" |
| 19 | + elif lines_untranlated == 0: |
| 20 | + result = "✅" |
| 21 | + else: |
| 22 | + lines_all = lines_tranlated + lines_untranlated |
| 23 | + progress = lines_tranlated / lines_all |
| 24 | + progress_percentage = round(progress * 100, 2) |
| 25 | + result = f"{progress_percentage} %" |
| 26 | + |
| 27 | + return result |
| 28 | + |
| 29 | + |
| 30 | +def get_open_issues_count() -> int: |
| 31 | + ''' |
| 32 | + Fetch GitHub API to get the number of OPEN ISSUES. |
| 33 | + ''' |
| 34 | + |
| 35 | + url = f"https://api.github.com/search/issues?q=repo:python/python-docs-zh-tw+type:issue+state:open" |
| 36 | + headers = { |
| 37 | + "Accept": "application/vnd.github+json", |
| 38 | + "X-GitHub-Api-Version": "2022-11-28", |
| 39 | + } |
| 40 | + r = requests.get(url=url, headers=headers) |
| 41 | + result = r.json() |
| 42 | + |
| 43 | + return result["total_count"] |
| 44 | + |
| 45 | + |
| 46 | +def get_github_issues() -> list: |
| 47 | + ''' |
| 48 | + Fetch GitHub API to collect the infomation of OPEN ISSUES, |
| 49 | + including issue title and assignee. |
| 50 | +
|
| 51 | + Steps: |
| 52 | + 1. Fetch GitHub API and get open issue list |
| 53 | + 2. Filter the issue if it have no "Translate" in the title |
| 54 | + 3. Filter the issue if it have no correct filepath in the title |
| 55 | +
|
| 56 | + Expected Output: |
| 57 | + [ ((dirname, filename), assignee_id, issue_url), ... ] |
| 58 | + ''' |
| 59 | + NUMBER_OF_ISSUES = get_open_issues_count() |
| 60 | + |
| 61 | + url = f"https://api.github.com/search/issues?q=repo:python/python-docs-zh-tw+type:issue+state:open&per_page={NUMBER_OF_ISSUES}" |
| 62 | + headers = { |
| 63 | + "Accept": "application/vnd.github+json", |
| 64 | + "X-GitHub-Api-Version": "2022-11-28", |
| 65 | + } |
| 66 | + r = requests.get(url=url, headers=headers) |
| 67 | + result = r.json() |
| 68 | + |
| 69 | + result_list = [] |
| 70 | + for issue in result["items"]: |
| 71 | + assignee = issue["assignee"]["login"] if issue["assignee"] else "" |
| 72 | + |
| 73 | + title = issue["title"] |
| 74 | + if "翻譯" not in title and "translate" not in title.lower(): |
| 75 | + continue |
| 76 | + |
| 77 | + match = re.search( |
| 78 | + "(?P<dirname>[^\s`][a-zA-z-]+)/(?P<filename>[a-zA-Z0-9._-]+(.po)?)", title) |
| 79 | + if not match: |
| 80 | + continue |
| 81 | + |
| 82 | + dirname, filename = match.group('dirname', 'filename') |
| 83 | + if not filename.endswith('.po'): |
| 84 | + filename += '.po' |
| 85 | + |
| 86 | + result_list.append(((dirname, filename), assignee, issue["html_url"])) |
| 87 | + |
| 88 | + return result_list |
| 89 | + |
| 90 | + |
| 91 | +def format_line_table_header() -> list: |
| 92 | + return [f"|Filename|Progress|Issue|Assignee|\r\n", |
| 93 | + f"|-------:|:-------|:----|:-------|\r\n"] |
| 94 | + |
| 95 | + |
| 96 | +def format_issue_link(url: str) -> str: |
| 97 | + return f"[{url.split('/')[-1]}]({url})" if len(url) > 0 else '' |
| 98 | + |
| 99 | + |
| 100 | +def format_line_file(filename: str, data: dict) -> str: |
| 101 | + return f"|`{filename}`|{data['progress']}|{format_issue_link(data['issue'])}|{data['assignee']}|\r\n" |
| 102 | + |
| 103 | + |
| 104 | +def format_line_directory(dirname: str) -> str: |
| 105 | + return f"## {dirname}\r\n" |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + issue_list = get_github_issues() |
| 110 | + |
| 111 | + ''' |
| 112 | + Search all the po file in the directory, |
| 113 | + and record the translation progress of each files. |
| 114 | + ''' |
| 115 | + BASE_DIR = Path("../") |
| 116 | + summary = {} |
| 117 | + for filepath in glob.glob(str(BASE_DIR / "**/*.po"), recursive=True): |
| 118 | + path = Path(filepath) |
| 119 | + filename = path.name |
| 120 | + dirname = path.parent.name if path.parent.name != BASE_DIR.name else '/' |
| 121 | + po = polib.pofile(filepath) |
| 122 | + |
| 123 | + summary.setdefault(dirname, {})[filename] = { |
| 124 | + 'progress': entry_check(po), |
| 125 | + 'issue': '', |
| 126 | + 'assignee': '', |
| 127 | + } |
| 128 | + |
| 129 | + ''' |
| 130 | + Unpack the open issue list, and add assignee after the progress |
| 131 | + ''' |
| 132 | + for (category, filename), assignee, issue_url in issue_list: |
| 133 | + try: |
| 134 | + summary[category][filename]['issue'] = issue_url |
| 135 | + summary[category][filename]['assignee'] = assignee |
| 136 | + except KeyError: |
| 137 | + pass |
| 138 | + |
| 139 | + ''' |
| 140 | + Adding Space for Formatting Markdown Link |
| 141 | + ''' |
| 142 | + |
| 143 | + ''' |
| 144 | + Format the lines that will write into the markdown file, |
| 145 | + also sort the directory name and file name. |
| 146 | + ''' |
| 147 | + writeliner = [] |
| 148 | + summary_sorted = dict(sorted(summary.items())) |
| 149 | + for dirname, filedict in summary_sorted.items(): |
| 150 | + writeliner.append(format_line_directory(dirname)) |
| 151 | + writeliner.extend(format_line_table_header()) |
| 152 | + |
| 153 | + filedict_sorted = dict(sorted(filedict.items())) |
| 154 | + for filename, filedata in filedict_sorted.items(): |
| 155 | + writeliner.append(format_line_file(filename, filedata)) |
| 156 | + |
| 157 | + with open( |
| 158 | + f"summarize_progress/result.md", |
| 159 | + "w", |
| 160 | + ) as file: |
| 161 | + file.writelines(writeliner) |
0 commit comments