CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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