# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH
"""Postprocess result collection models."""
from __future__ import annotations
from collections.abc import Iterator
from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .postprocess_record import PostprocessRecord
[docs]
@dataclass(slots=True, frozen=True, repr=False)
class PostprocessCollection:
"""Store postprocess records in project file order."""
records: tuple[PostprocessRecord, ...]
def __repr__(self) -> str:
"""Return indexed postprocess titles and availability states."""
from .internal.export.preview import format_table_preview
values: dict[object, object] = {
"index": tuple(range(len(self.records))),
"title": tuple(
"<untitled>" if record.title is None else record.title
for record in self.records
),
"status": tuple(
(
"available"
if record.available
else (
"unavailable"
if record.failure_message is None
else f"unavailable: {record.failure_message}"
)
)
for record in self.records
),
}
return f"PostprocessCollection(\n{format_table_preview(values, include_row_index=False, quote_strings=False)}\n)"
[docs]
def __len__(self) -> int:
"""Return the number of postprocess records.
Returns
-------
int
Number of postprocess records.
"""
return len(self.records)
[docs]
def __iter__(self) -> Iterator[PostprocessRecord]:
"""Iterate over records in project file order.
Returns
-------
Iterator[PostprocessRecord]
Postprocess records in project file order.
"""
return iter(self.records)
[docs]
def __getitem__(self, index: int) -> PostprocessRecord:
"""Return one postprocess record by position.
Parameters
----------
index : int
Record position. Position `0` selects the first record.
Returns
-------
PostprocessRecord
Selected postprocess record.
"""
return self.records[index]
[docs]
def titles(self) -> tuple[str, ...]:
"""Return non-empty postprocess titles in project file order.
Returns
-------
tuple[str, ...]
Titles for records that define a title.
"""
return tuple(
record.title for record in self.records if record.title is not None
)