131 lines
5.3 KiB
Python
131 lines
5.3 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: UTF-8 -*-
|
|
__metaclass__ = type
|
|
|
|
import binascii
|
|
import os
|
|
import re
|
|
import sys
|
|
import struct
|
|
import xml.etree.ElementTree as xml_et
|
|
|
|
|
|
DictCtypesFmt = {
|
|
"byte": "B", "int8": "b", "uint8": "b",
|
|
"uint16": "H", "int16": "h",
|
|
"uint32": "I", "int32": "i",
|
|
"uint64": "L", "int64": "l"
|
|
}
|
|
|
|
|
|
######################################################################################################################
|
|
# #
|
|
# Functions Initialization #
|
|
# #
|
|
######################################################################################################################
|
|
def xml_info_parse(f_xml, lname, child_lab_1, child_lab_2, child_lab_3):
|
|
xml_data_info_list = []
|
|
xml_value_list, xml_size_list, xml_type_list = [], [], []
|
|
s_fmt = None
|
|
tree = xml_et.parse(f_xml)
|
|
root = tree.getroot()
|
|
|
|
root_attrib_dict = root.attrib
|
|
xml_info_total_size = int(root_attrib_dict[child_lab_2])
|
|
|
|
for item in root.findall(lname):
|
|
i_value = item.find(child_lab_1).text
|
|
i_size = item.find(child_lab_2).text
|
|
i_type = item.find(child_lab_3).text
|
|
xml_value_list.append(i_value)
|
|
xml_size_list.append(int(i_size))
|
|
xml_type_list.append(i_type)
|
|
|
|
if len(xml_size_list) == len(xml_value_list) == len(xml_type_list):
|
|
if sum(xml_size_list) == xml_info_total_size:
|
|
for i in range(len(xml_type_list)):
|
|
hex_value_str = ""
|
|
xml_type_str = xml_type_list[i].lower()
|
|
m_type = re.search(r"system\.(\w+)", xml_type_str)
|
|
if m_type:
|
|
ctype_str = m_type.group(1)
|
|
if ctype_str in DictCtypesFmt.keys():
|
|
s_fmt = DictCtypesFmt[ctype_str]
|
|
else:
|
|
print ("Ctype String Not Match Any Format in Dict, Please check...")
|
|
return 0
|
|
if xml_type_list[i].find("[]") >= 0: # hex
|
|
hex_value_str += xml_value_list[i]
|
|
else: # decimal
|
|
hex_value_str = struct.pack('<' + s_fmt, int(xml_value_list[i])).encode("hex")
|
|
xml_data_info_list.append(hex_value_str)
|
|
return xml_data_info_list
|
|
else:
|
|
print ("Error: XML Data Size mismatched...\r\n")
|
|
return 0
|
|
else:
|
|
print ("Erro: XML Lable size, value count mismatched...\r\n")
|
|
return 0
|
|
|
|
|
|
def crc32_calculate(xml_crc32_info):
|
|
crc32_byte_dec_value = binascii.crc32(xml_crc32_info.decode("hex"))
|
|
crc32_byte_hex_str = struct.pack('<i', crc32_byte_dec_value).encode("hex") # little endian
|
|
crc32_byte_str = crc32_byte_hex_str[-2:] # crc 32 >> 24
|
|
|
|
return crc32_byte_str
|
|
|
|
|
|
######################################################################################################################
|
|
# #
|
|
# Main Entrance #
|
|
# #
|
|
######################################################################################################################
|
|
if __name__ == '__main__':
|
|
xml_file, file_name_str, xml_file_list = '', '', []
|
|
cmd_parameters = sys.argv
|
|
if 2 <= len(cmd_parameters):
|
|
xml_file = sys.argv[1]
|
|
|
|
if xml_file:
|
|
xml_file_list.append(xml_file)
|
|
else:
|
|
all_dir_list = os.listdir(os.getcwd())
|
|
for each_file in all_dir_list:
|
|
if each_file.find(r".xml") > 0:
|
|
xml_file_list.append(each_file)
|
|
|
|
for each_xml_file in xml_file_list:
|
|
if each_xml_file.find("\\") > 0:
|
|
xml_file_name = os.path.basename(each_xml_file)
|
|
else:
|
|
xml_file_name = each_xml_file
|
|
m_name = re.search(r"(\w+)\.xml", xml_file_name)
|
|
if m_name:
|
|
file_name_str = m_name.group(1)
|
|
else:
|
|
print ("Error: Please input .xml file...Reload...")
|
|
sys.exit()
|
|
|
|
bin_file = file_name_str + r".bin"
|
|
label_name = "item"
|
|
child_label_1, child_label_2, child_label_3 = "value", "size", "type"
|
|
xml_parse_info_list = []
|
|
|
|
xml_parse_info_list = xml_info_parse(each_xml_file, label_name, child_label_1, child_label_2, child_label_3)
|
|
xml_parse_info_str = "".join(xml_parse_info_list)
|
|
xml_crc32_str = xml_parse_info_str[3 * 2:] # ignore first 3 bytes
|
|
|
|
crc32_info = crc32_calculate(xml_crc32_str)
|
|
print ("Crc byte : %s" % crc32_info)
|
|
xml_parse_info_list[1] = crc32_info
|
|
|
|
xml_restruct_info_str = "".join(xml_parse_info_list)
|
|
wf = open(bin_file, 'wb')
|
|
wf.write(binascii.a2b_hex(xml_restruct_info_str))
|
|
wf.close()
|
|
|
|
print ("Pib file %s generated complete, please check...\r\n" % each_xml_file)
|
|
|
|
raw_input("Press <Enter> to Exit...")
|