File: doc_helpers.py

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (89 lines) | stat: -rw-r--r-- 3,258 bytes parent folder | download | duplicates (15)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


class MismatchedRowLengthsException(Exception):
    """
    This exception is thrown when there is a mismatch between the number of items in a row,
    and the number of headers defined.
    """

    pass


class TableBuilder:
    """
    Helper class for building tables.
    """

    def __init__(self, title, widths, header_rows, headers, indent=0):
        """
        :param title: str - Title of the table
        :param widths: list of str - Widths of each column of the table
        :param header_rows: int - Number of header rows
        :param headers: 2D list of str - Headers
        :param indent: int - Number of spaces to indent table
        """
        if not isinstance(title, str):
            raise TypeError("TableBuilder attribute title must be a string.")
        if not isinstance(widths, list) or not isinstance(widths[0], int):
            raise TypeError("TableBuilder attribute widths must be a list of integers.")
        if not isinstance(header_rows, int):
            raise TypeError("TableBuilder attribute header_rows must be an integer.")
        if (
            not isinstance(headers, list)
            or not isinstance(headers[0], list)
            or not isinstance(headers[0][0], str)
        ):
            raise TypeError(
                "TableBuilder attribute headers must be a two-dimensional list of strings."
            )
        if not isinstance(indent, int):
            raise TypeError("TableBuilder attribute indent must be an integer.")

        self.title = title
        self.widths = widths
        self.header_rows = header_rows
        self.headers = headers
        self.indent = " " * indent
        self.table = ""
        self._build_table()

    def _build_table(self):
        if len(self.widths) != len(self.headers[0]):
            raise MismatchedRowLengthsException(
                "Number of table headers must match number of column widths."
            )
        widths = " ".join(map(str, self.widths))
        self.table += (
            f"{self.indent}.. list-table:: **{self.title}**\n"
            f"{self.indent}   :widths: {widths}\n"
            f"{self.indent}   :header-rows: {self.header_rows}\n\n"
        )
        self.add_rows(self.headers)

    def add_rows(self, rows):
        if (
            type(rows) is not list
            or type(rows[0]) is not list
            or type(rows[0][0]) is not str
        ):
            raise TypeError("add_rows() requires a two-dimensional list of strings.")
        for row in rows:
            self.add_row(row)

    def add_row(self, values):
        if len(values) != len(self.widths):
            raise MismatchedRowLengthsException(
                "Number of items in a row must must number of columns defined."
            )
        for i, val in enumerate(values):
            if i == 0:
                self.table += f"{self.indent}   * - **{val}**\n"
            else:
                self.table += f"{self.indent}     - {val}\n"

    def finish_table(self):
        self.table += "\n"
        return self.table