|
3 | 3 |
|
4 | 4 | import os
|
5 | 5 | import sys
|
| 6 | +from glob import glob |
6 | 7 | from pathlib import Path
|
7 | 8 |
|
8 | 9 | from github import Github
|
9 | 10 | from potodo.potodo import PoFileStats
|
10 | 11 |
|
11 |
| -if len(sys.argv) != 2: |
12 |
| - print('Specify PO filename') |
13 |
| - sys.exit(1) |
14 |
| - |
15 |
| -pofilename = sys.argv[1] |
16 |
| -pofile = PoFileStats(Path(pofilename)) |
17 | 12 |
|
18 | 13 | g = Github(os.environ.get('GITHUB_TOKEN'))
|
19 |
| - |
20 | 14 | repo = g.get_repo('python/python-docs-es')
|
21 | 15 |
|
| 16 | +PYTHON_VERSION = "3.13" |
| 17 | +ISSUE_LABELS = [PYTHON_VERSION, "good first issue"] |
| 18 | +ISSUE_TITLE = 'Translate `{pofilename}`' |
| 19 | +ISSUE_BODY = '''This needs to reach 100% translated. |
22 | 20 |
|
23 |
| -issues = repo.get_issues(state='all') |
24 |
| -for issue in issues: |
25 |
| - if pofilename in issue.title: |
| 21 | +The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated. |
| 22 | +Meanwhile, the English version is shown. |
26 | 23 |
|
27 |
| - print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') |
28 |
| - sys.exit(1) |
| 24 | +Current stats for `{pofilename}`: |
29 | 25 |
|
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 |
| - if answer != 'y': |
33 |
| - sys.exit(1) |
| 26 | +- Fuzzy: {pofile_fuzzy} |
| 27 | +- Percent translated: {pofile_percent_translated}% |
| 28 | +- Entries: {pofile_entries} |
| 29 | +- Untranslated: {pofile_untranslated} |
34 | 30 |
|
35 |
| -if pofile.fuzzy == 0 and any([ |
36 |
| - pofile.translated == pofile.entries, |
37 |
| - pofile.untranslated == 0, |
38 |
| -]): |
39 |
| - print(f'Skipping {pofilename}. The file is 100% translated already.') |
40 |
| - sys.exit(1) |
| 31 | +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. |
41 | 32 |
|
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. |
| 33 | +Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''' |
48 | 34 |
|
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 version is shown. |
51 | 35 |
|
52 |
| -Current stats for `{pofilename}`: |
| 36 | +class IssueAlreadyExistingError(Exception): |
| 37 | + """Issue already existing in GitHub""" |
53 | 38 |
|
54 |
| -- Fuzzy: {pofile.fuzzy} |
55 |
| -- Percent translated: {pofile.percent_translated}% |
56 |
| -- Entries: {pofile.entries} |
57 |
| -- Untranslated: {pofile.untranslated} |
58 | 39 |
|
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. |
| 40 | +class PoFileAlreadyTranslated(Exception): |
| 41 | + """Given PO file is already 100% translated""" |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +def check_issue_not_already_existing(pofilename): |
| 46 | + issues = repo.get_issues(state='open') |
| 47 | + for issue in issues: |
| 48 | + if pofilename in issue.title: |
| 49 | + |
| 50 | + print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') |
| 51 | + raise IssueAlreadyExistingError() |
| 52 | + |
| 53 | + |
| 54 | +def check_translation_is_pending(pofile): |
| 55 | + if pofile.fuzzy == 0 and any([ |
| 56 | + pofile.translated == pofile.entries, |
| 57 | + pofile.untranslated == 0, |
| 58 | + ]): |
| 59 | + print(f'Skipping {pofile.filename}. The file is 100% translated already.') |
| 60 | + raise PoFileAlreadyTranslated() |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +def issue_generator(pofilename): |
| 65 | + pofile = PoFileStats(Path(pofilename)) |
| 66 | + |
| 67 | + check_issue_not_already_existing(pofilename) |
| 68 | + check_translation_is_pending(pofile) |
| 69 | + |
| 70 | + urlfile = pofilename.replace('.po', '.html') |
| 71 | + title = ISSUE_TITLE.format(pofilename=pofilename) |
| 72 | + body = ISSUE_BODY.format( |
| 73 | + python_version=PYTHON_VERSION, |
| 74 | + urlfile=urlfile, |
| 75 | + pofilename=pofilename, |
| 76 | + pofile_fuzzy=pofile.fuzzy, |
| 77 | + pofile_percent_translated=pofile.percent_translated, |
| 78 | + pofile_entries=pofile.entries, |
| 79 | + pofile_untranslated=pofile.untranslated, |
| 80 | + ) |
| 81 | + # https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue |
| 82 | + issue = repo.create_issue(title=title, body=body, labels=ISSUE_LABELS) |
| 83 | + |
| 84 | + return issue |
| 85 | + |
| 86 | +def create_issues(only_one=False): |
| 87 | + po_files = glob("**/*.po") |
| 88 | + existing_issue_counter = 0 |
| 89 | + already_translated_counter = 0 |
| 90 | + created_issues_counter = 0 |
| 91 | + |
| 92 | + print(f"TOTAL PO FILES: {len(po_files)}") |
| 93 | + |
| 94 | + for pofilename in po_files: |
| 95 | + try: |
| 96 | + issue = issue_generator(pofilename) |
| 97 | + created_issues_counter += 1 |
| 98 | + print(f'Issue "{issue.title}" created at {issue.html_url}') |
| 99 | + if only_one: |
| 100 | + break |
| 101 | + except IssueAlreadyExistingError: |
| 102 | + existing_issue_counter += 1 |
| 103 | + except PoFileAlreadyTranslated: |
| 104 | + already_translated_counter += 1 |
| 105 | + |
| 106 | + print("Stats:") |
| 107 | + print(f"- Existing issues: {existing_issue_counter}") |
| 108 | + print(f"- Already translated files: {already_translated_counter}") |
| 109 | + print(f"- Created issues: {created_issues_counter}") |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + error_msg = "Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue" |
| 116 | + if len(sys.argv) != 2: |
| 117 | + raise Exception(error_msg) |
| 118 | + |
| 119 | + arg = sys.argv[1] |
| 120 | + |
| 121 | + if arg == "--all": |
| 122 | + create_issues() |
| 123 | + |
| 124 | + elif arg == "--one": |
| 125 | + create_issues(only_one=True) |
| 126 | + |
| 127 | + else: |
| 128 | + try: |
| 129 | + issue_generator(arg) |
| 130 | + except FileNotFoundError: |
| 131 | + raise Exception(error_msg) |
60 | 132 |
|
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}') |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments