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,15 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ...core.container import Container
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Column(Container):
|
|
8
|
+
"""
|
|
9
|
+
Arrange child components vertically.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
align: str | None = None
|
|
13
|
+
justify: str | None = None
|
|
14
|
+
gap: str | None = None
|
|
15
|
+
direction: str | None = None
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ...core.container import Container
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Row(Container):
|
|
8
|
+
"""
|
|
9
|
+
Arrange child components horizontally.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
justify: str | None = None
|
|
13
|
+
align: str | None = None
|
|
14
|
+
gap: str | None = None
|
|
15
|
+
wrap: bool = False
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ...core.component import Component
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(kw_only=True)
|
|
7
|
+
class Image(Component):
|
|
8
|
+
"""
|
|
9
|
+
Represent an image in a report.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
src: str
|
|
13
|
+
alt: str = ""
|
|
14
|
+
width: str | None = None
|
|
15
|
+
height: str | None = None
|
|
16
|
+
fit: str = "contain"
|
|
17
|
+
|
|
18
|
+
def __post_init__(self):
|
|
19
|
+
"""
|
|
20
|
+
Validate image properties.
|
|
21
|
+
"""
|
|
22
|
+
if not isinstance(self.src, str) or not self.src.strip():
|
|
23
|
+
raise ValueError("Image source must be a non-empty string.")
|
|
24
|
+
if self.fit not in {"contain", "cover", "fill", "none", "scale-down"}:
|
|
25
|
+
raise ValueError("Unsupported image fit.")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ...core.component import Component
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(kw_only=True)
|
|
7
|
+
class PageNumber(Component):
|
|
8
|
+
"""
|
|
9
|
+
Represent a page numbering placeholder.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
format: str = "Page {page} of {pages}"
|
|
13
|
+
|
|
14
|
+
def __post_init__(self):
|
|
15
|
+
"""
|
|
16
|
+
Validate page number format.
|
|
17
|
+
"""
|
|
18
|
+
if not isinstance(self.format, str):
|
|
19
|
+
raise TypeError("Page number format must be a string.")
|
|
20
|
+
if not self.format:
|
|
21
|
+
raise ValueError("Page number format cannot be empty.")
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from ...core.component import Component
|
|
5
|
+
from ...styles.style import Style
|
|
6
|
+
from ...validators.table.cell import TableCellValidator
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True, kw_only=True)
|
|
10
|
+
class TableCell:
|
|
11
|
+
"""
|
|
12
|
+
Define one prepared cell: a plain value or a stack of child components; align=None inherits the
|
|
13
|
+
column.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
value: Any = None
|
|
17
|
+
children: list[Component] = field(default_factory=list)
|
|
18
|
+
align: str | None = None
|
|
19
|
+
colspan: int = 1
|
|
20
|
+
rowspan: int = 1
|
|
21
|
+
style: Style = field(default_factory=Style)
|
|
22
|
+
|
|
23
|
+
def __post_init__(self):
|
|
24
|
+
"""
|
|
25
|
+
Validate the cell definition.
|
|
26
|
+
"""
|
|
27
|
+
TableCellValidator.validate(self)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ...validators.table.column import TableColumnValidator
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True, kw_only=True)
|
|
7
|
+
class TableColumn:
|
|
8
|
+
"""
|
|
9
|
+
Define a table column and its presentation options.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
key: str
|
|
13
|
+
title: str
|
|
14
|
+
subtitle: str | None = None
|
|
15
|
+
width: str | None = None
|
|
16
|
+
align: str = "start"
|
|
17
|
+
header_align: str | None = None
|
|
18
|
+
vertical_align: str = "middle"
|
|
19
|
+
nowrap: bool = False
|
|
20
|
+
format: str | None = None
|
|
21
|
+
decimals: int = 2
|
|
22
|
+
currency: str | None = None
|
|
23
|
+
date_format: str | None = None
|
|
24
|
+
|
|
25
|
+
def __post_init__(self):
|
|
26
|
+
"""
|
|
27
|
+
Validate the column definition.
|
|
28
|
+
"""
|
|
29
|
+
TableColumnValidator.validate(self)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from ...styles.style import Style
|
|
6
|
+
from ...validators.table.row import TableRowValidator
|
|
7
|
+
from .cell import TableCell
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, kw_only=True)
|
|
11
|
+
class TableRow:
|
|
12
|
+
"""
|
|
13
|
+
Define one prepared row; values may hold plain values or TableCell instances.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
values: Mapping[str, Any] = field(default_factory=dict)
|
|
17
|
+
kind: str = "data"
|
|
18
|
+
cells: list[TableCell] | None = None
|
|
19
|
+
style: Style = field(default_factory=Style)
|
|
20
|
+
|
|
21
|
+
def __post_init__(self):
|
|
22
|
+
"""
|
|
23
|
+
Validate the row definition.
|
|
24
|
+
"""
|
|
25
|
+
TableRowValidator.validate(self)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(frozen=True, kw_only=True)
|
|
5
|
+
class TableStyle:
|
|
6
|
+
"""
|
|
7
|
+
Define table presentation and print settings.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
bordered: bool = True
|
|
11
|
+
striped: bool = False
|
|
12
|
+
hover: bool = False
|
|
13
|
+
compact: bool = False
|
|
14
|
+
ruled: bool = False
|
|
15
|
+
layout: str = "fixed"
|
|
16
|
+
show_header: bool = True
|
|
17
|
+
repeat_header: bool = True
|
|
18
|
+
avoid_row_break: bool = True
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
|
|
4
|
+
from ...core.component import Component
|
|
5
|
+
from ...validators.table.table import TableValidator
|
|
6
|
+
from .column import TableColumn
|
|
7
|
+
from .row import TableRow
|
|
8
|
+
from .style import TableStyle
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(kw_only=True)
|
|
12
|
+
class Table(Component):
|
|
13
|
+
"""
|
|
14
|
+
Define a table with externally prepared rows; rows may be a lazy iterable.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
columns: list[TableColumn] = field(default_factory=list)
|
|
18
|
+
rows: Iterable[TableRow] = field(default_factory=list)
|
|
19
|
+
header: str | None = None
|
|
20
|
+
footer: TableRow | None = None
|
|
21
|
+
table_style: TableStyle = field(default_factory=TableStyle)
|
|
22
|
+
|
|
23
|
+
def __post_init__(self):
|
|
24
|
+
"""
|
|
25
|
+
Validate the table without consuming its rows.
|
|
26
|
+
"""
|
|
27
|
+
TableValidator.validate(self)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from ...core.component import Component
|
|
4
|
+
from .text_span import TextSpan
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(kw_only=True)
|
|
8
|
+
class RichText(Component):
|
|
9
|
+
"""
|
|
10
|
+
Represent text containing multiple styled fragments.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
spans: list[TextSpan] = field(default_factory=list)
|
|
14
|
+
align: str = "start"
|
|
15
|
+
|
|
16
|
+
def __post_init__(self):
|
|
17
|
+
"""
|
|
18
|
+
Validate rich text configuration.
|
|
19
|
+
"""
|
|
20
|
+
if not all(isinstance(span, TextSpan) for span in self.spans):
|
|
21
|
+
raise TypeError("RichText spans must contain TextSpan instances.")
|
|
22
|
+
if self.align not in {"start", "center", "end", "left", "right"}:
|
|
23
|
+
raise ValueError("Unsupported text alignment.")
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from ...styles.style import Style
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True, kw_only=True)
|
|
7
|
+
class TextSpan:
|
|
8
|
+
"""
|
|
9
|
+
Represent one styled text fragment.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
value: str
|
|
13
|
+
style: Style = field(default_factory=Style)
|
|
14
|
+
language: str | None = None
|
|
15
|
+
direction: str | None = None
|
|
16
|
+
|
|
17
|
+
def __post_init__(self):
|
|
18
|
+
"""
|
|
19
|
+
Validate text fragment properties.
|
|
20
|
+
"""
|
|
21
|
+
if not isinstance(self.value, str):
|
|
22
|
+
raise TypeError("TextSpan value must be a string.")
|
|
23
|
+
if self.direction not in {None, "ltr", "rtl", "auto"}:
|
|
24
|
+
raise ValueError("Unsupported text direction.")
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from .component import Component
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Container(Component):
|
|
8
|
+
"""
|
|
9
|
+
Base component for managing nested report components.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
children: list[Component] = field(default_factory=list)
|
|
13
|
+
|
|
14
|
+
def __post_init__(self):
|
|
15
|
+
"""
|
|
16
|
+
Validate and initialize child components.
|
|
17
|
+
"""
|
|
18
|
+
children = self.children
|
|
19
|
+
self.children = []
|
|
20
|
+
self.extend(children)
|
|
21
|
+
|
|
22
|
+
def add(self, component: Component):
|
|
23
|
+
"""
|
|
24
|
+
Add a validated child component.
|
|
25
|
+
"""
|
|
26
|
+
self._validate_child(component)
|
|
27
|
+
self.children.append(component)
|
|
28
|
+
return self
|
|
29
|
+
|
|
30
|
+
def extend(self, components: list[Component]):
|
|
31
|
+
"""
|
|
32
|
+
Add multiple validated child components.
|
|
33
|
+
"""
|
|
34
|
+
components = list(components)
|
|
35
|
+
for component in components:
|
|
36
|
+
self._validate_child(component)
|
|
37
|
+
self.children.extend(components)
|
|
38
|
+
return self
|
|
39
|
+
|
|
40
|
+
def _validate_child(self, component: Component):
|
|
41
|
+
"""
|
|
42
|
+
Validate child type and prevent circular references.
|
|
43
|
+
"""
|
|
44
|
+
if not isinstance(component, Component):
|
|
45
|
+
raise TypeError("Child must be a Component.")
|
|
46
|
+
if component is self:
|
|
47
|
+
raise ValueError("A container cannot contain itself.")
|
|
48
|
+
if isinstance(component, Container) and component._contains(self):
|
|
49
|
+
raise ValueError("Circular component references are not allowed.")
|
|
50
|
+
|
|
51
|
+
def _contains(self, target: Component):
|
|
52
|
+
"""
|
|
53
|
+
Check whether a component exists in the descendant tree.
|
|
54
|
+
"""
|
|
55
|
+
visited = set()
|
|
56
|
+
stack = list(self.children)
|
|
57
|
+
while stack:
|
|
58
|
+
component = stack.pop()
|
|
59
|
+
if component is target:
|
|
60
|
+
return True
|
|
61
|
+
if id(component) in visited:
|
|
62
|
+
continue
|
|
63
|
+
visited.add(id(component))
|
|
64
|
+
if isinstance(component, Container):
|
|
65
|
+
stack.extend(component.children)
|
|
66
|
+
return False
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from dataclasses import fields
|
|
3
|
+
|
|
4
|
+
from ..styles.style import Style
|
|
5
|
+
from .component import Component
|
|
6
|
+
from .container import Container
|
|
7
|
+
from .registry import ComponentRegistry
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ComponentFactory:
|
|
11
|
+
"""
|
|
12
|
+
Create registered report components from dictionaries.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def create(cls, data: Mapping) -> Component:
|
|
17
|
+
"""
|
|
18
|
+
Create a component from its definition.
|
|
19
|
+
"""
|
|
20
|
+
if not isinstance(data, Mapping):
|
|
21
|
+
raise TypeError("Component definition must be a mapping.")
|
|
22
|
+
|
|
23
|
+
component_type = data.get("type")
|
|
24
|
+
if not isinstance(component_type, str) or not component_type:
|
|
25
|
+
raise ValueError("Component type must be a non-empty string.")
|
|
26
|
+
|
|
27
|
+
component_class = ComponentRegistry.get(component_type)
|
|
28
|
+
properties = {key: value for key, value in data.items() if key != "type"}
|
|
29
|
+
cls.validate_properties(component_class, properties)
|
|
30
|
+
|
|
31
|
+
if "style" in properties and not isinstance(properties["style"], Style):
|
|
32
|
+
properties["style"] = Style.from_dict(properties["style"])
|
|
33
|
+
|
|
34
|
+
if issubclass(component_class, Container):
|
|
35
|
+
children = properties.get("children", [])
|
|
36
|
+
if not isinstance(children, list):
|
|
37
|
+
raise TypeError("Component children must be a list.")
|
|
38
|
+
properties["children"] = [cls.create(child) for child in children]
|
|
39
|
+
elif "children" in properties:
|
|
40
|
+
raise ValueError(f"Component '{component_type}' does not support children.")
|
|
41
|
+
|
|
42
|
+
return component_class(**properties)
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def validate_properties(component_class: type[Component], properties: dict):
|
|
46
|
+
"""
|
|
47
|
+
Validate component properties against its dataclass fields.
|
|
48
|
+
"""
|
|
49
|
+
allowed = {field.name for field in fields(component_class)}
|
|
50
|
+
unknown = set(properties) - allowed
|
|
51
|
+
|
|
52
|
+
if unknown:
|
|
53
|
+
raise ValueError( f"Unknown properties for '{component_class.__name__}': {', '.join(sorted(unknown))}")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from ..core.component import Component
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ComponentRegistry:
|
|
5
|
+
"""
|
|
6
|
+
Register and resolve report component types.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
_components: dict[str, type[Component]] = {}
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def register(cls, name: str, component: type[Component]):
|
|
13
|
+
"""
|
|
14
|
+
Register a component type.
|
|
15
|
+
"""
|
|
16
|
+
if not isinstance(name, str) or not name:
|
|
17
|
+
raise ValueError("Component name must be a non-empty string.")
|
|
18
|
+
|
|
19
|
+
if not isinstance(component, type) or not issubclass(component, Component):
|
|
20
|
+
raise TypeError("Registered component must inherit from Component.")
|
|
21
|
+
|
|
22
|
+
if name in cls._components:
|
|
23
|
+
raise ValueError(f"Component '{name}' is already registered.")
|
|
24
|
+
|
|
25
|
+
cls._components[name] = component
|
|
26
|
+
return component
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def get(cls, name: str) -> type[Component]:
|
|
30
|
+
"""
|
|
31
|
+
Resolve a registered component type.
|
|
32
|
+
"""
|
|
33
|
+
try:
|
|
34
|
+
return cls._components[name]
|
|
35
|
+
except KeyError:
|
|
36
|
+
raise ValueError(f"Unknown component type: '{name}'.") from None
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def unregister(cls, name: str):
|
|
40
|
+
"""
|
|
41
|
+
Remove a registered component type.
|
|
42
|
+
"""
|
|
43
|
+
cls._components.pop(name, None)
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def all(cls) -> dict[str, type[Component]]:
|
|
47
|
+
"""
|
|
48
|
+
Return all registered component types.
|
|
49
|
+
"""
|
|
50
|
+
return cls._components.copy()
|