pyreportkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyreportkit-0.1.0.dist-info/METADATA +187 -0
- pyreportkit-0.1.0.dist-info/RECORD +222 -0
- pyreportkit-0.1.0.dist-info/WHEEL +5 -0
- pyreportkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- pyreportkit-0.1.0.dist-info/top_level.txt +1 -0
- reportkit/__init__.py +0 -0
- reportkit/components/__init__.py +45 -0
- reportkit/components/document/__init__.py +6 -0
- reportkit/components/document/footer.py +12 -0
- reportkit/components/document/header.py +12 -0
- reportkit/components/document/page.py +18 -0
- reportkit/components/document/report.py +12 -0
- reportkit/components/information/__init__.py +4 -0
- reportkit/components/information/info_grid.py +23 -0
- reportkit/components/information/key_value.py +28 -0
- reportkit/components/layout/__init__.py +9 -0
- reportkit/components/layout/box.py +10 -0
- reportkit/components/layout/column.py +15 -0
- reportkit/components/layout/divider.py +10 -0
- reportkit/components/layout/grid.py +12 -0
- reportkit/components/layout/row.py +15 -0
- reportkit/components/layout/section.py +10 -0
- reportkit/components/layout/spacer.py +12 -0
- reportkit/components/media/__init__.py +4 -0
- reportkit/components/media/chart.py +14 -0
- reportkit/components/media/image.py +25 -0
- reportkit/components/pagination/__init__.py +5 -0
- reportkit/components/pagination/page_break.py +10 -0
- reportkit/components/pagination/page_count.py +10 -0
- reportkit/components/pagination/page_number.py +21 -0
- reportkit/components/table/__init__.py +7 -0
- reportkit/components/table/cell.py +27 -0
- reportkit/components/table/column.py +29 -0
- reportkit/components/table/row.py +25 -0
- reportkit/components/table/style.py +18 -0
- reportkit/components/table/table.py +27 -0
- reportkit/components/typography/__init__.py +6 -0
- reportkit/components/typography/heading.py +13 -0
- reportkit/components/typography/rich_text.py +23 -0
- reportkit/components/typography/text.py +12 -0
- reportkit/components/typography/text_span.py +24 -0
- reportkit/core/__init__.py +4 -0
- reportkit/core/component.py +12 -0
- reportkit/core/container.py +66 -0
- reportkit/core/exceptions.py +0 -0
- reportkit/core/factory.py +53 -0
- reportkit/core/registry.py +50 -0
- reportkit/core/serializer.py +90 -0
- reportkit/exporters/__init__.py +0 -0
- reportkit/exporters/base.py +17 -0
- reportkit/exporters/excel/__init__.py +8 -0
- reportkit/exporters/excel/exceptions.py +4 -0
- reportkit/exporters/excel/exporter.py +68 -0
- reportkit/exporters/excel/renderers/__init__.py +34 -0
- reportkit/exporters/excel/renderers/base.py +27 -0
- reportkit/exporters/excel/renderers/information.py +55 -0
- reportkit/exporters/excel/renderers/layout/__init__.py +7 -0
- reportkit/exporters/excel/renderers/layout/columns.py +48 -0
- reportkit/exporters/excel/renderers/layout/data.py +46 -0
- reportkit/exporters/excel/renderers/layout/page.py +24 -0
- reportkit/exporters/excel/renderers/layout/spacing.py +30 -0
- reportkit/exporters/excel/renderers/layout/stack.py +13 -0
- reportkit/exporters/excel/renderers/media.py +46 -0
- reportkit/exporters/excel/renderers/registry.py +22 -0
- reportkit/exporters/excel/renderers/table/__init__.py +4 -0
- reportkit/exporters/excel/renderers/table/cells.py +17 -0
- reportkit/exporters/excel/renderers/table/data.py +38 -0
- reportkit/exporters/excel/renderers/table/renderer.py +64 -0
- reportkit/exporters/excel/renderers/table/widths.py +29 -0
- reportkit/exporters/excel/renderers/text.py +40 -0
- reportkit/exporters/excel/styles.py +130 -0
- reportkit/exporters/excel/values/__init__.py +5 -0
- reportkit/exporters/excel/values/cells.py +60 -0
- reportkit/exporters/excel/values/components.py +16 -0
- reportkit/exporters/excel/values/formats.py +31 -0
- reportkit/exporters/excel/values/numbers.py +19 -0
- reportkit/exporters/excel/workbook/__init__.py +5 -0
- reportkit/exporters/excel/workbook/data.py +48 -0
- reportkit/exporters/excel/workbook/formatted.py +63 -0
- reportkit/exporters/excel/workbook/styles.py +27 -0
- reportkit/exporters/excel/workbook/workbook.py +65 -0
- reportkit/exporters/excel/worksheet/__init__.py +5 -0
- reportkit/exporters/excel/worksheet/borders.py +30 -0
- reportkit/exporters/excel/worksheet/cells.py +38 -0
- reportkit/exporters/excel/worksheet/constants.py +12 -0
- reportkit/exporters/excel/worksheet/data.py +51 -0
- reportkit/exporters/excel/worksheet/formatted.py +158 -0
- reportkit/exporters/excel/worksheet/page.py +23 -0
- reportkit/exporters/html/__init__.py +6 -0
- reportkit/exporters/html/assets.py +87 -0
- reportkit/exporters/html/direction.py +15 -0
- reportkit/exporters/html/engine.py +37 -0
- reportkit/exporters/html/exporter.py +38 -0
- reportkit/exporters/html/page/__init__.py +3 -0
- reportkit/exporters/html/page/components.py +47 -0
- reportkit/exporters/html/page/margins.py +24 -0
- reportkit/exporters/html/page/numbering.py +31 -0
- reportkit/exporters/html/page/resolver.py +37 -0
- reportkit/exporters/html/page/running.py +30 -0
- reportkit/exporters/html/page/styles.py +22 -0
- reportkit/exporters/html/page/validator.py +76 -0
- reportkit/exporters/html/registry.py +45 -0
- reportkit/exporters/html/renderers/__init__.py +37 -0
- reportkit/exporters/html/renderers/base.py +20 -0
- reportkit/exporters/html/renderers/document/__init__.py +6 -0
- reportkit/exporters/html/renderers/document/footer.py +18 -0
- reportkit/exporters/html/renderers/document/header.py +18 -0
- reportkit/exporters/html/renderers/document/page.py +17 -0
- reportkit/exporters/html/renderers/document/report.py +16 -0
- reportkit/exporters/html/renderers/information/__init__.py +4 -0
- reportkit/exporters/html/renderers/information/info_grid.py +26 -0
- reportkit/exporters/html/renderers/information/key_value.py +18 -0
- reportkit/exporters/html/renderers/layout/__init__.py +9 -0
- reportkit/exporters/html/renderers/layout/box.py +17 -0
- reportkit/exporters/html/renderers/layout/column.py +41 -0
- reportkit/exporters/html/renderers/layout/divider.py +16 -0
- reportkit/exporters/html/renderers/layout/grid.py +48 -0
- reportkit/exporters/html/renderers/layout/row.py +35 -0
- reportkit/exporters/html/renderers/layout/section.py +17 -0
- reportkit/exporters/html/renderers/layout/spacer.py +18 -0
- reportkit/exporters/html/renderers/media/__init__.py +3 -0
- reportkit/exporters/html/renderers/media/image.py +18 -0
- reportkit/exporters/html/renderers/pagination/__init__.py +4 -0
- reportkit/exporters/html/renderers/pagination/page_break.py +15 -0
- reportkit/exporters/html/renderers/pagination/page_number.py +15 -0
- reportkit/exporters/html/renderers/table/__init__.py +4 -0
- reportkit/exporters/html/renderers/table/cell.py +77 -0
- reportkit/exporters/html/renderers/table/table.py +69 -0
- reportkit/exporters/html/renderers/typography/__init__.py +5 -0
- reportkit/exporters/html/renderers/typography/heading.py +18 -0
- reportkit/exporters/html/renderers/typography/rich_text.py +28 -0
- reportkit/exporters/html/renderers/typography/text.py +16 -0
- reportkit/exporters/html/static/base.css +45 -0
- reportkit/exporters/html/static/information.css +83 -0
- reportkit/exporters/html/static/layout.css +81 -0
- reportkit/exporters/html/static/media.css +10 -0
- reportkit/exporters/html/static/pdf.css +96 -0
- reportkit/exporters/html/static/print.css +91 -0
- reportkit/exporters/html/static/table.css +222 -0
- reportkit/exporters/html/static/tokens.css +68 -0
- reportkit/exporters/html/static/typography.css +54 -0
- reportkit/exporters/html/styles.py +167 -0
- reportkit/exporters/html/templates/components/document/footer.html +3 -0
- reportkit/exporters/html/templates/components/document/header.html +3 -0
- reportkit/exporters/html/templates/components/document/page.html +8 -0
- reportkit/exporters/html/templates/components/document/report.html +3 -0
- reportkit/exporters/html/templates/components/information/info_grid.html +6 -0
- reportkit/exporters/html/templates/components/information/key_value.html +9 -0
- reportkit/exporters/html/templates/components/layout/box.html +3 -0
- reportkit/exporters/html/templates/components/layout/column.html +3 -0
- reportkit/exporters/html/templates/components/layout/divider.html +1 -0
- reportkit/exporters/html/templates/components/layout/grid.html +3 -0
- reportkit/exporters/html/templates/components/layout/row.html +3 -0
- reportkit/exporters/html/templates/components/layout/section.html +3 -0
- reportkit/exporters/html/templates/components/layout/spacer.html +1 -0
- reportkit/exporters/html/templates/components/media/image.html +8 -0
- reportkit/exporters/html/templates/components/pagination/page_break.html +1 -0
- reportkit/exporters/html/templates/components/pagination/page_number.html +4 -0
- reportkit/exporters/html/templates/components/table/cell.html +7 -0
- reportkit/exporters/html/templates/components/table/table.html +34 -0
- reportkit/exporters/html/templates/components/typography/heading.html +1 -0
- reportkit/exporters/html/templates/components/typography/rich_text.html +5 -0
- reportkit/exporters/html/templates/components/typography/text.html +1 -0
- reportkit/exporters/html/templates/document.html +17 -0
- reportkit/exporters/html/templates.py +30 -0
- reportkit/exporters/json.py +0 -0
- reportkit/exporters/pdf/__init__.py +8 -0
- reportkit/exporters/pdf/conversion.py +104 -0
- reportkit/exporters/pdf/dependencies.py +12 -0
- reportkit/exporters/pdf/exceptions.py +4 -0
- reportkit/exporters/pdf/exporter.py +16 -0
- reportkit/exporters/pdf/resources/__init__.py +4 -0
- reportkit/exporters/pdf/resources/fetcher.py +37 -0
- reportkit/exporters/pdf/resources/policy.py +60 -0
- reportkit/exporters/registry.py +50 -0
- reportkit/exporters/word/__init__.py +8 -0
- reportkit/exporters/word/document/__init__.py +4 -0
- reportkit/exporters/word/document/document.py +74 -0
- reportkit/exporters/word/document/engine.py +71 -0
- reportkit/exporters/word/exceptions.py +4 -0
- reportkit/exporters/word/exporter.py +32 -0
- reportkit/exporters/word/renderers/__init__.py +24 -0
- reportkit/exporters/word/renderers/base.py +56 -0
- reportkit/exporters/word/renderers/information.py +30 -0
- reportkit/exporters/word/renderers/layout/__init__.py +6 -0
- reportkit/exporters/word/renderers/layout/columns.py +82 -0
- reportkit/exporters/word/renderers/layout/page.py +38 -0
- reportkit/exporters/word/renderers/layout/spacing.py +33 -0
- reportkit/exporters/word/renderers/layout/stack.py +50 -0
- reportkit/exporters/word/renderers/media.py +49 -0
- reportkit/exporters/word/renderers/registry.py +22 -0
- reportkit/exporters/word/renderers/table/__init__.py +3 -0
- reportkit/exporters/word/renderers/table/cells.py +39 -0
- reportkit/exporters/word/renderers/table/renderer.py +95 -0
- reportkit/exporters/word/renderers/table/widths.py +12 -0
- reportkit/exporters/word/renderers/text.py +40 -0
- reportkit/exporters/word/styles/__init__.py +3 -0
- reportkit/exporters/word/styles/text.py +90 -0
- reportkit/exporters/word/styles/xml.py +147 -0
- reportkit/exporters/word/values.py +36 -0
- reportkit/formatting/__init__.py +0 -0
- reportkit/formatting/cell.py +12 -0
- reportkit/formatting/cells.py +41 -0
- reportkit/formatting/values.py +106 -0
- reportkit/rendering/__init__.py +0 -0
- reportkit/rendering/context.py +31 -0
- reportkit/rendering/table.py +56 -0
- reportkit/rendering/tracks.py +51 -0
- reportkit/rendering/visitor.py +55 -0
- reportkit/report.py +0 -0
- reportkit/styles/__init__.py +0 -0
- reportkit/styles/borders.py +6 -0
- reportkit/styles/style.py +117 -0
- reportkit/styles/table.py +37 -0
- reportkit/styles/theme.py +45 -0
- reportkit/styles/tokens.py +29 -0
- reportkit/validators/__init__.py +3 -0
- reportkit/validators/table/__init__.py +3 -0
- reportkit/validators/table/cell.py +38 -0
- reportkit/validators/table/column.py +62 -0
- reportkit/validators/table/row.py +37 -0
- reportkit/validators/table/table.py +63 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from .style import Style
|
|
2
|
+
from .tokens import token
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def table_styles(base=Style()):
|
|
6
|
+
"""
|
|
7
|
+
Build the table styles from the shared table tokens, on top of the table component style.
|
|
8
|
+
"""
|
|
9
|
+
divider, frame, ruled = f"1px solid {token('--rk-table-divider-color')}", f"1px solid {token('--rk-table-frame-color')}", token("--rk-table-ruled-color")
|
|
10
|
+
return {
|
|
11
|
+
"cell": base.merge(Style(color=token("--rk-table-cell-color"), border_bottom=divider)),
|
|
12
|
+
"bordered": Style(border_left=divider, border_right=divider, border_top=divider, border_bottom=divider),
|
|
13
|
+
"header": base.merge(Style(background_color=token("--rk-table-header-bg"), color=token("--rk-table-header-color"), font_weight=700, border_top=f"1px solid {token('--rk-table-header-accent')}", border_bottom=f"2px solid {token('--rk-table-header-rule')}")),
|
|
14
|
+
"ruled": Style(border_top=f"1px solid {ruled}", border_bottom=f"2px solid {ruled}"),
|
|
15
|
+
"frame": Style(border_left=frame, border_right=frame, border_top=frame, border_bottom=frame),
|
|
16
|
+
"stripe": Style(background_color=token("--rk-table-stripe-bg")),
|
|
17
|
+
"total": Style(background_color=token("--rk-table-total-bg"), font_weight=700, border_top=f"2px solid {token('--rk-table-divider-color')}"),
|
|
18
|
+
"ruled-total": Style(font_weight=700, color=token("--rk-table-header-color"), border_top=f"2px solid {ruled}", border_bottom="0px none #ffffff"),
|
|
19
|
+
"subtotal": Style(background_color=token("--rk-table-subtotal-bg"), font_weight=600),
|
|
20
|
+
"group-header": Style(background_color=token("--rk-table-group-bg"), font_weight=700),
|
|
21
|
+
"data": Style(),
|
|
22
|
+
"compact": Style(font_size=token("--rk-font-xs")),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_row_style(component, table_row, index, styles):
|
|
27
|
+
"""
|
|
28
|
+
Combine the table, striping, row kind and row styles from the shared table styles.
|
|
29
|
+
"""
|
|
30
|
+
table_style = component.table_style
|
|
31
|
+
style = styles["cell"].merge(styles["bordered"]) if table_style.bordered else styles["cell"]
|
|
32
|
+
if table_style.striped and table_row.kind == "data" and index % 2:
|
|
33
|
+
style = style.merge(styles["stripe"])
|
|
34
|
+
if table_style.compact:
|
|
35
|
+
style = style.merge(styles["compact"])
|
|
36
|
+
kind = "ruled-total" if table_style.ruled and table_row.kind == "total" else table_row.kind
|
|
37
|
+
return style.merge(styles[kind]).merge(table_row.style)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from .style import Style
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Theme:
|
|
8
|
+
"""
|
|
9
|
+
Manage named reusable styles for report components.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
name: str = "default"
|
|
13
|
+
styles: dict[str, Style] = field(default_factory=dict)
|
|
14
|
+
|
|
15
|
+
def register(self, name: str, style: Style):
|
|
16
|
+
"""
|
|
17
|
+
Register a named style.
|
|
18
|
+
"""
|
|
19
|
+
if not isinstance(name, str) or not name:
|
|
20
|
+
raise ValueError("Style name must be a non-empty string.")
|
|
21
|
+
|
|
22
|
+
if not isinstance(style, Style):
|
|
23
|
+
raise TypeError("Expected a Style instance.")
|
|
24
|
+
|
|
25
|
+
if name in self.styles:
|
|
26
|
+
raise ValueError(f"Style '{name}' is already registered.")
|
|
27
|
+
|
|
28
|
+
self.styles[name] = style
|
|
29
|
+
return self
|
|
30
|
+
|
|
31
|
+
def get(self, name: str) -> Style:
|
|
32
|
+
"""
|
|
33
|
+
Resolve a registered style.
|
|
34
|
+
"""
|
|
35
|
+
try:
|
|
36
|
+
return self.styles[name]
|
|
37
|
+
except KeyError:
|
|
38
|
+
raise ValueError(f"Unknown style: '{name}'.") from None
|
|
39
|
+
|
|
40
|
+
def resolve(self, name: str, override: Style | None = None) -> Style:
|
|
41
|
+
"""
|
|
42
|
+
Resolve a named style with optional overrides.
|
|
43
|
+
"""
|
|
44
|
+
style = self.get(name)
|
|
45
|
+
return style.merge(override) if override is not None else style
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from functools import cache
|
|
3
|
+
|
|
4
|
+
from ..exporters.html.templates import HtmlTemplateEngine
|
|
5
|
+
|
|
6
|
+
TOKEN_PATTERN = re.compile(r"(--rk-[\w-]+)\s*:\s*([^;]+);")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@cache
|
|
10
|
+
def load_tokens():
|
|
11
|
+
"""
|
|
12
|
+
Read the design tokens from tokens.css so every exporter reuses the HTML visual identity.
|
|
13
|
+
"""
|
|
14
|
+
raw = dict(TOKEN_PATTERN.findall((HtmlTemplateEngine().static_dir / "tokens.css").read_text(encoding="utf-8")))
|
|
15
|
+
return {name: resolve_token(value.strip(), raw) for name, value in raw.items()}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def resolve_token(value, raw):
|
|
19
|
+
"""
|
|
20
|
+
Resolve var(--name) references inside a token value.
|
|
21
|
+
"""
|
|
22
|
+
return re.sub(r"var\((--rk-[\w-]+)\)", lambda match: resolve_token(raw[match.group(1)].strip(), raw), value)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def token(name):
|
|
26
|
+
"""
|
|
27
|
+
Return one design token value.
|
|
28
|
+
"""
|
|
29
|
+
return load_tokens()[name]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from ...core.component import Component
|
|
2
|
+
from .column import ALIGNMENTS
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TableCellValidator:
|
|
6
|
+
"""
|
|
7
|
+
Validate TableCell definitions.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
@classmethod
|
|
11
|
+
def validate(cls, cell):
|
|
12
|
+
"""
|
|
13
|
+
Run every cell rule.
|
|
14
|
+
"""
|
|
15
|
+
cls.validate_content(cell)
|
|
16
|
+
cls.validate_presentation(cell)
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def validate_content(cell):
|
|
20
|
+
"""
|
|
21
|
+
A cell shows either a plain value or child components, never both.
|
|
22
|
+
"""
|
|
23
|
+
if not isinstance(cell.children, (list, tuple)) or not all(isinstance(child, Component) for child in cell.children):
|
|
24
|
+
raise TypeError("Table cell children must be Component instances.")
|
|
25
|
+
if cell.children and cell.value is not None:
|
|
26
|
+
raise ValueError("TableCell accepts either value or children, not both.")
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def validate_presentation(cell):
|
|
30
|
+
"""
|
|
31
|
+
Validate alignment and spans.
|
|
32
|
+
"""
|
|
33
|
+
if cell.align is not None and cell.align not in ALIGNMENTS:
|
|
34
|
+
raise ValueError("Invalid cell alignment.")
|
|
35
|
+
for name in ("colspan", "rowspan"):
|
|
36
|
+
span = getattr(cell, name)
|
|
37
|
+
if not isinstance(span, int) or isinstance(span, bool) or span < 1:
|
|
38
|
+
raise ValueError(f"Table cell {name} must be a positive integer.")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
ALIGNMENTS = {"start", "center", "end", "left", "right"}
|
|
4
|
+
VERTICAL_ALIGNMENTS = {"top", "middle", "bottom"}
|
|
5
|
+
FORMATS = {None, "text", "number", "currency", "percent", "date", "datetime"}
|
|
6
|
+
WIDTH = re.compile(r"^\d+(?:\.\d+)?(?:px|%|mm|cm|in|pt|em|rem)$")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TableColumnValidator:
|
|
10
|
+
"""
|
|
11
|
+
Validate TableColumn definitions.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def validate(cls, column):
|
|
16
|
+
"""
|
|
17
|
+
Run every column rule.
|
|
18
|
+
"""
|
|
19
|
+
cls.validate_identity(column)
|
|
20
|
+
cls.validate_presentation(column)
|
|
21
|
+
cls.validate_formatting(column)
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def validate_identity(column):
|
|
25
|
+
"""
|
|
26
|
+
Validate the key and the header texts.
|
|
27
|
+
"""
|
|
28
|
+
if not isinstance(column.key, str) or not column.key:
|
|
29
|
+
raise ValueError("Column key must be a non-empty string.")
|
|
30
|
+
if not isinstance(column.title, str):
|
|
31
|
+
raise TypeError("Column title must be a string.")
|
|
32
|
+
if column.subtitle is not None and not isinstance(column.subtitle, str):
|
|
33
|
+
raise TypeError("Column subtitle must be a string.")
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def validate_presentation(column):
|
|
37
|
+
"""
|
|
38
|
+
Validate alignment, width and wrapping.
|
|
39
|
+
"""
|
|
40
|
+
if column.align not in ALIGNMENTS:
|
|
41
|
+
raise ValueError("Invalid column alignment.")
|
|
42
|
+
if column.header_align is not None and column.header_align not in ALIGNMENTS:
|
|
43
|
+
raise ValueError("Invalid column header alignment.")
|
|
44
|
+
if column.vertical_align not in VERTICAL_ALIGNMENTS:
|
|
45
|
+
raise ValueError("Invalid column vertical alignment.")
|
|
46
|
+
if column.width is not None and (not isinstance(column.width, str) or not WIDTH.fullmatch(column.width)):
|
|
47
|
+
raise ValueError("Column width must be a CSS length such as '20%' or '30mm'.")
|
|
48
|
+
if not isinstance(column.nowrap, bool):
|
|
49
|
+
raise TypeError("Column nowrap must be a boolean.")
|
|
50
|
+
|
|
51
|
+
@staticmethod
|
|
52
|
+
def validate_formatting(column):
|
|
53
|
+
"""
|
|
54
|
+
Validate value formatting options.
|
|
55
|
+
"""
|
|
56
|
+
if column.format not in FORMATS:
|
|
57
|
+
raise ValueError("Invalid column format.")
|
|
58
|
+
if not isinstance(column.decimals, int) or isinstance(column.decimals, bool) or not 0 <= column.decimals <= 12:
|
|
59
|
+
raise ValueError("Decimals must be an integer between 0 and 12.")
|
|
60
|
+
for name in ("currency", "date_format"):
|
|
61
|
+
if getattr(column, name) is not None and not isinstance(getattr(column, name), str):
|
|
62
|
+
raise TypeError(f"Column {name} must be a string.")
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
|
|
3
|
+
ROW_KINDS = {"data", "group-header", "subtotal", "total"}
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TableRowValidator:
|
|
7
|
+
"""
|
|
8
|
+
Validate TableRow definitions.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def validate(cls, row):
|
|
13
|
+
"""
|
|
14
|
+
Run every row rule.
|
|
15
|
+
"""
|
|
16
|
+
cls.validate_kind(row)
|
|
17
|
+
cls.validate_content(row)
|
|
18
|
+
|
|
19
|
+
@staticmethod
|
|
20
|
+
def validate_kind(row):
|
|
21
|
+
"""
|
|
22
|
+
Validate the row kind.
|
|
23
|
+
"""
|
|
24
|
+
if row.kind not in ROW_KINDS:
|
|
25
|
+
raise ValueError("Unsupported table row kind.")
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def validate_content(row):
|
|
29
|
+
"""
|
|
30
|
+
Validate values and explicit cells.
|
|
31
|
+
"""
|
|
32
|
+
from ...components.table.cell import TableCell # Deferred: the component modules import this validator.
|
|
33
|
+
|
|
34
|
+
if not isinstance(row.values, Mapping):
|
|
35
|
+
raise TypeError("Table row values must be a mapping.")
|
|
36
|
+
if row.cells is not None and not all(isinstance(cell, TableCell) for cell in row.cells):
|
|
37
|
+
raise TypeError("Row cells must contain TableCell instances.")
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TableValidator:
|
|
5
|
+
"""
|
|
6
|
+
Validate Table configuration; rows stay lazy and are checked as they are rendered.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
@classmethod
|
|
10
|
+
def validate(cls, table):
|
|
11
|
+
"""
|
|
12
|
+
Run every table rule that does not consume the rows.
|
|
13
|
+
"""
|
|
14
|
+
cls.validate_columns(table.columns)
|
|
15
|
+
cls.validate_rows(table)
|
|
16
|
+
cls.validate_style(table.table_style)
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def validate_columns(columns):
|
|
20
|
+
"""
|
|
21
|
+
Validate column types and unique keys.
|
|
22
|
+
"""
|
|
23
|
+
from ...components.table.column import TableColumn # Deferred: avoids a components/validators cycle.
|
|
24
|
+
|
|
25
|
+
if not columns:
|
|
26
|
+
raise ValueError("Table must contain at least one column.")
|
|
27
|
+
if not all(isinstance(column, TableColumn) for column in columns):
|
|
28
|
+
raise TypeError("All columns must be TableColumn instances.")
|
|
29
|
+
keys = [column.key for column in columns]
|
|
30
|
+
if len(keys) != len(set(keys)):
|
|
31
|
+
raise ValueError("Table column keys must be unique.")
|
|
32
|
+
|
|
33
|
+
@classmethod
|
|
34
|
+
def validate_rows(cls, table):
|
|
35
|
+
"""
|
|
36
|
+
Validate the rows container and the footer.
|
|
37
|
+
"""
|
|
38
|
+
if not isinstance(table.rows, Iterable):
|
|
39
|
+
raise TypeError("Table rows must be iterable.")
|
|
40
|
+
if table.footer is not None:
|
|
41
|
+
cls.validate_row(table.footer)
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def validate_row(row):
|
|
45
|
+
"""
|
|
46
|
+
Validate one row as it is consumed.
|
|
47
|
+
"""
|
|
48
|
+
from ...components.table.row import TableRow # Deferred: avoids a components/validators cycle.
|
|
49
|
+
|
|
50
|
+
if not isinstance(row, TableRow):
|
|
51
|
+
raise TypeError("Table rows must be TableRow instances.")
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def validate_style(table_style):
|
|
55
|
+
"""
|
|
56
|
+
Validate the table presentation settings.
|
|
57
|
+
"""
|
|
58
|
+
from ...components.table.style import TableStyle # Deferred: avoids a components/validators cycle.
|
|
59
|
+
|
|
60
|
+
if not isinstance(table_style, TableStyle):
|
|
61
|
+
raise TypeError("Table style must be a TableStyle instance.")
|
|
62
|
+
if table_style.layout not in {"fixed", "auto"}:
|
|
63
|
+
raise ValueError("Table layout must be 'fixed' or 'auto'.")
|