-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenerate_status_table.py
More file actions
100 lines (81 loc) · 2.81 KB
/
Copy pathgenerate_status_table.py
File metadata and controls
100 lines (81 loc) · 2.81 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
#!/usr/bin/env python3
"""
scripts/generate_status_table.py
Regenerate the per-file translation status table between marker comments in
a markdown file (default: STATUS.md) from the current state of all .po
files. Meant to run on a schedule so the table never goes stale, replacing
the old Transifex-exported report.
Requires: pip install polib
Usage:
python3 scripts/generate_status_table.py
python3 scripts/generate_status_table.py --file STATUS.md
"""
import argparse
import re
import sys
from datetime import date
from pathlib import Path
import polib
REPO_ROOT = Path(__file__).resolve().parent.parent
START_MARKER = "<!-- TRANSLATION_STATUS_START -->"
END_MARKER = "<!-- TRANSLATION_STATUS_END -->"
def file_stats(path: Path):
po = polib.pofile(str(path))
return (
len(po.translated_entries()),
len(po.fuzzy_entries()),
len(po.untranslated_entries()),
)
def build_table() -> str:
files = sorted(REPO_ROOT.rglob("*.po"))
rows = []
for f in files:
if ".cpython-src" in f.parts or ".git" in f.parts:
continue
t, fz, u = file_stats(f)
total = t + fz + u
translated_pct = (t / total * 100) if total else 100.0
fuzzy_pct = (fz / total * 100) if total else 0.0
rel = f.relative_to(REPO_ROOT)
rows.append((str(rel), translated_pct, fuzzy_pct))
# most-complete files first, matching the old report's ordering
rows.sort(key=lambda r: (-r[1], r[0]))
lines = [
"| File | Translated | Fuzzy |",
"|:-----|:-----------:|:-----------:|",
]
for name, t_pct, f_pct in rows:
lines.append(f"| {name} | {t_pct:.1f}% | {f_pct:.1f}% |")
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--file",
default="STATUS.md",
help="Markdown file containing the marker block to update",
)
args = parser.parse_args()
target = REPO_ROOT / args.file
text = target.read_text(encoding="utf-8")
if START_MARKER not in text or END_MARKER not in text:
print(f"Markers {START_MARKER} / {END_MARKER} not found in {args.file}.")
print("Add them around the table you want auto-generated, then re-run.")
sys.exit(1)
table = build_table()
block = (
f"{START_MARKER}\n"
f"### وضعیت ترجمه فایلها\n"
f"(بهروزرسانی: {date.today().isoformat()})\n\n"
f"{table}\n"
f"{END_MARKER}"
)
new_text = re.sub(
re.escape(START_MARKER) + r".*?" + re.escape(END_MARKER),
lambda _: block, # avoid backslash-escape surprises from re.sub
text,
flags=re.DOTALL,
)
target.write_text(new_text, encoding="utf-8")
print(f"Updated {args.file}.")
if __name__ == "__main__":
main()