Skip to content

Report

sereto.models.report

Report

Bases: SeretoBaseModel

Source code in sereto/models/report.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Report(SeretoBaseModel):
    config: Config

    @staticmethod
    @validate_call
    def get_path(dir_subtree: Path = Path("/")) -> Path:
        for dir in [Path.cwd()] + list(Path.cwd().parents):
            if dir.is_relative_to(dir_subtree):
                if Report.is_report_dir(dir):
                    return dir
            else:
                break

        raise SeretoPathError("not inside report's (sub)directory")

    @staticmethod
    @validate_call
    def get_config_path(dir_subtree: Path = Path("/")) -> Path:
        return Report.get_path(dir_subtree=dir_subtree) / "config.json"

    @staticmethod
    @validate_call
    def is_report_dir(path: Path) -> bool:
        """Check if the provided path is a root directory of a report.

        A report directory contains at least `.sereto` and `config.json` files.

        Args:
            path: The path to examine.

        Returns:
            True if the path is a report directory, False otherwise.
        """
        return (path / ".sereto").is_file() and (path / "config.json").is_file()

    @validate_call
    def load_runtime_vars(self, settings: Settings) -> None:
        """Get the config enriched with additional parameters like paths and findings."""
        report_path = self.get_path(dir_subtree=settings.reports_path)

        for cfg in [self.config] + self.config.updates:
            for target in cfg.targets:
                target.path = report_path / target.uname

    @validate_call
    def select_target(
        self,
        settings: Settings,
        version: ReportVersion | None = None,
        selector: int | str | None = None,
    ) -> Target:
        if version is None:
            version = self.config.last_version()

        cfg = self.config.at_version(version)

        # only single target present
        if selector is None:
            if len(cfg.targets) != 1:
                raise SeretoValueError(
                    f"cannot select target; no selector provided and there are {len(cfg.targets)} targets present"
                )
            return cfg.targets[0]

        # by index
        if isinstance(selector, int) or selector.isnumeric():
            ix = selector - 1 if isinstance(selector, int) else int(selector) - 1
            if not (0 <= ix <= len(cfg.targets) - 1):
                raise SeretoValueError("target index out of range")
            return cfg.targets[ix]

        # by category
        if selector in settings.categories:
            targets = [t for t in cfg.targets if t.category == selector]
            match len(targets):
                case 0:
                    raise SeretoValueError(f"category {selector!r} does not contain any target")
                case 1:
                    return targets[0]
                case _:
                    raise SeretoValueError(f"category {selector!r} contains multiple targets, use uname when querying")

        # by uname
        targets = [t for t in cfg.targets if t.uname == selector]
        if len(targets) != 1:
            raise SeretoValueError(f"cannot find target with uname {selector!r}")
        return targets[0]

is_report_dir(path) staticmethod

Check if the provided path is a root directory of a report.

A report directory contains at least .sereto and config.json files.

Parameters:

Name Type Description Default
path Path

The path to examine.

required

Returns:

Type Description
bool

True if the path is a report directory, False otherwise.

Source code in sereto/models/report.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@staticmethod
@validate_call
def is_report_dir(path: Path) -> bool:
    """Check if the provided path is a root directory of a report.

    A report directory contains at least `.sereto` and `config.json` files.

    Args:
        path: The path to examine.

    Returns:
        True if the path is a report directory, False otherwise.
    """
    return (path / ".sereto").is_file() and (path / "config.json").is_file()

load_runtime_vars(settings)

Get the config enriched with additional parameters like paths and findings.

Source code in sereto/models/report.py
48
49
50
51
52
53
54
55
@validate_call
def load_runtime_vars(self, settings: Settings) -> None:
    """Get the config enriched with additional parameters like paths and findings."""
    report_path = self.get_path(dir_subtree=settings.reports_path)

    for cfg in [self.config] + self.config.updates:
        for target in cfg.targets:
            target.path = report_path / target.uname