2015-04-16 14:21:18 -03:00
|
|
|
import xml.etree.ElementTree as ET
|
2014-02-15 20:47:11 -04:00
|
|
|
import codecs
|
2013-10-17 20:47:15 -03:00
|
|
|
|
2015-04-16 14:21:18 -03:00
|
|
|
def indent(elem, level=0):
|
|
|
|
i = "\n" + level*" "
|
|
|
|
if len(elem):
|
|
|
|
if not elem.text or not elem.text.strip():
|
|
|
|
elem.text = i + " "
|
|
|
|
if not elem.tail or not elem.tail.strip():
|
|
|
|
elem.tail = i
|
|
|
|
for elem in elem:
|
|
|
|
indent(elem, level+1)
|
|
|
|
if not elem.tail or not elem.tail.strip():
|
|
|
|
elem.tail = i
|
|
|
|
else:
|
|
|
|
if level and (not elem.tail or not elem.tail.strip()):
|
|
|
|
elem.tail = i
|
|
|
|
|
2014-02-15 16:24:53 -04:00
|
|
|
class XMLOutput():
|
2015-04-16 14:21:18 -03:00
|
|
|
|
2015-11-09 04:31:43 -04:00
|
|
|
def __init__(self, groups, board, inject_xml_file_name):
|
2015-04-16 14:21:18 -03:00
|
|
|
xml_parameters = ET.Element("parameters")
|
|
|
|
xml_version = ET.SubElement(xml_parameters, "version")
|
2015-04-21 16:31:08 -03:00
|
|
|
xml_version.text = "3"
|
2015-11-09 04:31:43 -04:00
|
|
|
importtree = ET.parse(inject_xml_file_name)
|
|
|
|
injectgroups = importtree.getroot().findall("group")
|
|
|
|
for igroup in injectgroups:
|
|
|
|
xml_parameters.append(igroup)
|
2015-04-21 16:31:08 -03:00
|
|
|
last_param_name = ""
|
|
|
|
board_specific_param_set = False
|
2013-10-17 20:47:15 -03:00
|
|
|
for group in groups:
|
2015-04-16 14:21:18 -03:00
|
|
|
xml_group = ET.SubElement(xml_parameters, "group")
|
|
|
|
xml_group.attrib["name"] = group.GetName()
|
2013-10-17 20:47:15 -03:00
|
|
|
for param in group.GetParams():
|
2015-04-21 16:31:08 -03:00
|
|
|
if (last_param_name == param.GetName() and not board_specific_param_set) or last_param_name != param.GetName():
|
|
|
|
xml_param = ET.SubElement(xml_group, "parameter")
|
|
|
|
xml_param.attrib["name"] = param.GetName()
|
|
|
|
xml_param.attrib["default"] = param.GetDefault()
|
|
|
|
xml_param.attrib["type"] = param.GetType()
|
|
|
|
last_param_name = param.GetName()
|
|
|
|
for code in param.GetFieldCodes():
|
|
|
|
value = param.GetFieldValue(code)
|
|
|
|
if code == "board":
|
|
|
|
if value == board:
|
|
|
|
board_specific_param_set = True
|
|
|
|
xml_field = ET.SubElement(xml_param, code)
|
|
|
|
xml_field.text = value
|
|
|
|
else:
|
|
|
|
xml_group.remove(xml_param)
|
|
|
|
else:
|
|
|
|
xml_field = ET.SubElement(xml_param, code)
|
|
|
|
xml_field.text = value
|
|
|
|
if last_param_name != param.GetName():
|
|
|
|
board_specific_param_set = False
|
2015-04-16 14:21:18 -03:00
|
|
|
indent(xml_parameters)
|
|
|
|
self.xml_document = ET.ElementTree(xml_parameters)
|
2014-02-15 16:24:53 -04:00
|
|
|
|
|
|
|
def Save(self, filename):
|
2015-05-03 05:01:27 -03:00
|
|
|
self.xml_document.write(filename, encoding="UTF-8")
|