|
| 1 | +importre |
| 2 | +importpolib |
| 3 | +importglob |
| 4 | +importrequests |
| 5 | + |
| 6 | +frompathlibimportPath |
| 7 | + |
| 8 | + |
| 9 | +defentry_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 | +iflines_tranlated==0: |
| 18 | +result="❌" |
| 19 | +eliflines_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 | +returnresult |
| 28 | + |
| 29 | + |
| 30 | +defget_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 | +returnresult["total_count"] |
| 44 | + |
| 45 | + |
| 46 | +defget_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 | +forissueinresult["items"]: |
| 71 | +assignee=issue["assignee"]["login"]ifissue["assignee"]else"" |
| 72 | + |
| 73 | +title=issue["title"] |
| 74 | +if"翻譯"notintitleand"translate"notintitle.lower(): |
| 75 | +continue |
| 76 | + |
| 77 | +match=re.search( |
| 78 | +"(?P<dirname>[^\s`][a-zA-z-]+)/(?P<filename>[a-zA-Z0-9._-]+(.po)?)",title) |
| 79 | +ifnotmatch: |
| 80 | +continue |
| 81 | + |
| 82 | +dirname,filename=match.group('dirname','filename') |
| 83 | +ifnotfilename.endswith('.po'): |
| 84 | +filename+='.po' |
| 85 | + |
| 86 | +result_list.append(((dirname,filename),assignee,issue["html_url"])) |
| 87 | + |
| 88 | +returnresult_list |
| 89 | + |
| 90 | + |
| 91 | +defformat_line_table_header()->list: |
| 92 | +return [f"|Filename|Progress|Issue|Assignee|\r\n", |
| 93 | +f"|-------:|:-------|:----|:-------|\r\n"] |
| 94 | + |
| 95 | + |
| 96 | +defformat_issue_link(url:str)->str: |
| 97 | +returnf"[{url.split('/')[-1]}]({url})"iflen(url)>0else'' |
| 98 | + |
| 99 | + |
| 100 | +defformat_line_file(filename:str,data:dict)->str: |
| 101 | +returnf"|`{filename}`|{data['progress']}|{format_issue_link(data['issue'])}|{data['assignee']}|\r\n" |
| 102 | + |
| 103 | + |
| 104 | +defformat_line_directory(dirname:str)->str: |
| 105 | +returnf"##{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 | +forfilepathinglob.glob(str(BASE_DIR/"**/*.po"),recursive=True): |
| 118 | +path=Path(filepath) |
| 119 | +filename=path.name |
| 120 | +dirname=path.parent.nameifpath.parent.name!=BASE_DIR.nameelse'/' |
| 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_urlinissue_list: |
| 133 | +try: |
| 134 | +summary[category][filename]['issue']=issue_url |
| 135 | +summary[category][filename]['assignee']=assignee |
| 136 | +exceptKeyError: |
| 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 | +fordirname,filedictinsummary_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 | +forfilename,filedatainfiledict_sorted.items(): |
| 155 | +writeliner.append(format_line_file(filename,filedata)) |
| 156 | + |
| 157 | +withopen( |
| 158 | +f"summarize_progress/result.md", |
| 159 | +"w", |
| 160 | + )asfile: |
| 161 | +file.writelines(writeliner) |