|
| 1 | +from __future__ import annotations |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from .alignment import Alignment |
| 5 | + |
| 6 | +from .annotations import SupportsStr |
| 7 | + |
| 8 | + |
| 9 | +class Table2AsciiError(Exception): |
| 10 | + """Base class for all table2ascii exceptions""" |
| 11 | + |
| 12 | + def _message(self): |
| 13 | + """Return the error message""" |
| 14 | + raise NotImplementedError |
| 15 | + |
| 16 | + |
| 17 | +class TableOptionError(Table2AsciiError, ValueError): |
| 18 | + """Base class for exceptions raised when an invalid option |
| 19 | + is passed when creating an ascii table |
| 20 | +
|
| 21 | + This class is a subclass of :class:`Table2AsciiError` and :class:`ValueError`. |
| 22 | + """ |
| 23 | + |
| 24 | + |
| 25 | +class ColumnCountMismatchError(TableOptionError): |
| 26 | + """Base class for exceptions raised when a parameter has an |
| 27 | + invalid number of columns |
| 28 | +
|
| 29 | + This class is a subclass of :class:`TableOptionError`. |
| 30 | + """ |
| 31 | + |
| 32 | + expected_columns: int |
| 33 | + |
| 34 | + |
| 35 | +class FooterColumnCountMismatchError(ColumnCountMismatchError): |
| 36 | + """Exception raised when the number of columns in the footer |
| 37 | + does not match the number of columns in the header |
| 38 | +
|
| 39 | + This class is a subclass of :class:`ColumnCountMismatchError`. |
| 40 | +
|
| 41 | + Attributes: |
| 42 | + footer (list[SupportsStr]): The footer that caused the error |
| 43 | + expected_columns (int): The number of columns that were expected |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, footer: list[SupportsStr], expected_columns: int): |
| 47 | + self.footer = footer |
| 48 | + self.expected_columns = expected_columns |
| 49 | + super().__init__(self._message()) |
| 50 | + |
| 51 | + def _message(self): |
| 52 | + return ( |
| 53 | + f"Footer column count mismatch: {len(self.footer)} columns " |
| 54 | + f"found, expected {self.expected_columns}." |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class BodyColumnCountMismatchError(ColumnCountMismatchError): |
| 59 | + """Exception raised when the number of columns in the body |
| 60 | + does not match the number of columns in the footer or header |
| 61 | +
|
| 62 | + This class is a subclass of :class:`ColumnCountMismatchError`. |
| 63 | +
|
| 64 | + Attributes: |
| 65 | + body (list[list[SupportsStr]]): The body that caused the error |
| 66 | + expected_columns (int): The number of columns that were expected |
| 67 | + first_invalid_row (list[SupportsStr]): The first row with an invalid column count |
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, body: list[list[SupportsStr]], expected_columns: int): |
| 71 | + self.body = body |
| 72 | + self.expected_columns = expected_columns |
| 73 | + self.first_invalid_row = next( |
| 74 | + (row for row in self.body if len(row) != self.expected_columns) |
| 75 | + ) |
| 76 | + super().__init__(self._message()) |
| 77 | + |
| 78 | + def _message(self): |
| 79 | + return ( |
| 80 | + f"Body column count mismatch: A row with {len(self.first_invalid_row)} " |
| 81 | + f"columns was found, expected {self.expected_columns}." |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +class AlignmentCountMismatchError(ColumnCountMismatchError): |
| 86 | + """Exception raised when the number of alignments does not match |
| 87 | + the number of columns in the table |
| 88 | +
|
| 89 | + This class is a subclass of :class:`ColumnCountMismatchError`. |
| 90 | +
|
| 91 | + Attributes: |
| 92 | + alignments (list[Alignment]): The alignments that caused the error |
| 93 | + expected_columns (int): The number of columns that were expected |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__(self, alignments: list[Alignment], expected_columns: int): |
| 97 | + self.alignments = alignments |
| 98 | + self.expected_columns = expected_columns |
| 99 | + super().__init__(self._message()) |
| 100 | + |
| 101 | + def _message(self): |
| 102 | + return ( |
| 103 | + f"Alignment count mismatch: {len(self.alignments)} alignments " |
| 104 | + f"found, expected {self.expected_columns}." |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +class ColumnWidthsCountMismatchError(ColumnCountMismatchError): |
| 109 | + """Exception raised when the number of column widths does not match |
| 110 | + the number of columns in the table |
| 111 | +
|
| 112 | + This class is a subclass of :class:`ColumnCountMismatchError`. |
| 113 | +
|
| 114 | + Attributes: |
| 115 | + column_widths (list[Optional[int]]): The column widths that caused the error |
| 116 | + expected_columns (int): The number of columns that were expected |
| 117 | + """ |
| 118 | + |
| 119 | + def __init__(self, column_widths: list[int | None], expected_columns: int): |
| 120 | + self.column_widths = column_widths |
| 121 | + self.expected_columns = expected_columns |
| 122 | + super().__init__(self._message()) |
| 123 | + |
| 124 | + def _message(self): |
| 125 | + return ( |
| 126 | + f"Column widths count mismatch: {len(self.column_widths)} column widths " |
| 127 | + f"found, expected {self.expected_columns}." |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class InvalidCellPaddingError(TableOptionError): |
| 132 | + """Exception raised when the cell padding is invalid |
| 133 | +
|
| 134 | + This class is a subclass of :class:`TableOptionError`. |
| 135 | +
|
| 136 | + Attributes: |
| 137 | + padding (int): The padding that caused the error |
| 138 | + """ |
| 139 | + |
| 140 | + def __init__(self, padding: int): |
| 141 | + self.padding = padding |
| 142 | + super().__init__(self._message()) |
| 143 | + |
| 144 | + def _message(self): |
| 145 | + return f"Invalid cell padding: {self.padding} is not a positive integer." |
| 146 | + |
| 147 | + |
| 148 | +class ColumnWidthTooSmallError(TableOptionError): |
| 149 | + """Exception raised when the column width is smaller than the minimum |
| 150 | + number of characters that are required to display the content |
| 151 | +
|
| 152 | + This class is a subclass of :class:`TableOptionError`. |
| 153 | +
|
| 154 | + Attributes: |
| 155 | + column_index (int): The index of the column that caused the error |
| 156 | + column_width (int): The column width that caused the error |
| 157 | + min_width (int): The minimum width that is allowed |
| 158 | + """ |
| 159 | + |
| 160 | + def __init__(self, column_index: int, column_width: int, min_width: int): |
| 161 | + self.column_index = column_index |
| 162 | + self.column_width = column_width |
| 163 | + self.min_width = min_width |
| 164 | + super().__init__(self._message()) |
| 165 | + |
| 166 | + def _message(self): |
| 167 | + return ( |
| 168 | + f"Column width too small: The column width for column index {self.column_index} " |
| 169 | + f" of `column_widths` is {self.column_width}, but the minimum width " |
| 170 | + f"required to display the content is {self.min_width}." |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +class InvalidAlignmentError(TableOptionError): |
| 175 | + """Exception raised when an invalid value is passed for an :class:`Alignment` |
| 176 | +
|
| 177 | + This class is a subclass of :class:`TableOptionError`. |
| 178 | +
|
| 179 | + Attributes: |
| 180 | + alignment (Any): The alignment value that caused the error |
| 181 | + """ |
| 182 | + |
| 183 | + def __init__(self, alignment: Any): |
| 184 | + self.alignment = alignment |
| 185 | + super().__init__(self._message()) |
| 186 | + |
| 187 | + def _message(self): |
| 188 | + return ( |
| 189 | + f"Invalid alignment: {self.alignment!r} is not a valid alignment. " |
| 190 | + f"Valid alignments are: {', '.join(a.__repr__() for a in Alignment)}" |
| 191 | + ) |
0 commit comments