-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcheck-colon.py
More file actions
105 lines (88 loc) · 2.74 KB
/
check-colon.py
File metadata and controls
105 lines (88 loc) · 2.74 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
#!/usr/bin/env python
"""Check consistency of colons at the end of entries.
It returns 0 if nothing was done, 1 if files were modified.
"""
import sys
import pathlib
import polib
def colon_is_visible(msg):
if msg.endswith(": ::"):
return True
if msg.endswith(" ::"):
return False
if msg.endswith("::"):
return True
raise ValueError("Don't know if msg ends with a visible or an invisible colon.")
def fix_double_colon(filename, entry, check, verbose):
fixed = entry.msgstr.rstrip(": \u202f\u00A0")
if colon_is_visible(entry.msgid):
fixed += "\u00A0::"
message = r"Expected translation to end with: '\u00A0::'"
else:
fixed += " ::"
message = "Expected translation to end with: ' ::'"
if entry.msgstr != fixed:
if check:
print(f"{filename}:{entry.linenum}: {message}")
if verbose:
print(entry)
else:
entry.msgstr = fixed
return True
return False
def fix_simple_colon(filename, entry, check, verbose):
fixed = entry.msgstr.rstrip(": \u202f\u00A0")
fixed += "\u00A0:"
if entry.msgstr != fixed:
if check:
print(
rf"{filename}:{entry.linenum}: Expected translation to end with: '\u00A0:'"
)
if verbose:
print(entry)
else:
entry.msgstr = fixed
return True
return False
def check(filename, check, verbose):
try:
pofile = polib.pofile(filename)
except OSError:
print(f"{filename} doesn't seem to be a .po file", file=sys.stderr)
return True
has_errors = False
for entry in pofile:
if not entry.msgstr:
continue # No need to check untranslated entries.
if entry.msgid.endswith("::"):
has_errors |= fix_double_colon(filename, entry, check, verbose)
elif entry.msgid.endswith(":"):
has_errors |= fix_simple_colon(filename, entry, check, verbose)
if not check and has_errors:
pofile.save()
return has_errors
def parse_args():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--check",
action="store_true",
default=False,
help="only display suspected entries, do not fix.",
)
parser.add_argument(
"--verbose",
action="store_true",
default=False,
help="display whole entry",
)
parser.add_argument("path", nargs="*")
return parser.parse_args()
def main():
has_errors = False
args = parse_args()
for filename in args.path:
has_errors |= check(filename, args.check, args.verbose)
sys.exit(has_errors)
if __name__ == "__main__":
main()