Skip to content

Commit 6916e38

Browse files
committed
refactor to cache issues
1 parent d3baf33 commit 6916e38

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

scripts/create_issue.py

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
from github import Github
1414
from potodo.potodo import PoFileStats
1515

16-
17-
g = Github(os.environ.get('GITHUB_TOKEN'))
18-
repo = g.get_repo('python/python-docs-es')
19-
2016
PYTHON_VERSION = "3.13"
2117
ISSUE_LABELS = [PYTHON_VERSION, "good first issue"]
2218
ISSUE_TITLE = 'Translate `{pofilename}`'
@@ -45,74 +41,84 @@ class PoFileAlreadyTranslated(Exception):
4541
"""Given PO file is already 100% translated"""
4642

4743

44+
class GitHubIssueGenerator:
45+
def __init__(self):
46+
g = Github(os.environ.get('GITHUB_TOKEN'))
47+
self.repo = g.get_repo('python/python-docs-es')
48+
self._issues = None
4849

49-
def check_issue_not_already_existing(pofilename):
50-
issues = repo.get_issues(state='open')
51-
for issue in issues:
52-
if pofilename in issue.title:
50+
@property
51+
def issues(self):
52+
if self._issues is None:
53+
self._issues = self.repo.get_issues(state='open')
5354

54-
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
55-
raise IssueAlreadyExistingError()
55+
return self._issues
5656

57+
def check_issue_not_already_existing(self, pofilename):
58+
for issue in self.issues:
59+
if pofilename in issue.title:
5760

58-
def check_translation_is_pending(pofile):
59-
if pofile.fuzzy == 0 and any([
60-
pofile.translated == pofile.entries,
61-
pofile.untranslated == 0,
62-
]):
63-
print(f'Skipping {pofile.filename}. The file is 100% translated already.')
64-
raise PoFileAlreadyTranslated()
61+
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
62+
raise IssueAlreadyExistingError
6563

6664

65+
@staticmethod
66+
def check_translation_is_pending(pofile):
67+
if pofile.fuzzy == 0 and any([
68+
pofile.translated == pofile.entries,
69+
pofile.untranslated == 0,
70+
]):
71+
print(f'Skipping {pofile.filename}. The file is 100% translated already.')
72+
raise PoFileAlreadyTranslated
6773

68-
def issue_generator(pofilename):
69-
pofile = PoFileStats(Path(pofilename))
7074

71-
check_issue_not_already_existing(pofilename)
72-
check_translation_is_pending(pofile)
7375

74-
urlfile = pofilename.replace('.po', '.html')
75-
title = ISSUE_TITLE.format(pofilename=pofilename)
76-
body = ISSUE_BODY.format(
77-
python_version=PYTHON_VERSION,
78-
urlfile=urlfile,
79-
pofilename=pofilename,
80-
pofile_fuzzy=pofile.fuzzy,
81-
pofile_percent_translated=pofile.percent_translated,
82-
pofile_entries=pofile.entries,
83-
pofile_untranslated=pofile.untranslated,
84-
)
85-
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue
86-
issue = repo.create_issue(title=title, body=body, labels=ISSUE_LABELS)
76+
def issue_generator(self, pofilename):
77+
pofile = PoFileStats(Path(pofilename))
8778

88-
return issue
79+
self.check_issue_not_already_existing(pofilename)
80+
self.check_translation_is_pending(pofile)
8981

90-
def create_issues(only_one=False):
91-
po_files = glob("**/*.po")
92-
existing_issue_counter = 0
93-
already_translated_counter = 0
94-
created_issues_counter = 0
82+
urlfile = pofilename.replace('.po', '.html')
83+
title = ISSUE_TITLE.format(pofilename=pofilename)
84+
body = ISSUE_BODY.format(
85+
python_version=PYTHON_VERSION,
86+
urlfile=urlfile,
87+
pofilename=pofilename,
88+
pofile_fuzzy=pofile.fuzzy,
89+
pofile_percent_translated=pofile.percent_translated,
90+
pofile_entries=pofile.entries,
91+
pofile_untranslated=pofile.untranslated,
92+
)
93+
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue
94+
issue = self.repo.create_issue(title=title, body=body, labels=ISSUE_LABELS)
9595

96-
print(f"TOTAL PO FILES: {len(po_files)}")
96+
return issue
9797

98-
for pofilename in po_files:
99-
try:
100-
issue = issue_generator(pofilename)
101-
created_issues_counter += 1
102-
print(f'Issue "{issue.title}" created at {issue.html_url}')
103-
if only_one:
104-
break
105-
except IssueAlreadyExistingError:
106-
existing_issue_counter += 1
107-
except PoFileAlreadyTranslated:
108-
already_translated_counter += 1
98+
def create_issues(self, only_one=False):
99+
po_files = glob("**/*.po")
100+
existing_issue_counter = 0
101+
already_translated_counter = 0
102+
created_issues_counter = 0
109103

110-
print("Stats:")
111-
print(f"- Existing issues: {existing_issue_counter}")
112-
print(f"- Already translated files: {already_translated_counter}")
113-
print(f"- Created issues: {created_issues_counter}")
104+
print(f"TOTAL PO FILES: {len(po_files)}")
114105

106+
for pofilename in po_files:
107+
try:
108+
issue = self.issue_generator(pofilename)
109+
created_issues_counter += 1
110+
print(f'Issue "{issue.title}" created at {issue.html_url}')
111+
if only_one:
112+
break
113+
except IssueAlreadyExistingError:
114+
existing_issue_counter += 1
115+
except PoFileAlreadyTranslated:
116+
already_translated_counter += 1
115117

118+
print("Stats:")
119+
print(f"- Existing issues: {existing_issue_counter}")
120+
print(f"- Already translated files: {already_translated_counter}")
121+
print(f"- Created issues: {created_issues_counter}")
116122

117123

118124
def main():
@@ -122,15 +128,17 @@ def main():
122128

123129
arg = sys.argv[1]
124130

131+
gh = GitHubIssueGenerator()
132+
125133
if arg == "--all":
126-
create_issues()
134+
gh.create_issues()
127135

128136
elif arg == "--one":
129-
create_issues(only_one=True)
137+
gh.create_issues(only_one=True)
130138

131139
else:
132140
try:
133-
issue_generator(arg)
141+
gh.issue_generator(arg)
134142
except FileNotFoundError:
135143
raise Exception(error_msg)
136144

0 commit comments

Comments
 (0)