-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_w.py
51 lines (40 loc) · 1.44 KB
/
make_w.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
import json
import re
import sys
import os
if sys.version_info.major < (3):
raise Exception("must use python 3")
else:
from json.decoder import JSONDecodeError
def to_w(s):
if isinstance(s, str):
return re.sub(r"[a-z,A-Z,0-9]", 'W', s)
elif isinstance(s, list):
return [to_w(x) for x in s]
elif isinstance(s, dict):
for k, v in s.items():
s[k] = to_w(v)
return s
else:
raise Exception("Something else? '{}'".format(type(s)))
if __name__ == "__main__":
localization_dir = os.path.abspath(os.path.dirname(__file__))
eng_lang_dir = os.path.join(localization_dir, 'eng')
for filepath in os.listdir(eng_lang_dir):
if filepath.endswith('.json'):
eng_file = os.path.join(eng_lang_dir, filepath)
with open(eng_file) as f:
try:
data = json.load(f)
except JSONDecodeError:
print("Error with reading file: {}".format(eng_file))
raise
data = to_w(data)
json_data = json.dumps(data, indent=4)
www_lang_dir = os.path.join(os.path.dirname(eng_lang_dir), 'www')
if not os.path.isdir(www_lang_dir):
os.mkdir(www_lang_dir)
filename = os.path.basename(filepath)
with open(os.path.join(www_lang_dir, filename), 'w') as f:
f.write(json_data)
print(data)