Path: blob/master/Tools/autotest/logger_metadata/emit_rst.py
9842 views
'''1AP_FLAKE8_CLEAN2'''3import emitter456class RSTEmitter(emitter.Emitter):7def preface(self):8return """.. Dynamically generated list of Logger Messages9.. This page was generated using Tools/autotest/logger_metdata/parse.py1011.. DO NOT EDIT1213.. _logmessages:1415Onboard Message Log Messages16============================1718This is a list of log messages which may be present in logs produced and stored onboard ArduPilot vehicles.1920"""2122def postface(self):23return ""2425def start(self):26self.fh = open("LogMessages.rst", mode='w')27print(self.preface(), file=self.fh)2829def emit(self, doccos, enumerations):30self.start()31for docco in doccos:32print('.. _%s:' % docco.name, file=self.fh)33print("", file=self.fh)34desc = docco.description35print(docco.name, file=self.fh)36print("~" * len(docco.name), file=self.fh)37if desc is not None:38print("\n%s\n" % desc, file=self.fh)3940rows = []41for f in docco.fields_order:42# Populate the description column43if "description" in docco.fields[f]:44fdesc = docco.fields[f]["description"]45else:46fdesc = ""47# Initialise Type/Unit and check for enum/bitfields48ftypeunit = ""49fieldnamething = None50if "bitmaskenum" in docco.fields[f]:51fieldnamething = "bitmaskenum"52table_label = "Bitmask values"53ftypeunit = "bitmask"54elif "valueenum" in docco.fields[f]:55fieldnamething = "valueenum"56table_label = "Values"57ftypeunit = "enum"58# If an enum/bitmask is defined, build the table59if fieldnamething is not None:60enum_name = docco.fields[f][fieldnamething]61if enum_name not in enumerations:62raise Exception("Unknown enum (%s) (have %s)" %63(enum_name, "\n".join(sorted(enumerations.keys()))))64enumeration = enumerations[enum_name]65bitmaskrows = []66for enumentry in enumeration.entries:67# print("enumentry: %s" % str(enumentry))68comment = enumentry.comment69if comment is None:70comment = ""71bitmaskrows.append([enumentry.name, str(enumentry.value), comment])72fdesc += "\n%s:\n\n%s" % (table_label, self.tablify(bitmaskrows))73# Populate the Type/Units column74if "units" in docco.fields[f] and docco.fields[f]["units"] != "":75ftypeunit = docco.fields[f]["units"]76elif "fmt" in docco.fields[f] and "char" in docco.fields[f]["fmt"]:77ftypeunit = docco.fields[f]["fmt"]78# Add the new row79rows.append([f, ftypeunit, fdesc])8081if rows:82print(self.tablify(rows), file=self.fh)8384print("", file=self.fh)85self.stop()8687def stop(self):88print(self.postface(), file=self.fh)89self.fh.close()9091# tablify swiped from rstemit.py9293def tablify_row(self, rowheading, row, widths, height):94joiner = "|"9596row_lines = [x.split("\n") for x in row]97for row_line in row_lines:98row_line.extend([""] * (height - len(row_line)))99if rowheading is not None:100rowheading_lines = rowheading.split("\n")101rowheading_lines.extend([""] * (height - len(rowheading_lines)))102103out_lines = []104for i in range(0, height):105out_line = ""106if rowheading is not None:107rowheading_line = rowheading_lines[i]108out_line += joiner + " " + rowheading_line + " " * (widths[0] - len(rowheading_line) - 1)109joiner = "#"110j = 0111for item in row_lines:112widthnum = j113if rowheading is not None:114widthnum += 1115line = item[i]116out_line += joiner + " " + line + " " * (widths[widthnum] - len(line) - 1)117joiner = "|"118j += 1119out_line += "|"120out_lines.append(out_line)121return "\n".join(out_lines)122123def tablify_longest_row_length(self, rows, rowheadings, headings):124check_width_rows = rows[:]125if headings is not None:126check_width_rows.append(headings)127longest_row_length = 0128for row in check_width_rows:129if len(row) > longest_row_length:130longest_row_length = len(row)131if rowheadings is not None:132longest_row_length += 1133return longest_row_length134135def longest_line_in_string(self, string):136longest = 0137for line in string.split("\n"):138if len(line) > longest:139longest = len(line)140return longest141142def tablify_calc_row_widths_heights(self, rows, rowheadings, headings):143rows_to_check = []144if headings is not None:145rows_to_check.append(headings)146rows_to_check.extend(rows[:])147148heights = [0] * len(rows_to_check)149150longest_row_length = self.tablify_longest_row_length(rows, rowheadings, headings)151widths = [0] * longest_row_length152153all_rowheadings = []154if rowheadings is not None:155if headings is not None:156all_rowheadings.append("")157all_rowheadings.extend(rowheadings)158159for rownum in range(0, len(rows_to_check)):160row = rows_to_check[rownum]161values_to_check = []162if rowheadings is not None:163values_to_check.append(all_rowheadings[rownum])164values_to_check.extend(row[:])165colnum = 0166for value in values_to_check:167height = len(value.split("\n"))168if height > heights[rownum]:169heights[rownum] = height170longest_line = self.longest_line_in_string(value)171width = longest_line + 2 # +2 for leading/trailing ws172if width > widths[colnum]:173widths[colnum] = width174colnum += 1175return (widths, heights)176177def tablify(self, rows, headings=None, rowheadings=None):178179(widths, heights) = self.tablify_calc_row_widths_heights(rows, rowheadings, headings)180181# create dividing lines182bar = ""183heading_bar = ""184for width in widths:185bar += "+"186heading_bar += "+"187bar += "-" * width188heading_bar += "=" * width189bar += "+"190heading_bar += "+"191192# create table193ret = bar + "\n"194if headings is not None:195rowheading = None196if rowheadings is not None:197rowheading = ""198ret += self.tablify_row(rowheading, headings, widths, heights[0]) + "\n"199ret += heading_bar + "\n"200for i in range(0, len(rows)):201rowheading = None202height = i203if rowheadings is not None:204rowheading = rowheadings[i]205if headings is not None:206height += 1207ret += self.tablify_row(rowheading, rows[i], widths, heights[height]) + "\n"208ret += bar + "\n"209210return ret211212213