|
7 | 7 | common review slip: translating or mangling `:class:`int``-style markup, |
8 | 8 | ``code`` spans, %s/{0} placeholders, or |substitution| refs. |
9 | 9 |
|
| 10 | +Requires: pip install polib |
| 11 | +
|
10 | 12 | Usage: |
11 | 13 | python3 scripts/check_markup.py library/functions.po |
12 | 14 | python3 scripts/check_markup.py tutorial/*.po |
|
16 | 18 | import sys |
17 | 19 | from pathlib import Path |
18 | 20 |
|
| 21 | +import polib |
| 22 | + |
19 | 23 | PATTERNS = [ |
20 | 24 | ("sphinx role", re.compile(r":(?:\w+:)?[\w.-]+:`.*?`")), |
21 | 25 | ("literal/code span", re.compile(r"``.*?``")), |
|
25 | 29 | ] |
26 | 30 |
|
27 | 31 |
|
28 | | -def unescape(raw: str) -> str: |
29 | | - """Undo PO string escaping (raw includes the surrounding quotes).""" |
30 | | - inner = raw[1:-1] |
31 | | - out = [] |
32 | | - i = 0 |
33 | | - while i < len(inner): |
34 | | - c = inner[i] |
35 | | - if c == "\\" and i + 1 < len(inner): |
36 | | - nxt = inner[i + 1] |
37 | | - out.append({"n": "\n", "t": "\t", '"': '"', "\\": "\\"}.get(nxt, nxt)) |
38 | | - i += 2 |
39 | | - else: |
40 | | - out.append(c) |
41 | | - i += 1 |
42 | | - return "".join(out) |
43 | | - |
44 | | - |
45 | | -def parse_po(path: Path): |
46 | | - """Yield (location, flags, msgid, msgstr) for each entry in a .po file.""" |
47 | | - lines = path.read_text(encoding="utf-8").splitlines() |
48 | | - i, n = 0, len(lines) |
49 | | - |
50 | | - def read_block(keyword_line): |
51 | | - nonlocal i |
52 | | - parts = [unescape(keyword_line.split(" ", 1)[1].strip())] |
53 | | - i += 1 |
54 | | - while i < n and lines[i].strip().startswith('"'): |
55 | | - parts.append(unescape(lines[i].strip())) |
56 | | - i += 1 |
57 | | - return "".join(parts) |
58 | | - |
59 | | - while i < n: |
60 | | - location, flags = "", [] |
61 | | - while i < n and lines[i].startswith("#"): |
62 | | - if lines[i].startswith("#:"): |
63 | | - location = lines[i][2:].strip() |
64 | | - elif lines[i].startswith("#,"): |
65 | | - flags = [f.strip() for f in lines[i][2:].split(",")] |
66 | | - i += 1 |
67 | | - if i >= n or not lines[i].startswith("msgid"): |
68 | | - i += 1 |
69 | | - continue |
70 | | - |
71 | | - msgid = read_block(lines[i]) |
72 | | - msgid_plural = read_block(lines[i]) if i < n and lines[i].startswith("msgid_plural") else None |
73 | | - |
74 | | - if i < n and lines[i].startswith("msgstr["): |
75 | | - msgstrs = {} |
76 | | - while i < n and lines[i].startswith("msgstr["): |
77 | | - idx = int(lines[i][7:lines[i].index("]")]) |
78 | | - msgstrs[idx] = read_block(lines[i]) |
79 | | - yield location, flags, msgid, msgstrs.get(0, "") |
80 | | - if msgid_plural is not None and 1 in msgstrs: |
81 | | - yield location, flags, msgid_plural, msgstrs[1] |
82 | | - continue |
83 | | - |
84 | | - msgstr = read_block(lines[i]) if i < n and lines[i].startswith("msgstr") else "" |
85 | | - yield location, flags, msgid, msgstr |
86 | | - |
87 | | - |
88 | 32 | def check_file(path: Path) -> int: |
89 | 33 | problems = 0 |
90 | | - for location, flags, msgid, msgstr in parse_po(path): |
91 | | - if not msgid or not msgstr: |
92 | | - continue # header entry or still untranslated |
| 34 | + po = polib.pofile(str(path)) |
| 35 | + for entry in po: |
| 36 | + if entry.obsolete or not entry.msgid or not entry.msgstr: |
| 37 | + continue # obsolete entry, header, or still untranslated |
93 | 38 | for label, pattern in PATTERNS: |
94 | | - expected = pattern.findall(msgid) |
| 39 | + expected = pattern.findall(entry.msgid) |
95 | 40 | if not expected: |
96 | 41 | continue |
97 | | - missing = [tok for tok in expected if tok not in msgstr] |
| 42 | + missing = [tok for tok in expected if tok not in entry.msgstr] |
98 | 43 | if missing: |
99 | 44 | problems += 1 |
100 | | - loc = f" ({location})" if location else "" |
101 | | - tag = " [fuzzy]" if "fuzzy" in flags else "" |
| 45 | + loc = f" ({entry.occurrences[0][0]}:{entry.occurrences[0][1]})" if entry.occurrences else "" |
| 46 | + tag = " [fuzzy]" if entry.fuzzy else "" |
102 | 47 | print(f"{path}{loc}{tag}: missing {label}: {missing}") |
103 | | - print(f" msgid : {msgid[:100]}") |
104 | | - print(f" msgstr: {msgstr[:100]}") |
| 48 | + print(f" msgid : {entry.msgid[:100]}") |
| 49 | + print(f" msgstr: {entry.msgstr[:100]}") |
105 | 50 | return problems |
106 | 51 |
|
107 | 52 |
|
|
0 commit comments