forked from an-anime-team/the-honkers-railway-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluentscan.py
executable file
·152 lines (103 loc) · 3.82 KB
/
fluentscan.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
# Author: @xstraok
# Modified by Observer KRypt0n_ <https://github.com/krypt0nn>
import os
import sys
import glob
import re
valid_commands = ["diff", "unused", "missing"]
if len(sys.argv) < 3:
print(f"Command format: ./fluentscan.py [command] [locale]\nAvailable commands: {valid_commands}")
sys.exit()
if sys.argv[1] not in valid_commands:
print(f"Invalid command \"{sys.argv[1]}\". Available commands: {valid_commands}")
sys.exit()
path = "assets/locales/" + sys.argv[2] + "/"
try:
open(path + "/main.ftl", "r").close()
except:
print(f"{path} does not exist")
sys.exit()
all_entries = {}
def dict_compare(d1, d2):
d1_keys = set(d1.keys())
d2_keys = set(d2.keys())
shared_keys = d1_keys.intersection(d2_keys)
added = d1_keys - d2_keys
removed = d2_keys - d1_keys
same = set(o for o in shared_keys if d1[o] == d2[o])
return added, removed, same
def to_dict(text):
result={}
for i in text:
if " =" in i:
try:
result[i.split()[0]] = ' '.join(i.split()[2:])
except:
pass
elif i:
result[list(result.keys())[-1]] += i
return result
def get_line_num(text,pattern):
line = 1
for i in text.split("\n"):
if pattern in i:
return line
line += 1
for filename in os.listdir("assets/locales/en"):
with open(os.path.join("assets/locales/en", filename), 'r') as locale_file:
created_locale = open(path + filename)
expected = to_dict(locale_file)
expected2 = to_dict(created_locale)
all_entries.update(expected)
added, removed, same = dict_compare(expected, expected2)
if sys.argv[1] == "unused" or sys.argv[1] == "missing":
files = glob.glob("src/**/*.rs", recursive=True)
used = []
vars = {}
for i in files:
with open(i, "r") as script:
text = script.read()
if sys.argv[1] == "unused":
for j in expected:
if f"\"{j}\"" in text:
used.append(j)
elif sys.argv[1] == "missing":
for j in text.split():
# TODO: ignore comments
if 'tr("' in j:
index = j.find('tr("')
var_name = re.sub('[^\\w-]+', '', j[index:].replace('tr("', '').replace("Some", ""))
# TODO: index multiple matches
vars[var_name] = [script.name, get_line_num(text,var_name)]
if sys.argv[1] == "unused":
for i in expected:
if i not in used:
print(f"[{locale_file.name}]\n"
" [Unused]\n"
f" {i}")
continue
if (added or removed or same) and sys.argv[1] == "diff":
print(f"[{created_locale.name[15:]}]")
if added:
print(" [Added]")
for i in added:
print(f" {i} = {expected[i]}")
if removed:
print(" [Removed]")
for i in removed:
print(f" {i} = {expected2[i]}")
#workaround
if same and same != "set()":
print(" [Untranslated]")
for i in same:
print(f" {i} = {expected[i]}")
print("")
if sys.argv[1] == "missing":
added, removed, same = dict_compare(vars, all_entries)
if not added:
print("Nothing is missing")
for i in added:
print(f"[{vars[i][0]}, line {vars[i][1]}]\n"
" [Missing]\n"
f" {i}")