Skip to content

tags

Tags

Bases: Sequence[str]

Source code in src/robot/model/tags.py
class Tags(Sequence[str]):
    __slots__ = ['_tags', '_reserved']

    def __init__(self, tags: Iterable[str] = ()):
        if isinstance(tags, Tags):
            self._tags, self._reserved = tags._tags, tags._reserved
        else:
            self._tags, self._reserved = self._init_tags(tags)

    def robot(self, name: str) -> bool:
        """Check do tags contain a reserved tag in format `robot:<name>`.

        This is same as `'robot:<name>' in tags` but considerably faster.
        """
        return name in self._reserved

    def _init_tags(self, tags) -> 'tuple[tuple[str, ...], tuple[str, ...]]':
        if not tags:
            return (), ()
        if isinstance(tags, str):
            tags = (tags,)
        return self._normalize(tags)

    def _normalize(self, tags):
        nd = NormalizedDict([(str(t), None) for t in tags], ignore='_')
        if '' in nd:
            del nd['']
        if 'NONE' in nd:
            del nd['NONE']
        reserved = tuple(tag[6:] for tag in nd.normalized_keys if tag[:6] == 'robot:')
        return tuple(nd), reserved

    def add(self, tags: Iterable[str]):
        self.__init__(tuple(self) + tuple(Tags(tags)))

    def remove(self, tags: Iterable[str]):
        match = TagPatterns(tags).match
        self.__init__([t for t in self if not match(t)])

    def match(self, tags: Iterable[str]) -> bool:
        return TagPatterns(tags).match(self)

    def __contains__(self, tags: Iterable[str]) -> bool:
        return self.match(tags)

    def __len__(self) -> int:
        return len(self._tags)

    def __iter__(self) -> Iterator[str]:
        return iter(self._tags)

    def __str__(self) -> str:
        tags = ', '.join(self)
        return f'[{tags}]'

    def __repr__(self) -> str:
        return repr(list(self))

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, Iterable):
            return False
        if not isinstance(other, Tags):
            other = Tags(other)
        self_normalized = [normalize(tag, ignore='_') for tag in self]
        other_normalized = [normalize(tag, ignore='_') for tag in other]
        return sorted(self_normalized) == sorted(other_normalized)

    @overload
    def __getitem__(self, index: int) -> str:
        ...

    @overload
    def __getitem__(self, index: slice) -> 'Tags':
        ...

    def __getitem__(self, index: 'int|slice') -> 'str|Tags':
        if isinstance(index, slice):
            return Tags(self._tags[index])
        return self._tags[index]

    def __add__(self, other: Iterable[str]) -> 'Tags':
        return Tags(tuple(self) + tuple(Tags(other)))

robot(name)

Check do tags contain a reserved tag in format robot:<name>.

This is same as 'robot:<name>' in tags but considerably faster.

Source code in src/robot/model/tags.py
def robot(self, name: str) -> bool:
    """Check do tags contain a reserved tag in format `robot:<name>`.

    This is same as `'robot:<name>' in tags` but considerably faster.
    """
    return name in self._reserved

normalize_tags(tags)

Performance optimization to normalize tags only once.

Source code in src/robot/model/tags.py
def normalize_tags(tags: Iterable[str]) -> Iterable[str]:
    """Performance optimization to normalize tags only once."""
    if isinstance(tags, NormalizedTags):
        return tags
    if isinstance(tags, str):
        tags = [tags]
    return NormalizedTags([normalize(t, ignore='_') for t in tags])