From 6ca35966431c539294c1c3089d2754ebb372f25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Wed, 20 Nov 2024 23:59:17 -0300 Subject: [PATCH 1/8] update some vars for new github api --- scripts/create_issue.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 9bf6a7dd5d..53eec76ea3 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -33,8 +33,8 @@ sys.exit(1) if pofile.fuzzy == 0 and any([ - pofile.translated_nb == pofile.po_file_size, - pofile.untranslated_nb == 0, + pofile.translated == pofile.entries, + pofile.untranslated == 0, ]): print(f'Skipping {pofilename}. The file is 100% translated already.') sys.exit(1) @@ -51,10 +51,10 @@ Current stats for `{pofilename}`: -- Fuzzy: {pofile.fuzzy_nb} +- Fuzzy: {pofile.fuzzy} - Percent translated: {pofile.percent_translated}% -- Entries: {pofile.translated_nb} / {pofile.po_file_size} -- Untranslated: {pofile.untranslated_nb} +- Entries: {pofile.entries} +- Untranslated: {pofile.untranslated} 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. From 099213c49b21b0e3428778ec72ded0c00628b1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 01:09:01 -0300 Subject: [PATCH 2/8] refactor create issue --- scripts/create_issue.py | 149 +++++++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 39 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 53eec76ea3..613444c965 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -3,61 +3,132 @@ import os import sys +from glob import glob from pathlib import Path from github import Github from potodo.potodo import PoFileStats -if len(sys.argv) != 2: - print('Specify PO filename') - sys.exit(1) - -pofilename = sys.argv[1] -pofile = PoFileStats(Path(pofilename)) g = Github(os.environ.get('GITHUB_TOKEN')) - repo = g.get_repo('python/python-docs-es') +PYTHON_VERSION = "3.13" +ISSUE_LABELS = [PYTHON_VERSION, "good first issue"] +ISSUE_TITLE = 'Translate `{pofilename}`' +ISSUE_BODY = '''This needs to reach 100% translated. -issues = repo.get_issues(state='all') -for issue in issues: - if pofilename in issue.title: +The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated. +Meanwhile, the English version is shown. - print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') - sys.exit(1) +Current stats for `{pofilename}`: - msg = f'There is a similar issue already created at {issue.html_url}.\nDo you want to create it anyways? [y/N] ' - answer = input(msg) - if answer != 'y': - sys.exit(1) +- Fuzzy: {pofile_fuzzy} +- Percent translated: {pofile_percent_translated}% +- Entries: {pofile_entries} +- Untranslated: {pofile_untranslated} -if pofile.fuzzy == 0 and any([ - pofile.translated == pofile.entries, - pofile.untranslated == 0, -]): - print(f'Skipping {pofilename}. The file is 100% translated already.') - sys.exit(1) +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. -# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue -title = f'Translate `{pofilename}`' -urlfile = pofilename.replace('.po', '.html') -issue = repo.create_issue( - title=title, - body=f'''This needs to reach 100% translated. +Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''' -The rendered version of this file will be available at https://docs.python.org/es/3.8/{urlfile} once translated. -Meanwhile, the English version is shown. -Current stats for `{pofilename}`: +class IssueAlreadyExistingError(Exception): + """Issue already existing in GitHub""" -- Fuzzy: {pofile.fuzzy} -- Percent translated: {pofile.percent_translated}% -- Entries: {pofile.entries} -- Untranslated: {pofile.untranslated} -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. +class PoFileAlreadyTranslated(Exception): + """Given PO file is already 100% translated""" + + + +def check_issue_not_already_existing(pofilename): + issues = repo.get_issues(state='open') + for issue in issues: + if pofilename in issue.title: + + print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') + raise IssueAlreadyExistingError() + + +def check_translation_is_pending(pofile): + if pofile.fuzzy == 0 and any([ + pofile.translated == pofile.entries, + pofile.untranslated == 0, + ]): + print(f'Skipping {pofile.filename}. The file is 100% translated already.') + raise PoFileAlreadyTranslated() + + + +def issue_generator(pofilename): + pofile = PoFileStats(Path(pofilename)) + + check_issue_not_already_existing(pofilename) + check_translation_is_pending(pofile) + + urlfile = pofilename.replace('.po', '.html') + title = ISSUE_TITLE.format(pofilename=pofilename) + body = ISSUE_BODY.format( + python_version=PYTHON_VERSION, + urlfile=urlfile, + pofilename=pofilename, + pofile_fuzzy=pofile.fuzzy, + pofile_percent_translated=pofile.percent_translated, + pofile_entries=pofile.entries, + pofile_untranslated=pofile.untranslated, + ) + # https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue + issue = repo.create_issue(title=title, body=body, labels=ISSUE_LABELS) + + return issue + +def create_issues(only_one=False): + po_files = glob("**/*.po") + existing_issue_counter = 0 + already_translated_counter = 0 + created_issues_counter = 0 + + print(f"TOTAL PO FILES: {len(po_files)}") + + for pofilename in po_files: + try: + issue = issue_generator(pofilename) + created_issues_counter += 1 + print(f'Issue "{issue.title}" created at {issue.html_url}') + if only_one: + break + except IssueAlreadyExistingError: + existing_issue_counter += 1 + except PoFileAlreadyTranslated: + already_translated_counter += 1 + + print("Stats:") + print(f"- Existing issues: {existing_issue_counter}") + print(f"- Already translated files: {already_translated_counter}") + print(f"- Created issues: {created_issues_counter}") + + + + +def main(): + error_msg = "Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue" + if len(sys.argv) != 2: + raise Exception(error_msg) + + arg = sys.argv[1] + + if arg == "--all": + create_issues() + + elif arg == "--one": + create_issues(only_one=True) + + else: + try: + issue_generator(arg) + except FileNotFoundError: + raise Exception(error_msg) -Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''', -) -print(f'Issue "{title}" created at {issue.html_url}') +if __name__ == "__main__": + main() From d3baf3356df9c911f13e38ade228a9e5e6cd54b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 01:12:31 -0300 Subject: [PATCH 3/8] add docstring --- scripts/create_issue.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 613444c965..8f7fc34765 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -1,5 +1,9 @@ -# Use together with `pageviews.py` -# 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 '{}' +""" +Run this script with one variable: + - PO filename to create an issue for that file + - or '--all' to create the issues for all untranslated files that doesn't have an open issue already + - or '--one' to create the next one issue +""" import os import sys From 6916e3821c4ace2dbba0d48274e79949b83f2b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 09:54:41 -0300 Subject: [PATCH 4/8] refactor to cache issues --- scripts/create_issue.py | 126 +++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 8f7fc34765..13140d38c4 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -13,10 +13,6 @@ from github import Github from potodo.potodo import PoFileStats - -g = Github(os.environ.get('GITHUB_TOKEN')) -repo = g.get_repo('python/python-docs-es') - PYTHON_VERSION = "3.13" ISSUE_LABELS = [PYTHON_VERSION, "good first issue"] ISSUE_TITLE = 'Translate `{pofilename}`' @@ -45,74 +41,84 @@ class PoFileAlreadyTranslated(Exception): """Given PO file is already 100% translated""" +class GitHubIssueGenerator: + def __init__(self): + g = Github(os.environ.get('GITHUB_TOKEN')) + self.repo = g.get_repo('python/python-docs-es') + self._issues = None -def check_issue_not_already_existing(pofilename): - issues = repo.get_issues(state='open') - for issue in issues: - if pofilename in issue.title: + @property + def issues(self): + if self._issues is None: + self._issues = self.repo.get_issues(state='open') - print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') - raise IssueAlreadyExistingError() + return self._issues + def check_issue_not_already_existing(self, pofilename): + for issue in self.issues: + if pofilename in issue.title: -def check_translation_is_pending(pofile): - if pofile.fuzzy == 0 and any([ - pofile.translated == pofile.entries, - pofile.untranslated == 0, - ]): - print(f'Skipping {pofile.filename}. The file is 100% translated already.') - raise PoFileAlreadyTranslated() + print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') + raise IssueAlreadyExistingError + @staticmethod + def check_translation_is_pending(pofile): + if pofile.fuzzy == 0 and any([ + pofile.translated == pofile.entries, + pofile.untranslated == 0, + ]): + print(f'Skipping {pofile.filename}. The file is 100% translated already.') + raise PoFileAlreadyTranslated -def issue_generator(pofilename): - pofile = PoFileStats(Path(pofilename)) - check_issue_not_already_existing(pofilename) - check_translation_is_pending(pofile) - urlfile = pofilename.replace('.po', '.html') - title = ISSUE_TITLE.format(pofilename=pofilename) - body = ISSUE_BODY.format( - python_version=PYTHON_VERSION, - urlfile=urlfile, - pofilename=pofilename, - pofile_fuzzy=pofile.fuzzy, - pofile_percent_translated=pofile.percent_translated, - pofile_entries=pofile.entries, - pofile_untranslated=pofile.untranslated, - ) - # https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue - issue = repo.create_issue(title=title, body=body, labels=ISSUE_LABELS) + def issue_generator(self, pofilename): + pofile = PoFileStats(Path(pofilename)) - return issue + self.check_issue_not_already_existing(pofilename) + self.check_translation_is_pending(pofile) -def create_issues(only_one=False): - po_files = glob("**/*.po") - existing_issue_counter = 0 - already_translated_counter = 0 - created_issues_counter = 0 + urlfile = pofilename.replace('.po', '.html') + title = ISSUE_TITLE.format(pofilename=pofilename) + body = ISSUE_BODY.format( + python_version=PYTHON_VERSION, + urlfile=urlfile, + pofilename=pofilename, + pofile_fuzzy=pofile.fuzzy, + pofile_percent_translated=pofile.percent_translated, + pofile_entries=pofile.entries, + pofile_untranslated=pofile.untranslated, + ) + # https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue + issue = self.repo.create_issue(title=title, body=body, labels=ISSUE_LABELS) - print(f"TOTAL PO FILES: {len(po_files)}") + return issue - for pofilename in po_files: - try: - issue = issue_generator(pofilename) - created_issues_counter += 1 - print(f'Issue "{issue.title}" created at {issue.html_url}') - if only_one: - break - except IssueAlreadyExistingError: - existing_issue_counter += 1 - except PoFileAlreadyTranslated: - already_translated_counter += 1 + def create_issues(self, only_one=False): + po_files = glob("**/*.po") + existing_issue_counter = 0 + already_translated_counter = 0 + created_issues_counter = 0 - print("Stats:") - print(f"- Existing issues: {existing_issue_counter}") - print(f"- Already translated files: {already_translated_counter}") - print(f"- Created issues: {created_issues_counter}") + print(f"TOTAL PO FILES: {len(po_files)}") + for pofilename in po_files: + try: + issue = self.issue_generator(pofilename) + created_issues_counter += 1 + print(f'Issue "{issue.title}" created at {issue.html_url}') + if only_one: + break + except IssueAlreadyExistingError: + existing_issue_counter += 1 + except PoFileAlreadyTranslated: + already_translated_counter += 1 + print("Stats:") + print(f"- Existing issues: {existing_issue_counter}") + print(f"- Already translated files: {already_translated_counter}") + print(f"- Created issues: {created_issues_counter}") def main(): @@ -122,15 +128,17 @@ def main(): arg = sys.argv[1] + gh = GitHubIssueGenerator() + if arg == "--all": - create_issues() + gh.create_issues() elif arg == "--one": - create_issues(only_one=True) + gh.create_issues(only_one=True) else: try: - issue_generator(arg) + gh.issue_generator(arg) except FileNotFoundError: raise Exception(error_msg) From 19150952bf8ec834653bb153f1ea5cbb4f7567d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 10:07:21 -0300 Subject: [PATCH 5/8] improve issue body --- scripts/create_issue.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 13140d38c4..7b2c156aff 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -14,7 +14,9 @@ from potodo.potodo import PoFileStats PYTHON_VERSION = "3.13" -ISSUE_LABELS = [PYTHON_VERSION, "good first issue"] +PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE = 5 +GOOD_FIRST_ISSUE_LABEL = "good first_issue" +ISSUE_LABELS = [PYTHON_VERSION] ISSUE_TITLE = 'Translate `{pofilename}`' ISSUE_BODY = '''This needs to reach 100% translated. @@ -23,10 +25,10 @@ Current stats for `{pofilename}`: -- Fuzzy: {pofile_fuzzy} -- Percent translated: {pofile_percent_translated}% -- Entries: {pofile_entries} -- Untranslated: {pofile_untranslated} +- Total entries: {pofile_entries} +- Entries that need work: {pending_entries} - ({pofile_percent_translated}%) + - Fuzzy: {pofile_fuzzy} + - Untranslated: {pofile_untranslated} 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. @@ -79,6 +81,13 @@ def issue_generator(self, pofilename): self.check_issue_not_already_existing(pofilename) self.check_translation_is_pending(pofile) + pending_entries = pofile.fuzzy + pofile.untranslated + + if pending_entries <= PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE: + labels = ISSUE_LABELS + [GOOD_FIRST_ISSUE_LABEL] + else: + labels = ISSUE_LABELS + urlfile = pofilename.replace('.po', '.html') title = ISSUE_TITLE.format(pofilename=pofilename) body = ISSUE_BODY.format( @@ -89,9 +98,10 @@ def issue_generator(self, pofilename): pofile_percent_translated=pofile.percent_translated, pofile_entries=pofile.entries, pofile_untranslated=pofile.untranslated, + pending_entries=pending_entries, ) # https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue - issue = self.repo.create_issue(title=title, body=body, labels=ISSUE_LABELS) + issue = self.repo.create_issue(title=title, body=body, labels=labels) return issue From 9461867ba21199c762498f2fe9281b3c06778f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 10:12:53 -0300 Subject: [PATCH 6/8] improve readability in check_translation_is_pending --- scripts/create_issue.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 7b2c156aff..91f54df0a5 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -66,10 +66,11 @@ def check_issue_not_already_existing(self, pofilename): @staticmethod def check_translation_is_pending(pofile): - if pofile.fuzzy == 0 and any([ - pofile.translated == pofile.entries, - pofile.untranslated == 0, - ]): + no_fuzzy_translations = pofile.fuzzy == 0 + translated_match_all_entries = pofile.translated == pofile.entries + no_untranslated_entries_left = pofile.untranslated == 0 + + if no_fuzzy_translations and (translated_match_all_entries or no_untranslated_entries_left): print(f'Skipping {pofile.filename}. The file is 100% translated already.') raise PoFileAlreadyTranslated From 9938321adbfb1717b752195038dde282727031bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 10:22:01 -0300 Subject: [PATCH 7/8] fixes in body issue --- scripts/create_issue.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index 91f54df0a5..d7ab6cb829 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -18,7 +18,7 @@ GOOD_FIRST_ISSUE_LABEL = "good first_issue" ISSUE_LABELS = [PYTHON_VERSION] ISSUE_TITLE = 'Translate `{pofilename}`' -ISSUE_BODY = '''This needs to reach 100% translated. +ISSUE_BODY = '''This file is {translated_percent}% translated and needs to reach 100%. The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated. Meanwhile, the English version is shown. @@ -26,7 +26,8 @@ Current stats for `{pofilename}`: - Total entries: {pofile_entries} -- Entries that need work: {pending_entries} - ({pofile_percent_translated}%) + +- Entries that need work: {pending_entries} - ({pending_percent}%) - Fuzzy: {pofile_fuzzy} - Untranslated: {pofile_untranslated} @@ -92,11 +93,12 @@ def issue_generator(self, pofilename): urlfile = pofilename.replace('.po', '.html') title = ISSUE_TITLE.format(pofilename=pofilename) body = ISSUE_BODY.format( + translated_percent=pofile.percent_translated, python_version=PYTHON_VERSION, urlfile=urlfile, pofilename=pofilename, pofile_fuzzy=pofile.fuzzy, - pofile_percent_translated=pofile.percent_translated, + pending_percent=100 - pofile.percent_translated, pofile_entries=pofile.entries, pofile_untranslated=pofile.untranslated, pending_entries=pending_entries, From 4a79e4ebd87b2752fad642aa74911d7abcef6f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Denner?= Date: Thu, 21 Nov 2024 10:47:54 -0300 Subject: [PATCH 8/8] fix first issue label --- scripts/create_issue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/create_issue.py b/scripts/create_issue.py index d7ab6cb829..e0e3161080 100644 --- a/scripts/create_issue.py +++ b/scripts/create_issue.py @@ -15,7 +15,7 @@ PYTHON_VERSION = "3.13" PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE = 5 -GOOD_FIRST_ISSUE_LABEL = "good first_issue" +GOOD_FIRST_ISSUE_LABEL = "good first issue" ISSUE_LABELS = [PYTHON_VERSION] ISSUE_TITLE = 'Translate `{pofilename}`' ISSUE_BODY = '''This file is {translated_percent}% translated and needs to reach 100%.