-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx_to_ini.py
178 lines (156 loc) · 6.93 KB
/
xlsx_to_ini.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys
import os
import configparser
import openpyxl as xl # pip install openpyxl needed!
import re
import datetime
variableExpr = re.compile(r"\~[a-z]+\([^\)]*\)")
parameterExpr = re.compile(r"%ls|%s|%S|%i|%I|%u|%d|%[0-9.]*f|%\.\*f")
def segment_check(orig, targ): # imported from localization.py
warningcnt = errorcnt = 0
if set(variableExpr.findall(orig)) != set(variableExpr.findall(targ)):
warningcnt = 1
if parameterExpr.findall(orig) != parameterExpr.findall(targ):
errorcnt = 1
return (warningcnt, errorcnt)
def main(args):
if __name__ != "__main__":
docs = args
else:
smartcat_config = configparser.ConfigParser(
delimiters="=", strict=True, interpolation=None
)
smartcat_config.read("smartcat.ini")
docs = [
smartcat_config["sc_ko_m"]["documentId_1"].split(","),
smartcat_config["sc_ko_m"]["documentId_2"].split(","),
]
# if you need manual select for xlsx, comment out above else section and use this line below:
# docs = [['a','global_ini_P1'],['a','global_lines_P1']]
log = open("mpull.log", "w")
globalconfig = configparser.ConfigParser(
delimiters="=", strict=True, interpolation=None
)
globalconfig.read("mconfig.ini") # load settings ini file
excludekeywords = list(globalconfig["parse"]["excludekeywords"].split(","))
## read 3dkeyword.txt for exclude
with open("3dkeyword.txt","r") as f:
keywords_for_3d = f.read().split('\n')
#print(keywords_for_3d)
origindata, transdata, phdata = {}, {}, {}
overwrited, nodata, excluded, variableError, parameterError = 0, 0, 0, 0, 0
warn_notrans = 0 # flag for mpull.log
for doc in docs:
if len(docs) > 1:
print(f"Reading {doc[1]}")
wd = xl.load_workbook(doc[1] + ".xlsx", read_only=True, data_only=True)
ws = wd.active
for rowd in ws.iter_rows(min_row=2): # starts with 2nd row
if len(rowd) > 4:
print(f"Warning: irregular rows detected in {rowd}")
inkey, insource, intarg = rowd[0].value, rowd[1].value, rowd[2].value
if inkey in transdata != None:
log.write(
f"Warning: overwrite duplicated keyword in {rowd}({inkey}) : {transdata[inkey]}\n"
)
overwrited += 1
# if insource.startswith('\''): intarg='\''+intarg #FIXME: print(insource,intarg) # add missed \' at first
transdata[inkey] = intarg
# origindata[inkey]=insource
with open("global_ref.ini", "r", encoding="utf-8-sig") as f:
config_string = "[DEFAULT]\n" + f.read()
origindata = configparser.ConfigParser(
delimiters="=", strict=True, interpolation=None
)
origindata.optionxform = str
origindata.read_string(config_string)
with open(
"PHkeywords.ini", "r", encoding="utf-8-sig"
) as f: # PH segments but used in game.
config_string = "[DEFAULT]\n" + f.read()
phdata = configparser.ConfigParser(delimiters="=", strict=True, interpolation=None)
phdata.optionxform = str
phdata.read_string(config_string)
with open(
"manualkeywords.ini", "r", encoding="utf-8-sig"
) as f: # missed at original ref data
config_string = "[DEFAULT]\n" + f.read()
mndata = configparser.ConfigParser(delimiters="=", strict=True, interpolation=None)
mndata.optionxform = str
mndata.read_string(config_string)
pullfilename = "global_pull_" + datetime.date.today().strftime("%y%m%d")
if os.path.exists(pullfilename + ".ini"):
iternum = 1
while os.path.exists(f"{pullfilename}_{iternum}.ini"):
iternum += 1
pullfilename = pullfilename+"_"+str(iternum)
with open(pullfilename + ".ini", "w", encoding="utf-8-sig") as f:
for keyword in origindata["DEFAULT"]:
if "" in keyword:
keyword = keyword.replace("", "")
if keyword in transdata and transdata[keyword] != None and \
not any(keyword3d in keyword for keyword3d in keywords_for_3d): #remain english for 3d glyphs. temporary solution.
tmp_warning, tmp_error = segment_check(
origindata["DEFAULT"][keyword], transdata[keyword]
)
if tmp_warning:
log.write(
f"Warning : Variable Format is not matched at '{keyword}'\n"
)
if tmp_error:
log.write(
f"Error : Parameter Format is not matched at '{keyword}'\n"
)
variableError += tmp_warning
parameterError += tmp_error
f.write(keyword + "=" + transdata[keyword] + "\n")
else: # doesn't exist in smartcat data
# print(f"Warning : No translated data of '{keyword}', write original data instead.")
if (
keyword in phdata["DEFAULT"]
): # check if data exist in PHKeywords.ini
f.write(keyword + "=" + phdata["DEFAULT"][keyword] + "\n")
continue
else:
f.write(keyword + "=" + origindata["DEFAULT"][keyword] + "\n")
if any(
tmp in origindata["DEFAULT"][keyword] for tmp in excludekeywords
):
excluded += 1
continue
if warn_notrans:
log.write(
f"Warning : No translated data of '{keyword}', write original data instead.\n"
)
nodata += 1
for keyword in mndata["DEFAULT"]:
if keyword in transdata:
print(
f"manual input keyword '{keyword}' is already exist in translated data"
)
else:
f.write(keyword + "=" + mndata["DEFAULT"][keyword] + "\n")
decnt = 0
with open("depreciated_keywords.log", "w") as f:
for keyword in transdata:
if keyword not in origindata["DEFAULT"]:
f.write(
f"keyword '{keyword}' is not used at original .ini file anymore.\n"
)
decnt += 1
if overwrited + nodata > 1:
print(
f"Merge done with {overwrited} overwritten, {nodata} original data uses, {excluded} placeholders"
)
else:
print(f"Merge successfully done with skipping {excluded} placeholders")
if decnt > 0:
print(
"There are depreciated keywords on translated data. please check depreciated_keywords.log file."
)
if variableError > 0:
print(f"There are {variableError} warnings in segments. check mpull.log file.")
if parameterError > 0:
print(f"There are {parameterError} errors in segments. check mpull.log file.")
if __name__ == "__main__":
sys.exit(main(sys.argv))