parse_value.py
7.91 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#######################################################
# parse files in value folder
# author: tqq
#######################################################
from public_def import *
import os
from xml.etree import ElementTree
g_debug = True
class ParseValueXml:
def __init__(self):
self.m_str_network_list = []
self.m_output_path = g_out_put_folder_name
def parseAllValue(self):
path = getPrePath("", 2) + "\\VKResourece\\values"
file_list = os.listdir(path)
for fl in file_list:
#genreate map key
temp_path = os.path.join(path, fl)
if os.path.isfile(temp_path) == False:
continue
temp = fl.replace(".xml","")
g_value_map[temp] = {}
file_path = path + "\\" + fl
fcontent = open(file_path, "r").read()
if fl.find(".xml") != -1:
if fl.find("address")!= -1:
self.parse_address(fcontent,"address")
elif fl.find("colors")!=-1:
self.parse_colors(fcontent,"colors")
elif fl.find("bihua_for_hkb")!=-1:
self.parse_bihua_for_hkb(fcontent,"bihua_for_hkb")
elif fl.find("dimen")!=-1:
self.parse_dim(fcontent,"dimen")
elif fl.find("string")!=-1:
self.parse_string(fcontent,"string")
return True
#address xml
def parse_address(self,file_content,fname):
file_name = fname
root = ElementTree.fromstring(file_content)
num = len(root._children)
cc_list = root._children
for i in xrange(0, num):
self.m_str_network_list.append([])
self.m_str_network_list[i].append(cc_list[i].attrib["name"])
for j in xrange(0,len(cc_list[i]._children)):
self.m_str_network_list[i].append(cc_list[i][j].text)
#print it all and wirte it to file.
#print self.m_str_network_list
g_value_map[file_name] = self.m_str_network_list
#file_name += ".h"
#file_path = self.m_output_path + file_name
#if g_debug == True:
# pass
#print "OutPut_path%s",file_path
#obj_file = open(file_path, "w")
#out put string arrays
output_string = ""
for cild in xrange(0, len(self.m_str_network_list)):
gc = self.m_str_network_list[cild]
if gc == []:
continue
array_name = gc[0]
output_string += "char* "
output_string += array_name
output_string += "[]\n{\n"
for stag in xrange(1, len(gc)):
output_string += "\""
output_string += gc[stag]
output_string += "\",\n"
output_string += "}\n\n"
# if g_debug == True:
# pass
#print output_string
#obj_file.write(output_string)
def parse_bihua_for_hkb(self,file_content,fname):
file_name = fname
root = ElementTree.fromstring(file_content)
child_list = root._children
map_bihua = {}
for i in xrange(0, len(child_list)):
if(child_list[i].attrib.has_key("name")):
value = child_list[i].attrib["name"]
map_bihua[value] = []
cc_list = child_list[i]._children
temp_list = []
if cc_list != []:
for k in xrange(0, len(cc_list)):
temp_list.append(cc_list[k].text)
map_bihua[value] = temp_list
#print map_bihua
g_value_map[file_name] = map_bihua
def parse_colors(self,file_content,fname):
file_name = fname
root = ElementTree.fromstring(file_content)
child_list = root._children
color_map = {}
#if g_debug:
#write_buffer = ""
for i in xrange(0,len(child_list)):
if child_list[i].attrib.has_key("name"):
value = child_list[i].attrib["name"]
color = child_list[i].text
color = color.replace("#","")
temp_color = "0x"
if len(color) < 8:
for i in xrange(0, 8 - len(color)):
temp_color += 'f'
#print temp_color
temp_color += color
else:
temp_color += color
#if g_debug:
# write_buffer += "\n#define\t\t\t"
# write_buffer += value
# write_buffer += "\t\t\t"
#write_buffer += temp_color
color_map[value] = temp_color
#type swap
#print color_map
#if g_debug:
# file_path = ""
# file_path = file_name + ".h"
# file = open(file_path, "w")
#file.write(write_buffer)
#file.flush()
#file.close()
g_value_map[file_name] = color_map
#string include single string and string array.
def parse_string(self,file_content,fname):
file_name = fname
root = ElementTree.fromstring(file_content)
child_list = root._children
string_map = {}
write_buffer = ""
for child in child_list:
if child.tag == "string" and child.attrib.has_key("name"):
temp_string = ""
string_name = child.attrib["name"]
string = child.text
temp_string += "\""
temp_string += string
temp_string += "\""
string_map[string_name] = temp_string
write_buffer += "\n"
write_buffer += "#define \t\t"
write_buffer += string_name
write_buffer += "\t\t\t"
write_buffer += temp_string
g_value_map[file_name] = string_map
#write it to file
file_name += ".h"
file_path = self.m_output_path + "\\" + file_name
file = open(file_path,"w")
file.write(write_buffer)
file.flush()
file.close()
return True
def parse_dim(self,file_content,fname):
file_name = fname
root = ElementTree.fromstring(file_content)
child_list = root._children
dim_map = {}
# if g_debug:
# write_buffer = ""
for i in xrange(0, len(child_list)):
tag = child_list[i].tag
attr = child_list[i].attrib
#print tag
key = ""
value = ""
if attr.has_key("name"):
key = attr["name"]
value = child_list[i].text
else:
continue
#sp parse
if "sp" in value:
tsp = GetSp()
if tsp != 0:
value+= str(tsp)
value = value.replace("sp", "*")
value += "/160"
elif "dip" in value:
value = value.replace("dip", "")
value+="/160"
if tag not in dim_map:
dim_map[tag] = {}
dim_map[tag][key] = value
# if g_debug:
# write_buffer += "\ntag:\t"
# write_buffer += tag
# if tag == "integer":
# write_buffer += "\t"*4
# else:
# write_buffer += "\t"*5
#write_buffer += key
#write_buffer += "\t"*10
#write_buffer += value
#print dim_map
#write to file to test
# if g_debug:
# file_name += ".h"
# file = open(file_name, "w")
# file.write(write_buffer)
# file.flush()
# file.close()
g_value_map[fname] = dim_map
return True