-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertString.py
More file actions
220 lines (181 loc) · 7.68 KB
/
convertString.py
File metadata and controls
220 lines (181 loc) · 7.68 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import xml.dom.minidom
from pyExcelerator import *
# stuff to handle text encodings
import codecs
enc, dec, read_wrap, write_wrap = codecs.lookup('utf-8')
import struct
import os
RUSSIAN_MAPPING_UTF8_TO_ASCII = [ (1040,'A'),
(1042,'B'),
(1045,'E'),
(1047,'3'),
(1050,'K'),
(1052,'M'),
(1053,'H'),
(1054,'O'),
(1056,'P'),
(1057,'C'),
(1058,'T'),
(1059,'Y'),
(1061,'X') ]
EXPORTED_SHEET_NAMES = []
def IsValidKey(key):
for char in key:
if (char == '_'
or (char >= 'a' and char <= 'z')
or (char >= 'A' and char <= 'Z')
or (char >= '0' and char <= '9')):
continue
else:
return False
return True
def parseXLSStrings( inputfile, group_prefix, sheet):
# try:
langs = []
sections = {}
name,vals = parse_xls(inputfile, group_prefix)[sheet]
#count languages
col = 2
while vals.has_key( (0,col) ) :
langs.append( vals[(0, col)] )
col += 1
numLangs = col - 2
assert len(set(langs)) == len(langs), 'found duplicate lang :' + str(langs)
#count strings
row = 1
while vals.has_key( (row,0) ) :
row += 1
numStrings = row
for line in range(1,numStrings) :
sectionName = vals[(line, 0)]
sectionName = group_prefix + sectionName
if not sections.has_key( sectionName ) :
sections[sectionName] = { 'name' : sectionName, 'ids' : [] }
for lang in langs :
sections[sectionName][lang] = []
section = sections[sectionName]
strId = vals[(line, 1)]
if(strId in section['ids']):
print("ERROR: duplicate textID :" + strId + "\t\t In Sheet: " + sectionName )
if(not IsValidKey(strId)):
print("ERROR: invalid textID :" + strId + "\t\t In Sheet: " + sectionName )
section['ids'].append(strId)
for i in range(numLangs) :
strn = ' '
if vals.has_key( (line, i + 2) ) :
strn = vals[(line, i + 2)]
if isinstance(strn, float):##HACK: For value "5" excel return float.. so we don't want "5.0", is why we convert it in INT then STR
strn = int(strn)
strn = unicode(strn)
# replace non-ascii characters
strn = strn.replace( u'\u2026', u'...')
section[langs[i]].append( strn )
return ( list(langs), sections.values() )
def ExportBinary( stringsData ):
global EXPORTED_SHEET_NAMES
for sheetname, data in stringsData.iteritems():
(langs, strings) = data
filename = sheetname.lower() + "."
EXPORTED_SHEET_NAMES.append( sheetname.lower() )
for section in strings:
for lang in langs:
completeFilename = filename + lang.lower()
f = open(completeFilename,'wb')
numString = len( section[lang] )
#write total number of strings
f.write( struct.pack('I', numString) )
numString = len(section[lang])
for i in range(0,numString):
item = section[lang][i]
item = item.upper()
item = item.replace("\\N", "\n")
item = item.replace("\\N", '\n')
item = item.replace( u'\u2026' ,'...')
item = item.encode('ISO-8859-1')
section[lang][i] = item
#write string data.
for item in section[lang]:
numByte = len(item)
f.write( struct.pack('H', numByte) )
for i in range(0, numByte):
f.write( item[i] )
f.close()
def stringsLoadData( inputfile, group_prefix ):
sheetcounter = 0
xlWorkBook = parse_xls(inputfile, group_prefix)
stringsData = {}
for sheet_name, values in xlWorkBook:
(langs,strings) = parseXLSStrings(inputfile, group_prefix, sheetcounter)
sheetcounter += 1
stringsData[sheet_name] = (langs, strings)
return stringsData
def getBinaryFilename(outFolder,sheet,lang):
#TODO replace 'bad' filecharacters
filename = sheet.lower() + "_" + lang.lower() + ".bin"
return os.path.join( outFolder, filename)
def getIdxMapFilename(outFolder, sheet):
filename = sheet.lower() + ".id"
return os.path.join( outFolder, filename)
def getAllIdxFilenames( stringsData, outFolder=""):
targets = []
for sheetname, data in stringsData.iteritems():
targets.append(getIdxMapFilename(outFolder, sheetname))
return targets
def getAllBinaryFilenames( stringsData, outFolder=""):
targets = []
for sheetname, data in stringsData.iteritems():
(langs, strings) = data
for section in strings:
for lang in langs:
targets.append(getBinaryFilename(outFolder,sheetname,lang))
return targets
def ExportUTF8( stringsData, outFolder=""):
global EXPORTED_SHEET_NAMES
sheetFiles = {}
for sheetname, data in stringsData.iteritems():
(langs, strings) = data
EXPORTED_SHEET_NAMES.append( sheetname.lower() )
if not sheetname in sheetFiles:
sheetFiles[sheetname] = []
numString = 0
for section in strings:
for lang in langs:
completeFilename = getBinaryFilename(outFolder,sheetname,lang)
sheetFiles[sheetname].append(os.path.basename(completeFilename))
f = open(completeFilename,'wb')
numString = len( section[lang] )
#write total number of strings
f.write( struct.pack('H', numString) )
numString = len(section[lang])
for i in range(0,numString):
item = section[lang][i]
item = item.replace(u'\u2026', u'...')
#Replace russian unicode with ascii to have a smaller ext_map arrays
#for (key,value) in RUSSIAN_MAPPING_UTF8_TO_ASCII:
#item = item.replace(unichr(key), unicode(value))
item = item.encode('utf-8')
section[lang][i] = item
#write string data.
for item in section[lang]:
numByte = len(item)
f.write( struct.pack('H', numByte) )
for i in range(0, numByte):
f.write( item[i] )
f.close()
# export id map
idxMapFileName = getIdxMapFilename(outFolder, sheetname)
sheetFiles[sheetname].append(os.path.basename(idxMapFileName))
f = open(idxMapFileName, 'wb')
f.write(struct.pack('H', numString))
print "Totle:" + str(numString) + " text in sheet " + sheetname
for section in strings:
# export id map
for item in section['ids']:
numByte = len(item)
f.write(struct.pack('H', numByte))
for i in range(0, numByte):
f.write(item[i])
f.close()
return sheetFiles
def ExportGameText(infile, outfile):
return ExportUTF8(stringsLoadData(infile, ""), outfile)