Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/logger_metadata/emit_rst.py
9842 views
1
'''
2
AP_FLAKE8_CLEAN
3
'''
4
import emitter
5
6
7
class RSTEmitter(emitter.Emitter):
8
def preface(self):
9
return """.. Dynamically generated list of Logger Messages
10
.. This page was generated using Tools/autotest/logger_metdata/parse.py
11
12
.. DO NOT EDIT
13
14
.. _logmessages:
15
16
Onboard Message Log Messages
17
============================
18
19
This is a list of log messages which may be present in logs produced and stored onboard ArduPilot vehicles.
20
21
"""
22
23
def postface(self):
24
return ""
25
26
def start(self):
27
self.fh = open("LogMessages.rst", mode='w')
28
print(self.preface(), file=self.fh)
29
30
def emit(self, doccos, enumerations):
31
self.start()
32
for docco in doccos:
33
print('.. _%s:' % docco.name, file=self.fh)
34
print("", file=self.fh)
35
desc = docco.description
36
print(docco.name, file=self.fh)
37
print("~" * len(docco.name), file=self.fh)
38
if desc is not None:
39
print("\n%s\n" % desc, file=self.fh)
40
41
rows = []
42
for f in docco.fields_order:
43
# Populate the description column
44
if "description" in docco.fields[f]:
45
fdesc = docco.fields[f]["description"]
46
else:
47
fdesc = ""
48
# Initialise Type/Unit and check for enum/bitfields
49
ftypeunit = ""
50
fieldnamething = None
51
if "bitmaskenum" in docco.fields[f]:
52
fieldnamething = "bitmaskenum"
53
table_label = "Bitmask values"
54
ftypeunit = "bitmask"
55
elif "valueenum" in docco.fields[f]:
56
fieldnamething = "valueenum"
57
table_label = "Values"
58
ftypeunit = "enum"
59
# If an enum/bitmask is defined, build the table
60
if fieldnamething is not None:
61
enum_name = docco.fields[f][fieldnamething]
62
if enum_name not in enumerations:
63
raise Exception("Unknown enum (%s) (have %s)" %
64
(enum_name, "\n".join(sorted(enumerations.keys()))))
65
enumeration = enumerations[enum_name]
66
bitmaskrows = []
67
for enumentry in enumeration.entries:
68
# print("enumentry: %s" % str(enumentry))
69
comment = enumentry.comment
70
if comment is None:
71
comment = ""
72
bitmaskrows.append([enumentry.name, str(enumentry.value), comment])
73
fdesc += "\n%s:\n\n%s" % (table_label, self.tablify(bitmaskrows))
74
# Populate the Type/Units column
75
if "units" in docco.fields[f] and docco.fields[f]["units"] != "":
76
ftypeunit = docco.fields[f]["units"]
77
elif "fmt" in docco.fields[f] and "char" in docco.fields[f]["fmt"]:
78
ftypeunit = docco.fields[f]["fmt"]
79
# Add the new row
80
rows.append([f, ftypeunit, fdesc])
81
82
if rows:
83
print(self.tablify(rows), file=self.fh)
84
85
print("", file=self.fh)
86
self.stop()
87
88
def stop(self):
89
print(self.postface(), file=self.fh)
90
self.fh.close()
91
92
# tablify swiped from rstemit.py
93
94
def tablify_row(self, rowheading, row, widths, height):
95
joiner = "|"
96
97
row_lines = [x.split("\n") for x in row]
98
for row_line in row_lines:
99
row_line.extend([""] * (height - len(row_line)))
100
if rowheading is not None:
101
rowheading_lines = rowheading.split("\n")
102
rowheading_lines.extend([""] * (height - len(rowheading_lines)))
103
104
out_lines = []
105
for i in range(0, height):
106
out_line = ""
107
if rowheading is not None:
108
rowheading_line = rowheading_lines[i]
109
out_line += joiner + " " + rowheading_line + " " * (widths[0] - len(rowheading_line) - 1)
110
joiner = "#"
111
j = 0
112
for item in row_lines:
113
widthnum = j
114
if rowheading is not None:
115
widthnum += 1
116
line = item[i]
117
out_line += joiner + " " + line + " " * (widths[widthnum] - len(line) - 1)
118
joiner = "|"
119
j += 1
120
out_line += "|"
121
out_lines.append(out_line)
122
return "\n".join(out_lines)
123
124
def tablify_longest_row_length(self, rows, rowheadings, headings):
125
check_width_rows = rows[:]
126
if headings is not None:
127
check_width_rows.append(headings)
128
longest_row_length = 0
129
for row in check_width_rows:
130
if len(row) > longest_row_length:
131
longest_row_length = len(row)
132
if rowheadings is not None:
133
longest_row_length += 1
134
return longest_row_length
135
136
def longest_line_in_string(self, string):
137
longest = 0
138
for line in string.split("\n"):
139
if len(line) > longest:
140
longest = len(line)
141
return longest
142
143
def tablify_calc_row_widths_heights(self, rows, rowheadings, headings):
144
rows_to_check = []
145
if headings is not None:
146
rows_to_check.append(headings)
147
rows_to_check.extend(rows[:])
148
149
heights = [0] * len(rows_to_check)
150
151
longest_row_length = self.tablify_longest_row_length(rows, rowheadings, headings)
152
widths = [0] * longest_row_length
153
154
all_rowheadings = []
155
if rowheadings is not None:
156
if headings is not None:
157
all_rowheadings.append("")
158
all_rowheadings.extend(rowheadings)
159
160
for rownum in range(0, len(rows_to_check)):
161
row = rows_to_check[rownum]
162
values_to_check = []
163
if rowheadings is not None:
164
values_to_check.append(all_rowheadings[rownum])
165
values_to_check.extend(row[:])
166
colnum = 0
167
for value in values_to_check:
168
height = len(value.split("\n"))
169
if height > heights[rownum]:
170
heights[rownum] = height
171
longest_line = self.longest_line_in_string(value)
172
width = longest_line + 2 # +2 for leading/trailing ws
173
if width > widths[colnum]:
174
widths[colnum] = width
175
colnum += 1
176
return (widths, heights)
177
178
def tablify(self, rows, headings=None, rowheadings=None):
179
180
(widths, heights) = self.tablify_calc_row_widths_heights(rows, rowheadings, headings)
181
182
# create dividing lines
183
bar = ""
184
heading_bar = ""
185
for width in widths:
186
bar += "+"
187
heading_bar += "+"
188
bar += "-" * width
189
heading_bar += "=" * width
190
bar += "+"
191
heading_bar += "+"
192
193
# create table
194
ret = bar + "\n"
195
if headings is not None:
196
rowheading = None
197
if rowheadings is not None:
198
rowheading = ""
199
ret += self.tablify_row(rowheading, headings, widths, heights[0]) + "\n"
200
ret += heading_bar + "\n"
201
for i in range(0, len(rows)):
202
rowheading = None
203
height = i
204
if rowheadings is not None:
205
rowheading = rowheadings[i]
206
if headings is not None:
207
height += 1
208
ret += self.tablify_row(rowheading, rows[i], widths, heights[height]) + "\n"
209
ret += bar + "\n"
210
211
return ret
212
213