forked from mccreery/better-hud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizelang.py
94 lines (69 loc) · 2.66 KB
/
normalizelang.py
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
#!/usr/bin/env python3
import os
MODID = "betterhud"
ROOT_DIR = os.path.join("src", "main", "resources", "assets", MODID, "lang")
CANONICAL = "en_US.lang"
HEADER = ("file", "score", "changed", "unchanged", "missing", "extra")
STORE_FROM = 1
def get(line):
line = line.strip()
if "=" in line and line[0] != "#":
return line.split("=", 1)
else:
return None
def get_row(silent, *cells, column_width = 10):
if not silent:
print(" ".join(str(cell).ljust(column_width) for cell in cells))
return {HEADER[i]: cells[i] for i in range(STORE_FROM, min(len(cells), len(HEADER)))}
def get_data(silent = False):
data = {}
with open(os.path.join(ROOT_DIR, CANONICAL), encoding="utf-8") as f:
lines = f.readlines()
get_row(silent, *HEADER)
entries = sum(bool(get(l)) for l in lines)
data[CANONICAL] = get_row(silent, CANONICAL, "100%", 0, entries, 0, 0)
scores = []
for f in os.listdir(ROOT_DIR):
if f.lower() == CANONICAL.lower(): continue
mapping = {}
with open(os.path.join(ROOT_DIR, f), "r", encoding="utf-8") as f1:
for line in f1.readlines():
kv = get(line)
if kv: mapping[kv[0]] = kv[1]
with open(os.path.join(ROOT_DIR, f), "w", encoding="utf-8") as f1:
untranslated = 0
translated = 0
identical = 0
for line in lines:
kv = get(line)
if kv:
key = kv[0]
if key in mapping:
if mapping[key] == kv[1]:
identical += 1
else:
translated += 1
f1.write(key + "=" + mapping[key] + "\n")
del mapping[key]
else:
untranslated += 1
f1.write("#" + line)
else:
f1.write(line)
score = (translated + identical) / entries * 100
scores.append(score)
data[f] = get_row(silent, f, str(round(score)) + "%", translated, identical, untranslated, len(mapping))
if mapping:
f1.write("\n# Unmapped lines\n")
for key in mapping:
line = key + "=" + mapping[key]
f1.write(line + "\n")
data["total"] = get_row(silent, "total", str(round(sum(scores) / len(scores))) + "%")
return data
if __name__ == "__main__":
import sys
isjson = "-j" in sys.argv or "--json" in sys.argv
data = get_data(isjson)
if isjson:
import json
print(json.dumps(data))