-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathformat_check.py
More file actions
88 lines (68 loc) · 2.16 KB
/
format_check.py
File metadata and controls
88 lines (68 loc) · 2.16 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
import argparse
import collections
import contextlib
import glob
import os
from pprint import pprint
import polib
parser = argparse.ArgumentParser()
parser.add_argument(
"path",
nargs="?",
default=None,
help="Path to the .po file or directory containing .po files",
)
parser.add_argument("-t", "--threshold", type=int, default=85)
args = parser.parse_args()
subject = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
)
)
is_file = False
with contextlib.suppress(TypeError):
if os.path.isdir(args.subject):
is_file = False
subject = os.path.abspath(args.subject)
elif os.path.isfile(args.subject):
is_file = True
subject = (
args.subject
if os.path.isabs(args.subject)
else os.path.join(subject, args.subject)
)
else:
print("Invalid subject, showing all files.")
DELIMITERS = ("``", "*")
def has_delimiters(x):
return any(d in x for d in DELIMITERS)
def main(subject):
subject = [subject] if is_file else glob.glob(f"{subject}**/**/*.po")
files_with_differences = collections.defaultdict(list)
for _, pofilename in enumerate(subject):
po = polib.pofile(pofilename)
if po.percent_translated() < min(args.threshold, 100):
continue
for entry in po:
wordsid = wordsstr = []
if has_delimiters(entry.msgid):
wordsid = [word for word in entry.msgid.split() if has_delimiters(word)]
if has_delimiters(entry.msgstr):
wordsstr = [
word for word in entry.msgstr.split() if has_delimiters(word)
]
if len(wordsid) != len(wordsstr):
key = pofilename
files_with_differences[key].append(
{
"occurrences": entry.occurrences,
"words": {
"original": wordsid,
"translated": wordsstr,
},
}
)
return files_with_differences
if __name__ == "__main__":
pprint(main(subject))