Define TypeTable and TypeTableRow classes

Hopefully this will make it a bit easier to understand what's going on.
This commit is contained in:
Richard van der Hoff 2017-11-03 14:45:56 +00:00
parent caf1333d12
commit 96650e2824
4 changed files with 125 additions and 91 deletions

View file

@ -59,10 +59,12 @@ import importlib
import json
import logging
import os
import re
import sys
from textwrap import TextWrapper
from matrix_templates.units import TypeTableRow
def create_from_template(template, sections):
return template.render(sections)
@ -117,15 +119,23 @@ def main(input_module, files=None, out_dir=None, verbose=False, substitutions={}
Given a list of rows, returns a list giving the maximum length of the
values in each column.
:param list[dict[str, str]] input: a list of rows. Each row should be a
dict with the keys given in ``keys``.
:param list[TypeTableRow|dict[str,str]] input:
a list of rows
:param list[str] keys: the keys corresponding to the table columns
:param list[int] defaults: for each column, the default column width.
:param int default_width: if ``defaults`` is shorter than ``keys``, this
will be used as a fallback
"""
def getrowattribute(row, k):
# the row may be a dict (particularly the title row, which is
# generated by the template
if not isinstance(row, TypeTableRow):
return row[k]
return getattr(row, k)
def colwidth(key, default):
return reduce(max, (len(row[key]) for row in input),
rowwidths = (len(getrowattribute(row, key)) for row in input)
return reduce(max, rowwidths,
default if default is not None else default_width)
results = map(colwidth, keys, defaults)