Skip to content

Commit

Permalink
Finally Fixed XML Output
Browse files Browse the repository at this point in the history
  • Loading branch information
dewagter committed Mar 5, 2013
1 parent b7a186d commit 4df350e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
16 changes: 11 additions & 5 deletions sw/tools/airframe_editor/airframe_editor.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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):

Expand Down Expand Up @@ -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)
Expand Down
78 changes: 40 additions & 38 deletions sw/tools/airframe_editor/xml_airframe.py
Expand Up @@ -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("<!DOCTYPE airframe SYSTEM \"airframe.dtd\"><!-- \n\tAirframe comment \n--> <airframe/>")
some_file_like_object = StringIO.StringIO("<!DOCTYPE airframe SYSTEM \"airframe.dtd\">\n\n<!-- \n\tAirframe comment \n-->\n\n<airframe/>")
airframe_xml_tree = ET.parse(some_file_like_object)
airframe = airframe_xml_tree.getroot()


child1 = ET.Comment("+-+-+-+-+-+-+- FIRMWARES -+-+-+-+-+-+-+")
airframe.append(child1)

Expand Down Expand Up @@ -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__':
Expand All @@ -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')
4 changes: 0 additions & 4 deletions sw/tools/airframe_editor/xml_common.py
Expand Up @@ -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)
Expand Down

0 comments on commit 4df350e

Please sign in to comment.