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