Skip to content

Target

sereto.target

Target dataclass

Source code in sereto/target.py
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
@dataclass
class Target:
    data: TargetModel
    findings: Findings
    path: DirectoryPath
    version: ProjectVersion

    @classmethod
    @validate_call
    def load(cls, data: TargetModel, path: DirectoryPath, version: ProjectVersion, templates: DirectoryPath) -> Self:
        return cls(
            data=data,
            findings=Findings.load_from(target_dir=path, target_locators=data.locators, templates=templates),
            path=path,
            version=version,
        )

    @classmethod
    @validate_call
    def new(
        cls, data: TargetModel, project_path: DirectoryPath, templates: DirectoryPath, version: ProjectVersion
    ) -> Self:
        target_path = project_path / (data.uname + version.path_suffix)

        logger.info("Creating target directory: '{}'", target_path)
        target_path.mkdir()

        category_templates = templates / "categories" / data.category

        if (category_templates / "skel").is_dir():
            logger.info("Populating new target directory from: '{}'", category_templates / "skel")
            copy_skel(templates=category_templates, dst=target_path)
        else:
            logger.warning("No 'skel' directory found: '{}'", category_templates)

        return cls.load(data=data, path=target_path, version=version, templates=templates)

    @validate_call
    def to_model(self) -> TargetModel:
        return self.data

    @property
    def uname(self) -> str:
        """Unique name for the target instance.

        Returns:
            The unique name of the target.
        """
        return self.data.uname + self.version.path_suffix

    @validate_call
    def filter_locators(self, type: str | Iterable[str]) -> list[LocatorModel]:
        """Filter locators by type.

        Args:
            type: The type of locators to filter by. Can be a single type or an iterable of types.

        Returns:
            A list of locators of the specified type.
        """
        type = [type] if isinstance(type, str) else list(type)
        return [loc for loc in self.data.locators if loc.type in type]

uname property

Unique name for the target instance.

Returns:

Type Description
str

The unique name of the target.

filter_locators(type)

Filter locators by type.

Parameters:

Name Type Description Default
type str | Iterable[str]

The type of locators to filter by. Can be a single type or an iterable of types.

required

Returns:

Type Description
list[LocatorModel]

A list of locators of the specified type.

Source code in sereto/target.py
65
66
67
68
69
70
71
72
73
74
75
76
@validate_call
def filter_locators(self, type: str | Iterable[str]) -> list[LocatorModel]:
    """Filter locators by type.

    Args:
        type: The type of locators to filter by. Can be a single type or an iterable of types.

    Returns:
        A list of locators of the specified type.
    """
    type = [type] if isinstance(type, str) else list(type)
    return [loc for loc in self.data.locators if loc.type in type]