Quellcode für text_lint.operations.assertions.bases.assertion_base

"""AssertionBase class."""

import abc
from typing import TYPE_CHECKING, List, Match, Optional, Sequence

from text_lint.config import SAVED_NAME_REGEX
from text_lint.exceptions.assertions import AssertionCaptureGroupNotFound
from text_lint.exceptions.results import SplitGroupNotFound
from text_lint.operations.assertions.args.split import (
    AliasYamlSplit,
    SplitArgs,
)
from text_lint.operations.bases.operation_base import OperationBase
from text_lint.operations.mixins.parameter_validation import (
    validator_factories,
)
from text_lint.results.tree import ResultTree

if TYPE_CHECKING:  # pragma: no cover
  from text_lint.linter import states
  from text_lint.schema import AliasYamlOperation, Schema


[Doku] class AssertionBase(OperationBase["states.AssertionState"], abc.ABC): """Assertion operation base class."""
[Doku] def __init__( self, name: str, save: Optional[str] = None, splits: Optional[AliasYamlSplit] = None, ) -> None: """Initialize AssertionBase instances. :param name: The name of the configured assertion. :param save: An optional save id to create for matched values. :param splits: Optional text splitting configuration for matched values. :raises: TypeError """ self.name = name self.save = save self.validate_parameters() self.splits = SplitArgs.create(splits)
[Doku] class Parameters: """Parameter validation for this operation.""" name = {"type": str} save = { "type": str, "optional": True, "validators": [validator_factories.create_matches_regex(SAVED_NAME_REGEX)], }
[Doku] @abc.abstractmethod def apply(self, state: "states.AssertionState") -> None: """Apply this operation to the given state object. :param state: The state object to apply this operation to. """
[Doku] def create_result_tree( self, matches: Sequence[Match[str]], ) -> Optional["ResultTree"]: """Create a result tree for any matches this assertion generates. :param matches: Regex matches generated by the operation. :returns: If applicable the new result tree instance, otherwise None. """ if not self.save: return None result = ResultTree(self.save) for match in matches: try: result.add_matches(match.groups(), self.splits.as_dict()) except SplitGroupNotFound as exc: raise AssertionCaptureGroupNotFound( assertion=self, capture_group=exc.group, ) from exc return result
[Doku] def schema_validator( self, schema_assertion_index: int, schema_assertion_instances: List["AssertionBase"], schema_assertion_definitions: List["AliasYamlOperation"], schema: "Schema", ) -> None: """Validate this operation in the context of an entire loaded schema. :param schema_assertion_index: The index of this assertion in the schema. :param schema_assertion_instances: All schema assertion operations. :param schema_assertion_definitions: All schema assertion definitions. :param schema: The active schema instance in use by the linter. :raises: TypeError """