Repository navigation
Expand file tree
/
Copy pathreview_report.py
More file actions
232 lines (179 loc) · 6.96 KB
/
Copy pathreview_report.py
File metadata and controls
232 lines (179 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
"""
scripts/review_report.py
Builds the markdown body of the "translation review needed" issue from the
changes currently STAGED in git (run it after `git add --all` and
`unstage_cosmetic.py`). Writes an empty file if there is nothing to report.
Sections:
- Summary
- New PO files (counts only; every string in them is untranslated)
- Newly fuzzy strings (old msgid -> new msgid, plus the current msgstr)
- New untranslated strings (new msgids that matched nothing, not fuzzy)
"New" always means "not present in HEAD", so re-running never re-reports
strings from earlier syncs.
Requires: polib (pip install polib)
"""
from __future__ import annotations
import argparse
import subprocess
from pathlib import Path
import polib
# GitHub rejects issue bodies over 65,536 characters.
MAX_BODY = 60_000
FUZZY_PER_FILE = 10
UNTRANSLATED_PER_FILE = 25
MAX_LINE = 160
def git(*args: str) -> str:
return subprocess.run(
["git", *args], capture_output=True, text=True, check=True
).stdout
def staged(diff_filter: str) -> list:
out = git(
"diff",
"--staged",
"--no-renames",
"-z",
"--name-only",
f"--diff-filter={diff_filter}",
"--",
"*.po",
)
return [p for p in out.split("\0") if p]
def key(entry: polib.POEntry) -> tuple:
return (entry.msgctxt, entry.msgid)
def live(po: polib.POFile) -> list:
return [e for e in po if not e.obsolete]
def is_fuzzy(entry: polib.POEntry) -> bool:
return "fuzzy" in entry.flags
def is_untranslated(entry: polib.POEntry) -> bool:
if entry.msgid_plural:
return not any(entry.msgstr_plural.values())
return not entry.msgstr
def clip(text: str, limit: int = MAX_LINE) -> str:
# Keep everything on one line and make sure we can't close a code fence.
text = text.replace("\n", "\\n").replace("```", "'''")
return text if len(text) <= limit else text[: limit - 1] + "…"
# ---------------------------------------------------------------------------
# Collect
# ---------------------------------------------------------------------------
def collect():
new_files = [] # (path, number of strings)
changes = [] # (path, newly_fuzzy, new_untranslated)
for path in staged("A"):
po = polib.pofile(path)
new_files.append((path, len(live(po))))
for path in staged("M"):
old = polib.pofile(git("show", f"HEAD:{path}"))
new = polib.pofile(path)
old_live = live(old)
old_keys = {key(e) for e in old_live}
old_fuzzy = {key(e) for e in old_live if is_fuzzy(e)}
newly_fuzzy = []
new_untranslated = []
for e in live(new):
if is_fuzzy(e):
if key(e) not in old_fuzzy:
newly_fuzzy.append(e)
elif is_untranslated(e) and key(e) not in old_keys:
new_untranslated.append(e)
if newly_fuzzy or new_untranslated:
changes.append((path, newly_fuzzy, new_untranslated))
return new_files, changes
# ---------------------------------------------------------------------------
# Render
# ---------------------------------------------------------------------------
def fuzzy_block(path: str, entries: list) -> str:
lines = [f"### `{path}` ({len(entries)} new fuzzy)", "", "```diff"]
for e in entries[:FUZZY_PER_FILE]:
if e.previous_msgid:
lines.append("- " + clip(e.previous_msgid))
else:
lines.append(" (previous msgid unavailable)")
lines.append("+ " + clip(e.msgid))
if e.msgstr:
lines.append(" msgstr: " + clip(e.msgstr))
lines.append("")
if len(entries) > FUZZY_PER_FILE:
lines.append(f" … and {len(entries) - FUZZY_PER_FILE} more")
lines.append("```")
return "\n".join(lines) + "\n"
def untranslated_block(path: str, entries: list) -> str:
lines = [f"### `{path}` ({len(entries)} new untranslated)", "", "```text"]
lines += [clip(e.msgid) for e in entries[:UNTRANSLATED_PER_FILE]]
if len(entries) > UNTRANSLATED_PER_FILE:
lines.append(f"… and {len(entries) - UNTRANSLATED_PER_FILE} more")
lines.append("```")
return "\n".join(lines) + "\n"
class Body:
"""Accumulates blocks until the size budget runs out."""
def __init__(self, limit: int):
self.parts: list = []
self.size = 0
self.limit = limit
self.omitted = 0
def add(self, text: str) -> None:
if self.size + len(text) > self.limit:
self.omitted += 1
return
self.parts.append(text)
self.size += len(text) + 1
def render(new_files: list, changes: list) -> str:
fuzzy_total = sum(len(f) for _, f, _ in changes)
untr_total = sum(len(u) for _, _, u in changes)
if not (new_files or fuzzy_total or untr_total):
return ""
body = Body(MAX_BODY)
summary = ["## Summary", ""]
if new_files:
strings = sum(n for _, n in new_files)
summary.append(
f"- 🆕 **{len(new_files)}** new PO files ({strings} strings to translate)"
)
if fuzzy_total:
n = sum(1 for _, f, _ in changes if f)
summary.append(f"- 🔍 **{fuzzy_total}** newly fuzzy strings in {n} files")
if untr_total:
n = sum(1 for _, _, u in changes if u)
summary.append(f"- ✏️ **{untr_total}** new untranslated strings in {n} files")
body.add("\n".join(summary) + "\n")
if new_files:
lines = [f"## 🆕 New PO files ({len(new_files)})"]
lines += [f"- `{p}` ({n} strings)" for p, n in new_files]
body.add("\n".join(lines) + "\n")
if fuzzy_total:
body.add(
"## 🔍 Newly fuzzy strings\n\n"
"The source text changed slightly, so the existing translation "
"(`msgstr`) may need updating. `-` is the old source, `+` the new.\n"
)
for path, fuzzy, _ in changes:
if fuzzy:
body.add(fuzzy_block(path, fuzzy))
if untr_total:
body.add(
"## ✏️ New untranslated strings\n\n"
"Brand-new source strings with no close match to an existing "
"translation.\n"
)
for path, _, untr in changes:
if untr:
body.add(untranslated_block(path, untr))
if body.omitted:
body.parts.append(
f"_{body.omitted} section(s) omitted to fit GitHub's issue size "
f"limit — see the sync commit's diff for the full list._\n"
)
return "\n".join(body.parts)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", required=True, type=Path)
args = parser.parse_args()
new_files, changes = collect()
report = render(new_files, changes)
args.output.write_text(report, encoding="utf-8")
print(
f"Report: {len(report)} chars, {len(new_files)} new files, "
f"{len(changes)} changed files with new strings"
)
if __name__ == "__main__":
main()