From 4df350ef6f724916b88937d82829d4544c85a113 Mon Sep 17 00:00:00 2001 From: Christophe De Wagter Date: Wed, 13 Feb 2013 17:53:35 +0100 Subject: [PATCH] Finally Fixed XML Output --- sw/tools/airframe_editor/airframe_editor.py | 16 +++-- sw/tools/airframe_editor/xml_airframe.py | 78 +++++++++++---------- sw/tools/airframe_editor/xml_common.py | 4 -- 3 files changed, 51 insertions(+), 47 deletions(-) diff --git a/sw/tools/airframe_editor/airframe_editor.py b/sw/tools/airframe_editor/airframe_editor.py index 516e3a612fe..5545e18dd5e 100755 --- a/sw/tools/airframe_editor/airframe_editor.py +++ b/sw/tools/airframe_editor/airframe_editor.py @@ -24,10 +24,11 @@ class AirframeEditor: def load_airframe_xml(self): global airframe_file self.tvcolumn.set_title(airframe_file.replace(paparazzi.airframes_dir,"")) - [e, self.xml] = xml_airframe.load(airframe_file, self.treestore) + [e, self.xml] = xml_airframe.load(airframe_file) if (e): gui_dialogs.error_loading_xml(e.__str__()) raise e + xml_airframe.fill_tree(self.xml, self.treestore) def update_combo(self,combo,list): combo.set_sensitive(False) @@ -63,8 +64,9 @@ def find_module_defines(self, widget): for d in mod.defines: self.gridstore.append( [ "define", d[0], d[1], d[2], d[3] ] ) - def process(self, widget): - xml_airframe.reorganize_airframe_xml(self.xml) + def reorganize_xml(self, widget): + self.xml = xml_airframe.reorganize_airframe_xml(self.xml) + xml_airframe.fill_tree(self.xml, self.treestore) def textchanged(self, widget): self.text_box.set_text(self.textbox.get_text()) @@ -84,7 +86,11 @@ def open(self, widget): # Constructor Functions def select(self, widget): - print("Selected ",self.treeview.get_selection()) + #get data from highlighted selection + treeselection = self.treeview.get_selection() + (model, iter) = treeselection.get_selected() + name_of_data = self.treestore.get_value(iter, 0) + print("Selected ",name_of_data) def fill_tree_from_airframe(self): @@ -168,7 +174,7 @@ def __init__(self): self.btnOpen.connect("clicked", self.open) self.btnRun = gtk.Button("Reorganize XML") - self.btnRun.connect("clicked", self.process) + self.btnRun.connect("clicked", self.reorganize_xml) self.btnFirmwares = gtk.Button("Firmwares") self.btnFirmwares.connect("clicked", self.find_firmwares) diff --git a/sw/tools/airframe_editor/xml_airframe.py b/sw/tools/airframe_editor/xml_airframe.py index 3d3e0294d5f..a0076c48808 100755 --- a/sw/tools/airframe_editor/xml_airframe.py +++ b/sw/tools/airframe_editor/xml_airframe.py @@ -3,24 +3,30 @@ from __future__ import print_function import lxml.etree as ET +import StringIO + import xml_common import paparazzi def find_and_add(source, target, search): - temp = source.getroot().findall(search) - if temp == None: - return + temp = source.getroot().findall("./"+search) + for t in temp: + xml_common.indent(t,1) target.extend(temp) def find_and_add_sections_with_name(source, target, find_name): - for block in source.getroot(): - name = block.get("name") - if name == find_name: - target.append(block) + temp = source.getroot().findall("./*[@name='" +find_name+ "']") + for t in temp: + xml_common.indent(t,1) + target.extend(temp) def reorganize_airframe_xml(airframe_xml): - airframe = ET.fromstring(" ") + some_file_like_object = StringIO.StringIO("\n\n\n\n") + airframe_xml_tree = ET.parse(some_file_like_object) + airframe = airframe_xml_tree.getroot() + + child1 = ET.Comment("+-+-+-+-+-+-+- FIRMWARES -+-+-+-+-+-+-+") airframe.append(child1) @@ -59,51 +65,46 @@ def reorganize_airframe_xml(airframe_xml): find_and_add_sections_with_name(airframe_xml,airframe,"GUIDANCE_V") find_and_add_sections_with_name(airframe_xml,airframe,"GUIDANCE_H") - - airframe.append(ET.Comment("+-+-+-+-+-+-+- MISC -+-+-+-+-+-+-+")) - for block in airframe_xml.getroot(): - name = block.get("name") - if name == None: - name = "None" - if (block.tag == "firmware"): - print("firmware",block.tag, name) - elif (block.tag in ["servos", "commands", "rc_commands", "command_laws"]): - print("actuator",block.tag, name) - else: - print("other",block.tag, name) - airframe.append(block) - + find_and_add(airframe_xml,airframe,"*") xml_common.indent(airframe) + + temp = airframe.findall("./*") + for t in temp: + t.tail = "\n\n " + #print(etree.tostring(airframe)) - ET.ElementTree(airframe).write('test.xml',pretty_print=True) + #ET.ElementTree(airframe_xml_tree).write('test.xml') + return airframe_xml_tree -def load(airframe_file, tree): +def load(airframe_file): try: my_xml = ET.parse(airframe_file) - root = my_xml.getroot() + return [None, my_xml] + except (IOError, ET.XMLSyntaxError, ET.XMLSyntaxError) as e: + return [e, my_xml] + + +def fill_tree(my_xml, tree): + root = my_xml.getroot() - tree.clear() - for block in root: + tree.clear() + for block in root: + if not isinstance(block, ET._Comment): name = block.get("name") if name == None: name = "" - - print(block.tag.__str__() + " " + name) + + #print(block.tag.__str__() + " " + name) piter = tree.append(None, [ block.tag.__str__() + " " + name ]) for elem in block: ename = elem.get("name") if ename == None: ename = "" - tree.append(piter, [ elem.tag.__str__() + " " + ename ]) - return [None, my_xml] - except (IOError, ET.XMLSyntaxError, ET.XMLSyntaxError) as e: - return [e, my_xml] - - - + if not isinstance(elem, ET._Comment): + tree.append(piter, [ elem.tag.__str__() + " " + ename ]) if __name__ == '__main__': @@ -112,5 +113,6 @@ def load(airframe_file, tree): airframe_file = sys.argv[1] airframe = ET.parse(airframe_file) else: - airframe = ET.parse("../../../conf/airframes/CDW/classix.xml") - reorganize_airframe_xml(airframe) + [e, airframe] = load("../../../conf/airframes/CDW/classix.xml") + xml = reorganize_airframe_xml(airframe) + ET.ElementTree(xml.getroot()).write('test.xml') diff --git a/sw/tools/airframe_editor/xml_common.py b/sw/tools/airframe_editor/xml_common.py index c67375e21b8..30f5d3e7df3 100644 --- a/sw/tools/airframe_editor/xml_common.py +++ b/sw/tools/airframe_editor/xml_common.py @@ -35,11 +35,7 @@ def indent(elem, level=0, more_sibs=False): i = "\n" - if (not more_sibs) & (level == 2): - i+= "\n" num_kids = len(elem) - if ((level <= 1)): - i += "\n" if level: i += (level-1) * ' ' #print(level, elem.tag, num_kids, more_sibs)