#!/usr/bin/env python
from xml.sax.saxutils import escape, quoteattr
from emit import Emit
from param import known_param_fields, known_units
from lxml import etree
# Emit ArduPilot documentation in an machine readable XML format for Mission Planner
class XmlEmitMP(Emit):
def __init__(self, *args, **kwargs):
Emit.__init__(self, *args, **kwargs)
self.mp_fname = 'ParameterMetaData.xml'
self.f = open(self.mp_fname, mode='w')
self.preamble = '''\n'''
self.f.write(self.preamble)
self.f.write('\n')
self.gname = None
self.skip_name = False
def close(self):
self.f.write(' %s>\n' % self.gname)
self.f.write('''\n''')
self.f.close()
# sort and reformat XML
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(self.mp_fname, parser)
root = tree.getroot()
vehicle = tree.find(self.gname)
sort_xml_node(vehicle)
sorted_unicode = etree.tostring(root, pretty_print=True, encoding='unicode')
f = open(self.mp_fname, mode='w')
f.write(self.preamble)
f.write(sorted_unicode)
f.close()
def emit_comment(self, s):
self.f.write("")
def start_libraries(self):
self.skip_name = True
def emit(self, g):
t = ""
if not self.skip_name:
self.gname = g.reference
if self.gname == "ArduCopter":
self.gname = "ArduCopter2"
if self.gname == "APMrover2" or self.gname == "Rover":
self.gname = "ArduRover"
t = ' <%s>\n' % self.gname
for param in g.params:
# Begin our parameter node
# Get param name and and remove key
name = param.name.split(':')[-1]
t += ' <%s>\n' % name
if hasattr(param, 'DisplayName'):
t += ' %s\n' % param.DisplayName
if hasattr(param, 'Description'):
t += ' %s\n' % escape(param.Description) # i.e. parameter docs
if hasattr(param, 'User'):
t += ' %s\n' % param.User # i.e. Standard or Advanced
# not used yet
# if hasattr(param, 'Calibration'):
# t += ' %s%s%s>\n' % (field, escape(param.__dict__[field]), field)
t += ' %s>\n' % name
# print t
self.f.write(t)
def sort_xml_node(node):
if not isinstance(node.tag, str):
# not a TAG, it is comment or DATA
# no need to sort
return
# sort child along attr
node[:] = sorted(node, key=lambda field: field.tag)
# and recurse
for child in node:
child[:] = sorted(child, key=lambda field: field.tag)