Tools: add generator for MP parameter xml

This commit is contained in:
Pierre Kancir 2020-03-05 12:45:15 +01:00 committed by Andrew Tridgell
parent a1eb284349
commit 4930142b6e
2 changed files with 97 additions and 1 deletions

View File

@ -14,6 +14,7 @@ from wikiemit import WikiEmit
from xmlemit import XmlEmit from xmlemit import XmlEmit
from mdemit import MDEmit from mdemit import MDEmit
from jsonemit import JSONEmit from jsonemit import JSONEmit
from xmlemit_mp import XmlEmitMP
parser = ArgumentParser(description="Parse ArduPilot parameters.") parser = ArgumentParser(description="Parse ArduPilot parameters.")
parser.add_argument("-v", "--verbose", dest='verbose', action='store_true', default=False, help="show debugging output") parser.add_argument("-v", "--verbose", dest='verbose', action='store_true', default=False, help="show debugging output")
@ -27,7 +28,7 @@ parser.add_argument("--format",
dest='output_format', dest='output_format',
action='store', action='store',
default='all', default='all',
choices=['all', 'html', 'rst', 'wiki', 'xml', 'json', 'edn', 'md'], choices=['all', 'html', 'rst', 'wiki', 'xml', 'json', 'edn', 'md', 'xml_mp'],
help="what output format to use") help="what output format to use")
args = parser.parse_args() args = parser.parse_args()
@ -397,6 +398,8 @@ if args.emit_params:
do_emit(RSTEmit()) do_emit(RSTEmit())
if args.output_format == 'all' or args.output_format == 'md': if args.output_format == 'all' or args.output_format == 'md':
do_emit(MDEmit()) do_emit(MDEmit())
if args.output_format == 'all' or args.output_format == 'xml_mp':
do_emit(XmlEmitMP())
if args.output_format == 'all' or args.output_format == 'edn': if args.output_format == 'all' or args.output_format == 'edn':
try: try:
from ednemit import EDNEmit from ednemit import EDNEmit

View File

@ -0,0 +1,93 @@
#!/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):
Emit.__init__(self)
self.mp_fname = 'ParameterMetaData.xml'
self.f = open(self.mp_fname, mode='w')
self.preamble = '''<?xml version="1.0" encoding="utf-8"?>\n'''
self.f.write(self.preamble)
self.f.write('<Params>\n')
self.gname = None
self.skip_name = False
def close(self):
self.f.write(' </%s>\n' % self.gname)
self.f.write('''</Params>\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("<!-- " + s + " -->")
def start_libraries(self):
self.skip_name = True
def emit(self, g):
t = ""
if not self.skip_name:
self.gname = g.name
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 += ' <DisplayName>%s</DisplayName>\n' % param.DisplayName
if hasattr(param, 'Description'):
t += ' <Description>%s</Description>\n' % escape(param.Description) # i.e. parameter docs
if hasattr(param, 'User'):
t += ' <User>%s</User>\n' % param.User # i.e. Standard or Advanced
# not used yet
# if hasattr(param, 'Calibration'):
# t += ' <Calibration>%s</Calibration\n' % quoteattr(param.Calibration)
# Add values as chidren of this node
for field in param.__dict__.keys():
if field not in ['name', 'DisplayName', 'Description', 'User'] and field in known_param_fields:
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)