Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/param_metadata/xmlemit.py
9743 views
1
# flake8: noqa
2
3
from lxml import etree
4
5
from emit import Emit
6
from param import known_param_fields, known_units
7
import re
8
9
# Emit ArduPilot documentation in an machine readable XML format
10
class XmlEmit(Emit):
11
def __init__(self, *args, **kwargs):
12
Emit.__init__(self, *args, **kwargs)
13
self.wiki_fname = 'apm.pdef.xml'
14
self.f = open(self.wiki_fname, mode='w')
15
self.preamble = '''<?xml version="1.0" encoding="utf-8"?>
16
<!-- Dynamically generated list of documented parameters (generated by param_parse.py) -->
17
'''
18
self.f.write(self.preamble)
19
self.paramfile = etree.Element('paramfile')
20
self.vehicles = etree.SubElement(self.paramfile, 'vehicles')
21
self.libraries = etree.SubElement(self.paramfile, 'libraries')
22
self.current_element = self.vehicles
23
24
def close(self):
25
# etree.indent(self.paramfile) # not available on thor, Ubuntu 16.04
26
pretty_xml = etree.tostring(self.paramfile, pretty_print=True, encoding='unicode')
27
self.f.write(pretty_xml)
28
self.f.close()
29
30
def emit_comment(self, s):
31
self.f.write("<!-- " + s + " -->")
32
33
def start_libraries(self):
34
self.current_element = self.libraries
35
36
def add_xml_subtree_for_param_field(self, param, xml_param, field, new_element_name, item_name):
37
'''assumes that "field" is a comma-separated list of items,
38
creates a subtree with those elements within'''
39
xml_values = etree.SubElement(xml_param, new_element_name)
40
values = (param.__dict__[field]).split(',')
41
nv_unsorted = {}
42
for value in values:
43
v = value.split(':')
44
if len(v) != 2:
45
raise ValueError("Bad value (%s)" % v)
46
# i.e. numeric value, string label
47
if v[0] in nv_unsorted:
48
raise ValueError("%s already exists" % v[0])
49
nv_unsorted[v[0]] = v[1]
50
51
all_keys = nv_unsorted.keys()
52
if hasattr(param, 'SortValues'):
53
sort = getattr(param, 'SortValues').lower()
54
zero_at_top = False
55
if sort == 'alphabeticalzeroattop':
56
zero_at_top = True
57
else:
58
raise ValueError("Unknown sort (%s)" % sort)
59
60
all_keys = self.sorted_Values_keys(nv_unsorted, zero_at_top=zero_at_top)
61
62
for key in all_keys:
63
value = nv_unsorted[key].strip()
64
code = key.rstrip().strip()
65
xml_value = etree.SubElement(xml_values, item_name, code=code)
66
xml_value.text = value
67
68
def sorted_Values_keys(self, nv_pairs, zero_at_top=False):
69
'''sorts name/value pairs derived from items in @Values. Sorts by
70
value, with special attention paid to common "Do nothing" values'''
71
keys = nv_pairs.keys()
72
def sort_key(value):
73
description = nv_pairs[value]
74
if zero_at_top:
75
# make sure -1 and 0 appear at the top of the list
76
if value == "-1":
77
return "0000000"
78
if value == "0":
79
return "0000001"
80
return description
81
return sorted(keys, key=sort_key)
82
83
def emit(self, g):
84
xml_parameters = etree.SubElement(self.current_element, 'parameters', name=g.reference) # i.e. ArduPlane
85
86
for param in g.params:
87
if not self.should_emit_param(param):
88
continue
89
# Begin our parameter node
90
if hasattr(param, 'DisplayName'):
91
xml_param = etree.SubElement(xml_parameters, 'param', humanName=param.DisplayName, name=param.name) # i.e. ArduPlane (ArduPlane:FOOPARM)
92
else:
93
xml_param = etree.SubElement(xml_parameters, 'param', name=param.name)
94
95
if hasattr(param, 'Description'):
96
xml_param.set('documentation', param.Description) # i.e. parameter docs
97
if hasattr(param, 'User'):
98
xml_param.set('user', param.User) # i.e. Standard or Advanced
99
100
if hasattr(param, 'Calibration'):
101
xml_param.set('calibration', param.Calibration)
102
103
# Add values as children of this node
104
for field in param.__dict__.keys():
105
if not self.should_emit_field(param, field):
106
continue
107
if field not in ['name', 'DisplayName', 'Description', 'User', 'SortValues'] and field in known_param_fields:
108
# we emit Bitmask as both a sub-element (so that
109
# consumers don't need to parse the Bitmask list),
110
# but also as a text so we don't break existing
111
# implementations. Contrast with "values", which
112
# is only emitted as a sub-element.
113
if field == 'Bitmask':
114
self.add_xml_subtree_for_param_field(
115
param,
116
xml_param,
117
field='Bitmask',
118
new_element_name='bitmask',
119
item_name='bit',
120
)
121
122
if field == 'Values' and Emit.prog_values_field.match(param.__dict__[field]):
123
self.add_xml_subtree_for_param_field(
124
param,
125
xml_param,
126
field='Values',
127
new_element_name='values',
128
item_name='value',
129
)
130
131
elif field == 'Units':
132
abreviated_units = param.__dict__[field]
133
if abreviated_units != '':
134
units = known_units[abreviated_units] # use the known_units dictionary to convert the abbreviated unit into a full textual one
135
xml_field1 = etree.SubElement(xml_param, 'field', name=field) # i.e. A/s
136
xml_field1.text = abreviated_units
137
xml_field2 = etree.SubElement(xml_param, 'field', name='UnitText') # i.e. ampere per second
138
xml_field2.text = units
139
else:
140
xml_field = etree.SubElement(xml_param, 'field', name=field)
141
xml_field.text = param.__dict__[field]
142
143
if xml_param.text is None and not len(xml_param):
144
xml_param.text = '\n' # add </param> on next line in case of empty element.
145
146