2013-02-16 18:01:55 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2020-10-07 17:14:20 -03:00
|
|
|
from lxml import etree
|
2013-02-16 18:01:55 -04:00
|
|
|
|
|
|
|
from emit import Emit
|
2017-05-02 07:36:14 -03:00
|
|
|
from param import known_param_fields, known_units
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2021-04-07 02:07:29 -03:00
|
|
|
# Emit ArduPilot documentation in an machine readable XML format
|
2013-02-16 18:01:55 -04:00
|
|
|
class XmlEmit(Emit):
|
2021-04-07 00:12:41 -03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
Emit.__init__(self, *args, **kwargs)
|
2020-09-15 07:27:21 -03:00
|
|
|
self.wiki_fname = 'apm.pdef.xml'
|
|
|
|
self.f = open(self.wiki_fname, mode='w')
|
|
|
|
self.preamble = '''<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<!-- Dynamically generated list of documented parameters (generated by param_parse.py) -->
|
|
|
|
'''
|
|
|
|
self.f.write(self.preamble)
|
2020-10-07 17:14:20 -03:00
|
|
|
self.paramfile = etree.Element('paramfile')
|
|
|
|
self.vehicles = etree.SubElement(self.paramfile, 'vehicles')
|
|
|
|
self.libraries = etree.SubElement(self.paramfile, 'libraries')
|
|
|
|
self.current_element = self.vehicles
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
def close(self):
|
2021-04-01 22:53:42 -03:00
|
|
|
# etree.indent(self.paramfile) # not available on thor, Ubuntu 16.04
|
2020-10-07 17:14:20 -03:00
|
|
|
pretty_xml = etree.tostring(self.paramfile, pretty_print=True, encoding='unicode')
|
|
|
|
self.f.write(pretty_xml)
|
2016-07-31 07:22:06 -03:00
|
|
|
self.f.close()
|
|
|
|
|
2013-02-16 18:01:55 -04:00
|
|
|
def emit_comment(self, s):
|
|
|
|
self.f.write("<!-- " + s + " -->")
|
|
|
|
|
|
|
|
def start_libraries(self):
|
2020-10-07 17:14:20 -03:00
|
|
|
self.current_element = self.libraries
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2018-12-06 05:07:51 -04:00
|
|
|
def emit(self, g):
|
2021-05-12 01:45:27 -03:00
|
|
|
xml_parameters = etree.SubElement(self.current_element, 'parameters', name=g.reference) # i.e. ArduPlane
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2013-02-16 18:01:55 -04:00
|
|
|
for param in g.params:
|
|
|
|
# Begin our parameter node
|
2016-07-31 07:22:06 -03:00
|
|
|
if hasattr(param, 'DisplayName'):
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_param = etree.SubElement(xml_parameters, 'param', humanName=param.DisplayName, name=param.name) # i.e. ArduPlane (ArduPlane:FOOPARM)
|
2013-02-16 18:01:55 -04:00
|
|
|
else:
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_param = etree.SubElement(xml_parameters, 'param', name=param.name)
|
2016-07-31 07:22:06 -03:00
|
|
|
|
|
|
|
if hasattr(param, 'Description'):
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_param.set('documentation', param.Description) # i.e. parameter docs
|
2013-04-15 21:51:01 -03:00
|
|
|
if hasattr(param, 'User'):
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_param.set('user', param.User) # i.e. Standard or Advanced
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2020-02-01 20:56:29 -04:00
|
|
|
if hasattr(param, 'Calibration'):
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_param.set('calibration', param.Calibration)
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2013-02-16 18:01:55 -04:00
|
|
|
# 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:
|
|
|
|
if field == 'Values' and Emit.prog_values_field.match(param.__dict__[field]):
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_values = etree.SubElement(xml_param, 'values')
|
2013-02-16 18:01:55 -04:00
|
|
|
values = (param.__dict__[field]).split(',')
|
|
|
|
for value in values:
|
|
|
|
v = value.split(':')
|
2018-03-25 22:35:13 -03:00
|
|
|
if len(v) != 2:
|
|
|
|
raise ValueError("Bad value (%s)" % v)
|
2020-10-07 17:14:20 -03:00
|
|
|
# i.e. numeric value, string label
|
|
|
|
xml_value = etree.SubElement(xml_values, 'value', code=v[0])
|
|
|
|
xml_value.text = v[1]
|
2016-07-31 07:22:06 -03:00
|
|
|
|
2017-05-02 07:36:14 -03:00
|
|
|
elif field == 'Units':
|
|
|
|
abreviated_units = param.__dict__[field]
|
|
|
|
if abreviated_units != '':
|
|
|
|
units = known_units[abreviated_units] # use the known_units dictionary to convert the abreviated unit into a full textual one
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_field1 = etree.SubElement(xml_param, 'field', name=field) # i.e. A/s
|
|
|
|
xml_field1.text = abreviated_units
|
|
|
|
xml_field2 = etree.SubElement(xml_param, 'field', name='UnitText') # i.e. ampere per second
|
|
|
|
xml_field2.text = units
|
2013-02-16 18:01:55 -04:00
|
|
|
else:
|
2020-10-07 17:14:20 -03:00
|
|
|
xml_field = etree.SubElement(xml_param, 'field', name=field)
|
|
|
|
xml_field.text = param.__dict__[field]
|
2013-02-16 18:01:55 -04:00
|
|
|
|
2020-10-07 17:14:20 -03:00
|
|
|
if xml_param.text is None and not len(xml_param):
|
|
|
|
xml_param.text = '\n' # add </param> on next line in case of empty element.
|