Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/autotest/logger_metadata/emit_rst.py
Views: 1799
from __future__ import print_function12import emitter34class RSTEmitter(emitter.Emitter):5def preface(self):6return """.. Dynamically generated list of Logger Messages7.. This page was generated using Tools/autotest/logger_metdata/parse.py89.. DO NOT EDIT1011.. _logmessages:1213Onboard Message Log Messages14============================1516This is a list of log messages which may be present in logs produced and stored onboard ArduPilot vehicles.1718"""19def postface(self):20return ""2122def start(self):23self.fh = open("LogMessages.rst", mode='w')24print(self.preface(), file=self.fh)2526def emit(self, doccos, enumerations):27self.start()28for docco in doccos:29print('.. _%s:' % docco.name, file=self.fh)30print("", file=self.fh)31desc = docco.description32print(docco.name, file=self.fh)33print("~" * len(docco.name), file=self.fh)34if desc is not None:35print("\n%s\n" % desc, file=self.fh)3637rows = []38for f in docco.fields_order:39# Populate the description column40if "description" in docco.fields[f]:41fdesc = docco.fields[f]["description"]42else:43fdesc = ""44# Initialise Type/Unit and check for enum/bitfields45ftypeunit = ""46fieldnamething = None47if "bitmaskenum" in docco.fields[f]:48fieldnamething = "bitmaskenum"49table_label = "Bitmask values"50ftypeunit = "bitmask"51elif "valueenum" in docco.fields[f]:52fieldnamething = "valueenum"53table_label = "Values"54ftypeunit = "enum"55# If an enum/bitmask is defined, build the table56if fieldnamething is not None:57enum_name = docco.fields[f][fieldnamething]58if enum_name not in enumerations:59raise Exception("Unknown enum (%s) (have %s)" %60(enum_name, "\n".join(sorted(enumerations.keys()))))61enumeration = enumerations[enum_name]62bitmaskrows = []63for enumentry in enumeration.entries:64# print("enumentry: %s" % str(enumentry))65comment = enumentry.comment66if comment is None:67comment = ""68bitmaskrows.append([enumentry.name, str(enumentry.value), comment])69fdesc += "\n%s:\n\n%s" % (table_label, self.tablify(bitmaskrows))70# Populate the Type/Units column71if "units" in docco.fields[f] and docco.fields[f]["units"] != "":72ftypeunit = docco.fields[f]["units"]73elif "fmt" in docco.fields[f] and "char" in docco.fields[f]["fmt"]:74ftypeunit = docco.fields[f]["fmt"]75# Add the new row76rows.append([f, ftypeunit, fdesc])7778if rows:79print(self.tablify(rows), file=self.fh)8081print("", file=self.fh)82self.stop()8384def stop(self):85print(self.postface(), file=self.fh)86self.fh.close()878889# tablify swiped from rstemit.py9091def tablify_row(self, rowheading, row, widths, height):92joiner = "|"9394row_lines = [x.split("\n") for x in row]95for row_line in row_lines:96row_line.extend([""] * (height - len(row_line)))97if rowheading is not None:98rowheading_lines = rowheading.split("\n")99rowheading_lines.extend([""] * (height - len(rowheading_lines)))100101out_lines = []102for i in range(0, height):103out_line = ""104if rowheading is not None:105rowheading_line = rowheading_lines[i]106out_line += joiner + " " + rowheading_line + " " * (widths[0] - len(rowheading_line) - 1)107joiner = "#"108j = 0109for item in row_lines:110widthnum = j111if rowheading is not None:112widthnum += 1113line = item[i]114out_line += joiner + " " + line + " " * (widths[widthnum] - len(line) - 1)115joiner = "|"116j += 1117out_line += "|"118out_lines.append(out_line)119return "\n".join(out_lines)120121def tablify_longest_row_length(self, rows, rowheadings, headings):122check_width_rows = rows[:]123if headings is not None:124check_width_rows.append(headings)125longest_row_length = 0126for row in check_width_rows:127if len(row) > longest_row_length:128longest_row_length = len(row)129if rowheadings is not None:130longest_row_length += 1131return longest_row_length132133def longest_line_in_string(self, string):134longest = 0135for line in string.split("\n"):136if len(line) > longest:137longest = len(line)138return longest139140def tablify_calc_row_widths_heights(self, rows, rowheadings, headings):141rows_to_check = []142if headings is not None:143rows_to_check.append(headings)144rows_to_check.extend(rows[:])145146heights = [0] * len(rows_to_check)147148longest_row_length = self.tablify_longest_row_length(rows, rowheadings, headings)149widths = [0] * longest_row_length150151all_rowheadings = []152if rowheadings is not None:153if headings is not None:154all_rowheadings.append("")155all_rowheadings.extend(rowheadings)156157for rownum in range(0, len(rows_to_check)):158row = rows_to_check[rownum]159values_to_check = []160if rowheadings is not None:161values_to_check.append(all_rowheadings[rownum])162values_to_check.extend(row[:])163colnum = 0164for value in values_to_check:165height = len(value.split("\n"))166if height > heights[rownum]:167heights[rownum] = height168longest_line = self.longest_line_in_string(value)169width = longest_line + 2 # +2 for leading/trailing ws170if width > widths[colnum]:171widths[colnum] = width172colnum += 1173return (widths, heights)174175def tablify(self, rows, headings=None, rowheadings=None):176177(widths, heights) = self.tablify_calc_row_widths_heights(rows, rowheadings, headings)178179# create dividing lines180bar = ""181heading_bar = ""182for width in widths:183bar += "+"184heading_bar += "+"185bar += "-" * width186heading_bar += "=" * width187bar += "+"188heading_bar += "+"189190# create table191ret = bar + "\n"192if headings is not None:193rowheading = None194if rowheadings is not None:195rowheading = ""196ret += self.tablify_row(rowheading, headings, widths, heights[0]) + "\n"197ret += heading_bar + "\n"198for i in range(0, len(rows)):199rowheading = None200height = i201if rowheadings is not None:202rowheading = rowheadings[i]203if headings is not None:204height += 1205ret += self.tablify_row(rowheading, rows[i], widths, heights[height]) + "\n"206ret += bar + "\n"207208return ret209210211212