4
4
import datetime
5
5
import requests
6
6
7
+ from pathlib import Path
8
+
9
+
10
+ def entry_check (pofile : polib .POFile ) -> str :
11
+ '''
12
+ Check the po file with how many entries are translated or not.
13
+ '''
7
14
8
- def entry_check (pofile ) -> str :
9
15
lines_tranlated = len (pofile .translated_entries ())
10
16
lines_untranlated = len (pofile .untranslated_entries ())
11
17
@@ -17,12 +23,16 @@ def entry_check(pofile) -> str:
17
23
lines_all = lines_tranlated + lines_untranlated
18
24
progress = lines_tranlated / lines_all
19
25
progress_percentage = round (progress * 100 , 2 )
20
- result = f"Ongoing, { str ( progress_percentage ) } %"
26
+ result = f"Ongoing, { progress_percentage } %"
21
27
22
28
return result
23
29
24
30
25
31
def get_open_issues_count () -> int :
32
+ '''
33
+ Fetch GitHub API to get the number of OPEN ISSUES.
34
+ '''
35
+
26
36
url = f"https://api.github.com/search/issues?q=repo:python/python-docs-zh-tw+type:issue+state:open"
27
37
headers = {
28
38
"Accept" : "application/vnd.github+json" ,
@@ -35,6 +45,16 @@ def get_open_issues_count() -> int:
35
45
36
46
37
47
def get_github_issues () -> list :
48
+ '''
49
+ Fetch GitHub API to collect the infomation of OPEN ISSUES,
50
+ including issue title and assignee.
51
+
52
+ Steps:
53
+ 1. Fetch GitHub API and get open issue list
54
+ 2. Filter the issue if it have no assignee
55
+ 3. Filter the issue if it have no "Translate" in the title
56
+ 4. Filter the issue if it have no correct filepath in the title
57
+ '''
38
58
NUMBER_OF_ISSUES = get_open_issues_count ()
39
59
40
60
url = f"https://api.github.com/search/issues?q=repo:python/python-docs-zh-tw+type:issue+state:open&per_page={ NUMBER_OF_ISSUES } "
@@ -47,92 +67,72 @@ def get_github_issues() -> list:
47
67
48
68
result_list = []
49
69
for issue in result ["items" ]:
50
- title_segments = issue ["title" ].split ()
51
-
52
- if len (title_segments ) < 2 :
53
- continue
54
- if title_segments [0 ] != "翻譯" and title_segments [0 ].lower () != "translate" :
55
- continue
56
70
if issue ["assignee" ] is None :
57
71
continue
58
72
59
- filename = title_segments [1 ].strip ("`" )
73
+ title = issue ["title" ]
74
+ if "翻譯" not in title and "translate" not in title .lower ():
75
+ continue
60
76
61
- if re .fullmatch ("[a-zA-z-]+/[a-zA-Z0-9._-]+.po" , filename ):
62
- filename_segments = filename .split ("/" )
63
- # print(filename_segments)
64
- elif re .fullmatch ("[a-zA-z-]+/[a-zA-Z0-9._-]+" , filename ):
65
- filename_segments = filename .split ("/" )
66
- filename_segments [1 ] += ".po"
67
- else :
77
+ match = re .search ("(?P<dirname>[^\s`][a-zA-z-]+)/(?P<filename>[a-zA-Z0-9._-]+(.po)?)" , title )
78
+ if not match :
68
79
continue
80
+
81
+ dirname , filename = match .group ('dirname' , 'filename' )
82
+ if not filename .endswith ('.po' ):
83
+ filename += '.po'
69
84
70
- result_list .append ([ filename_segments , issue ["assignee" ]["login" ]] )
85
+ result_list .append ((( dirname , filename ), issue ["assignee" ]["login" ]) )
71
86
72
87
return result_list
73
88
74
-
75
- def format_line_file (filename , result ) -> str :
89
+ def format_line_file (filename : str , result : str ) -> str :
76
90
return f" - { filename .ljust (37 , '-' )} { result } \r \n "
77
91
78
92
79
- def format_line_directory (dirname ) -> str :
80
- tmp = f"- { dirname } /\r "
81
- return tmp
93
+ def format_line_directory (dirname : str ) -> str :
94
+ return f"- { dirname } /\r \n "
82
95
83
96
84
97
if __name__ == "__main__" :
85
98
issue_list = get_github_issues ()
86
99
87
- directories = [
88
- "c-api" ,
89
- "distributing" ,
90
- "extending" ,
91
- "faq" ,
92
- "howto" ,
93
- "includes" ,
94
- "installing" ,
95
- "library" ,
96
- "reference" ,
97
- "tutorial" ,
98
- "using" ,
99
- "whatsnew" ,
100
- ]
101
-
100
+ '''
101
+ Search all the po file in the directory,
102
+ and record the translation progress of each files.
103
+ '''
104
+ BASE_DIR = Path ("../" )
102
105
summary = {}
103
-
104
- file_list = glob .glob ("./../**/*.po" , recursive = True )
105
- file_list .sort ()
106
-
107
- for filepath in file_list :
108
- if len (filepath .split ("/" )) == 4 : # in-dir files
109
- _ , _ , dirname , filename = filepath .split ("/" )
110
- else : # root dir files
111
- _ , _ , filename = filepath .split ("/" )
112
- dirname = "/"
113
-
114
- if dirname not in summary :
115
- summary [dirname ] = {}
116
-
106
+ for filepath in glob .glob (str (BASE_DIR / "**/*.po" ), recursive = True ):
107
+ path = Path (filepath )
108
+ filename = path .name
109
+ dirname = path .parent .name if path .parent .name != BASE_DIR .name else '/'
117
110
po = polib .pofile (filepath )
118
- result = entry_check (po )
119
- summary [dirname ][filename ] = result
111
+ summary .setdefault (dirname , {})[filename ] = entry_check (po )
120
112
113
+ '''
114
+ Unpack the open issue list, and add assignee after the progress
115
+ '''
121
116
for (category , filename ), assignee in issue_list :
122
117
try :
123
118
summary [category ][filename ] += f", 💻 { assignee } "
124
119
except KeyError :
125
120
pass
126
121
122
+ '''
123
+ Format the lines that will write into the markdown file,
124
+ also sort the directory name and file name.
125
+ '''
127
126
writeliner = []
128
- for dirname , filedict in summary .items ():
127
+ summary_sorted = dict (sorted (summary .items ()))
128
+ for dirname , filedict in summary_sorted .items ():
129
129
writeliner .append (format_line_directory (dirname
7720
span>))
130
- for filename , result in filedict .items ():
130
+ filedict_sorted = dict (sorted (filedict .items ()))
131
+ for filename , result in filedict_sorted .items ():
131
132
writeliner .append (format_line_file (filename , result ))
132
133
133
- timestamp = datetime .datetime .now ().strftime ("%Y%m%d_%H%M%S" )
134
134
with open (
135
- f"summarize_progress/dist/summarize_progress_ { timestamp } .md" ,
135
+ f"summarize_progress/dist/summarize_progress .md" ,
136
136
"w" ,
137
137
) as file :
138
138
file .writelines (writeliner )
0 commit comments