|
1 |
| -# Use together with `pageviews.py` |
2 |
| -# python scripts/pageviews.py | head -n 150 | grep -v whats | cut -d ' ' -f 2 | sed 's/\.html/\.po/g' | xargs -I '{}' python scripts/create_issue.py '{}' |
| 1 | +""" |
| 2 | +Run this script with one variable: |
| 3 | + - PO filename to create an issue for that file |
| 4 | + - or '--all' to create the issues for all untranslated files that doesn't have an open issue already |
| 5 | + - or '--one' to create the next one issue |
| 6 | +""" |
3 | 7 |
|
4 | 8 | importos
|
5 | 9 | importsys
|
| 10 | +fromglobimportglob |
6 | 11 | frompathlibimportPath
|
7 | 12 |
|
8 | 13 | fromgithubimportGithub
|
9 | 14 | frompotodo.potodoimportPoFileStats
|
10 | 15 |
|
11 |
| -iflen(sys.argv)!=2: |
12 |
| -print('Specify PO filename') |
13 |
| -sys.exit(1) |
| 16 | +PYTHON_VERSION="3.13" |
| 17 | +PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE=5 |
| 18 | +GOOD_FIRST_ISSUE_LABEL="good first issue" |
| 19 | +ISSUE_LABELS= [PYTHON_VERSION] |
| 20 | +ISSUE_TITLE='Translate `{pofilename}`' |
| 21 | +ISSUE_BODY='''This file is {translated_percent}% translated and needs to reach 100%. |
14 | 22 |
|
15 |
| -pofilename=sys.argv[1] |
16 |
| -pofile=PoFileStats(Path(pofilename)) |
| 23 | +The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated. |
| 24 | +Meanwhile, the English version is shown. |
17 | 25 |
|
18 |
| -g=Github(os.environ.get('GITHUB_TOKEN')) |
| 26 | +Current stats for `{pofilename}`: |
19 | 27 |
|
20 |
| -repo=g.get_repo('python/python-docs-es') |
| 28 | +- Total entries: {pofile_entries} |
21 | 29 |
|
| 30 | +- Entries that need work: {pending_entries} - ({pending_percent}%) |
| 31 | + - Fuzzy: {pofile_fuzzy} |
| 32 | + - Untranslated: {pofile_untranslated} |
22 | 33 |
|
23 |
| -issues=repo.get_issues(state='all') |
24 |
| -forissueinissues: |
25 |
| -ifpofilenameinissue.title: |
| 34 | +Please, comment here if you want this file to be assigned to you and a member will assign it to you as soon as possible, so you can start working on it. |
26 | 35 |
|
27 |
| -print(f'Skipping{pofilename}. There is a similar issue already created at{issue.html_url}') |
28 |
| -sys.exit(1) |
| 36 | +Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''' |
29 | 37 |
|
30 |
| -msg=f'There is a similar issue already created at{issue.html_url}.\nDo you want to create it anyways? [y/N] ' |
31 |
| -answer=input(msg) |
32 |
| -ifanswer!='y': |
33 |
| -sys.exit(1) |
34 | 38 |
|
35 |
| -ifpofile.fuzzy==0andany([ |
36 |
| -pofile.translated_nb==pofile.po_file_size, |
37 |
| -pofile.untranslated_nb==0, |
38 |
| -]): |
39 |
| -print(f'Skipping{pofilename}. The file is 100% translated already.') |
40 |
| -sys.exit(1) |
| 39 | +classIssueAlreadyExistingError(Exception): |
| 40 | +"""Issue already existing in GitHub""" |
41 | 41 |
|
42 |
| -# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue |
43 |
| -title=f'Translate `{pofilename}`' |
44 |
| -urlfile=pofilename.replace('.po','.html') |
45 |
| -issue=repo.create_issue( |
46 |
| -title=title, |
47 |
| -body=f'''This needs to reach 100% translated. |
48 | 42 |
|
49 |
| -The rendered version of this file will be available at https://docs.python.org/es/3.8/{urlfile} once translated. |
50 |
| -Meanwhile, the English versionisshown. |
| 43 | +classPoFileAlreadyTranslated(Exception): |
| 44 | +"""Given PO fileisalready 100% translated""" |
51 | 45 |
|
52 |
| -Current stats for `{pofilename}`: |
53 | 46 |
|
54 |
| -- Fuzzy:{pofile.fuzzy_nb} |
55 |
| -- Percent translated:{pofile.percent_translated}% |
56 |
| -- Entries:{pofile.translated_nb} /{pofile.po_file_size} |
57 |
| -- Untranslated:{pofile.untranslated_nb} |
| 47 | +classGitHubIssueGenerator: |
| 48 | +def__init__(self): |
| 49 | +g=Github(os.environ.get('GITHUB_TOKEN')) |
| 50 | +self.repo=g.get_repo('python/python-docs-es') |
| 51 | +self._issues=None |
58 | 52 |
|
59 |
| -Please, comment here if you want this file to be assigned to you and a member will assign it to you as soon as possible, so you can start working on it. |
| 53 | +@property |
| 54 | +defissues(self): |
| 55 | +ifself._issuesisNone: |
| 56 | +self._issues=self.repo.get_issues(state='open') |
| 57 | + |
| 58 | +returnself._issues |
| 59 | + |
| 60 | +defcheck_issue_not_already_existing(self,pofilename): |
| 61 | +forissueinself.issues: |
| 62 | +ifpofilenameinissue.title: |
| 63 | + |
| 64 | +print(f'Skipping{pofilename}. There is a similar issue already created at{issue.html_url}') |
| 65 | +raiseIssueAlreadyExistingError |
| 66 | + |
| 67 | + |
| 68 | +@staticmethod |
| 69 | +defcheck_translation_is_pending(pofile): |
| 70 | +no_fuzzy_translations=pofile.fuzzy==0 |
| 71 | +translated_match_all_entries=pofile.translated==pofile.entries |
| 72 | +no_untranslated_entries_left=pofile.untranslated==0 |
| 73 | + |
| 74 | +ifno_fuzzy_translationsand (translated_match_all_entriesorno_untranslated_entries_left): |
| 75 | +print(f'Skipping{pofile.filename}. The file is 100% translated already.') |
| 76 | +raisePoFileAlreadyTranslated |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +defissue_generator(self,pofilename): |
| 81 | +pofile=PoFileStats(Path(pofilename)) |
| 82 | + |
| 83 | +self.check_issue_not_already_existing(pofilename) |
| 84 | +self.check_translation_is_pending(pofile) |
| 85 | + |
| 86 | +pending_entries=pofile.fuzzy+pofile.untranslated |
| 87 | + |
| 88 | +ifpending_entries<=PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE: |
| 89 | +labels=ISSUE_LABELS+ [GOOD_FIRST_ISSUE_LABEL] |
| 90 | +else: |
| 91 | +labels=ISSUE_LABELS |
| 92 | + |
| 93 | +urlfile=pofilename.replace('.po','.html') |
| 94 | +title=ISSUE_TITLE.format(pofilename=pofilename) |
| 95 | +body=ISSUE_BODY.format( |
| 96 | +translated_percent=pofile.percent_translated, |
| 97 | +python_version=PYTHON_VERSION, |
| 98 | +urlfile=urlfile, |
| 99 | +pofilename=pofilename, |
| 100 | +pofile_fuzzy=pofile.fuzzy, |
| 101 | +pending_percent=100-pofile.percent_translated, |
| 102 | +pofile_entries=pofile.entries, |
| 103 | +pofile_untranslated=pofile.untranslated, |
| 104 | +pending_entries=pending_entries, |
| 105 | + ) |
| 106 | +# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue |
| 107 | +issue=self.repo.create_issue(title=title,body=body,labels=labels) |
| 108 | + |
| 109 | +returnissue |
| 110 | + |
| 111 | +defcreate_issues(self,only_one=False): |
| 112 | +po_files=glob("**/*.po") |
| 113 | +existing_issue_counter=0 |
| 114 | +already_translated_counter=0 |
| 115 | +created_issues_counter=0 |
| 116 | + |
| 117 | +print(f"TOTAL PO FILES:{len(po_files)}") |
| 118 | + |
| 119 | +forpofilenameinpo_files: |
| 120 | +try: |
| 121 | +issue=self.issue_generator(pofilename) |
| 122 | +created_issues_counter+=1 |
| 123 | +print(f'Issue "{issue.title}" created at{issue.html_url}') |
| 124 | +ifonly_one: |
| 125 | +break |
| 126 | +exceptIssueAlreadyExistingError: |
| 127 | +existing_issue_counter+=1 |
| 128 | +exceptPoFileAlreadyTranslated: |
| 129 | +already_translated_counter+=1 |
| 130 | + |
| 131 | +print("Stats:") |
| 132 | +print(f"- Existing issues:{existing_issue_counter}") |
| 133 | +print(f"- Already translated files:{already_translated_counter}") |
| 134 | +print(f"- Created issues:{created_issues_counter}") |
| 135 | + |
| 136 | + |
| 137 | +defmain(): |
| 138 | +error_msg="Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue" |
| 139 | +iflen(sys.argv)!=2: |
| 140 | +raiseException(error_msg) |
| 141 | + |
| 142 | +arg=sys.argv[1] |
| 143 | + |
| 144 | +gh=GitHubIssueGenerator() |
| 145 | + |
| 146 | +ifarg=="--all": |
| 147 | +gh.create_issues() |
| 148 | + |
| 149 | +elifarg=="--one": |
| 150 | +gh.create_issues(only_one=True) |
| 151 | + |
| 152 | +else: |
| 153 | +try: |
| 154 | +gh.issue_generator(arg) |
| 155 | +exceptFileNotFoundError: |
| 156 | +raiseException(error_msg) |
60 | 157 |
|
61 |
| -Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''', |
62 |
| -) |
63 |
| -print(f'Issue "{title}" created at{issue.html_url}') |
| 158 | +if__name__=="__main__": |
| 159 | +main() |