Skip to content

renderer

renderer

Abstract base class for implementing report renderers.

ReportType

Bases: Enum

Possible report types.

Source code in cssfinder/reports/renderer.py
class ReportType(Enum):
    """Possible report types."""

    HTML = "html"
    PDF = "pdf"
    ARCHIVE = "zip"
    TXT = "txt"
    JSON = "json"

    def get_file_name(self) -> str:
        """Return default file name for specific type of report."""
        return f"report.{self.value}"

get_file_name

get_file_name() -> str

Return default file name for specific type of report.

Source code in cssfinder/reports/renderer.py
def get_file_name(self) -> str:
    """Return default file name for specific type of report."""
    return f"report.{self.value}"

Renderer

Bases: ABC

Base class for creating report renderers.

Source code in cssfinder/reports/renderer.py
class Renderer(ABC):
    """Base class for creating report renderers."""

    def __init__(self, props: SlopeProperties, plots: list[Plot], task: Task) -> None:
        self.ctx = Ctx(props, plots, task)

    @abstractmethod
    def render(self) -> Report:
        """Render report.

        Returns
        -------
        RenderedReport
            Report handle providing interface for saving report.

        """

render abstractmethod

render() -> Report

Render report.

Returns:

Type Description
RenderedReport

Report handle providing interface for saving report.

Source code in cssfinder/reports/renderer.py
@abstractmethod
def render(self) -> Report:
    """Render report.

    Returns
    -------
    RenderedReport
        Report handle providing interface for saving report.

    """

Ctx dataclass

Report template rendering context.

Source code in cssfinder/reports/renderer.py
@dataclass
class Ctx:
    """Report template rendering context."""

    props: SlopeProperties
    plots: list[Plot]
    task: Task

    @property
    def title(self) -> str:
        """Document title."""
        return f"Report {self.task.project_directory.name} / {self.task.task_name}"

    @property
    def meta(self) -> OrderedDict:
        """Return project metadata."""
        return OrderedDict(
            {
                "Project name": self.task.project.meta.name,
                "Task name": self.task.task_name,
                "Author": self.task.project.meta.author,
                "Email": self.task.project.meta.email,
                "Description": self.task.project.meta.description,
                "Version": self.task.project.meta.version,
            },
        )

    @property
    def math_props(self) -> OrderedDict:
        """Return mathematical properties."""
        return OrderedDict(
            {
                "Correction count": f"{self.props.correction_count}",
                "Hilbert-Schmidt distance": f"{self.props.optimum:.3f}",
                "Sample correlation coefficient": f"{self.props.r_value:.3f}",
                "Offset (optimum)": f"{self.props.optimum:.3f}",
                "Trend (aa1)": f"{self.props.aa1:.3f}",
                "Exp Offset (bb1)": f"{self.props.bb1:.3f}",
            },
        )

title property

title: str

Document title.

meta property

meta: OrderedDict

Return project metadata.

math_props property

math_props: OrderedDict

Return mathematical properties.

Report dataclass

Container for rendered report.

Source code in cssfinder/reports/renderer.py
@dataclass
class Report:
    """Container for rendered report."""

    content: bytes
    report_type: ReportType
    default_dest: Optional[Path] = None

    def get_default_dest(self) -> Path:
        """Return path to default destination of report file."""
        if self.default_dest is not None:
            return self.default_dest

        return Path.cwd() / self.report_type.get_file_name()

    def save_default(self) -> None:
        """Save file to default destination."""
        if self.default_dest is None:
            raise DefaultDestinationNotSpecifiedError(self.report_type)
        self.save_to(self.default_dest)

    def save_to(self, dest: Path) -> None:
        """Save report to a file.

        Parameters
        ----------
        dest : Path
            Path to destination file.

        """
        dest.write_bytes(self.content)

get_default_dest

get_default_dest() -> Path

Return path to default destination of report file.

Source code in cssfinder/reports/renderer.py
def get_default_dest(self) -> Path:
    """Return path to default destination of report file."""
    if self.default_dest is not None:
        return self.default_dest

    return Path.cwd() / self.report_type.get_file_name()

save_default

save_default() -> None

Save file to default destination.

Source code in cssfinder/reports/renderer.py
def save_default(self) -> None:
    """Save file to default destination."""
    if self.default_dest is None:
        raise DefaultDestinationNotSpecifiedError(self.report_type)
    self.save_to(self.default_dest)

save_to

save_to(dest: Path) -> None

Save report to a file.

Parameters:

Name Type Description Default
dest Path

Path to destination file.

required
Source code in cssfinder/reports/renderer.py
def save_to(self, dest: Path) -> None:
    """Save report to a file.

    Parameters
    ----------
    dest : Path
        Path to destination file.

    """
    dest.write_bytes(self.content)

DefaultDestinationNotSpecifiedError

Bases: Exception

Raised when save_default() was called on Report object with no default_dest.

Source code in cssfinder/reports/renderer.py
class DefaultDestinationNotSpecifiedError(Exception):
    """Raised when save_default() was called on Report object with no default_dest."""

    def __init__(self, report_type: ReportType) -> None:
        super().__init__(
            f"Default destination for report object of type {report_type.name!r} "
            "was not specified.",
        )