import warnings
from typing import List, Optional, Tuple, Union
from reportlab.platypus import Flowable, Paragraph, Table
from input.properties import columns, subtables
from pyxml2pdf.styles.table_styles import TableStyle
from pyxml2pdf.tables.tables import EventTable
[docs]class TableBuilder:
def __init__(self):
self._table_style = TableStyle()
self._stylesheet = self._table_style.custom_styles["stylesheet"]
self._subtables = self.create_subtables()
[docs] def create_subtables(self):
"""Create subtables for all different kinds of events
:return list[EventTable]: a list of all subtables
"""
subtables_list = []
for subtable in subtables:
subtable_table = EventTable(
subtable["label"], subtable["content"][0], subtable["content"][1]
)
subtable_table.extend(self.make_header(subtable["label"]))
subtables_list.append(subtable_table)
return subtables_list
[docs] def collect_subtables(self) -> List[Table]:
"""Collect all subtables at once
:return: the subtables
"""
return [element for subtable in self._subtables for element in subtable.events]
[docs] def distribute_event(self, event):
"""Distribute an event to the subtables according to the related categories
:param Core.events.Event event: event to distribute
"""
distribution_failed = True
set_of_cats = set(event.categories)
for subtable in self._subtables:
if set_of_cats.intersection(
subtable.activities
) and set_of_cats.intersection(subtable.locations):
subtable.append(event.get_table_row(subtable.title))
distribution_failed = False
if distribution_failed:
warnings.warn(
event.responsible
+ "'s event on "
+ event.date
+ " would not be printed, because it does not contain a valid"
" combination of locations and activities. Either add a valid location"
" or add a valid activity or both.",
RuntimeWarning,
)
[docs] def create_fixedwidth_table(
self,
cells: List[List[Flowable]],
widths: Optional[Union[float, List[float]]] = None,
style: Optional[List[Tuple[Union[str, Tuple[int]]]]] = None,
) -> Table:
"""Create a table with specified column widths
Create a table from specified cells with fixed column widths and a specific
style.
:param cells: cells wrapped by a list representing the columns wrapped by a
list representing the lines
:param widths: the column widths
:param style: desired table's style
:returns: table containing specified cells in fixed width, styled columns
"""
if widths is None:
widths = self._table_style.column_widths
if style is None:
style = self._table_style.custom_styles["normal"]
table = Table(cells, colWidths=widths)
table.setStyle(style)
return table