Пример #1
0
class AbstractInputPathOutputPathState(AbstractState):
    """An Amazon States Language state including InputPath and OutputPath.

    `input_path` and `output_path` let you control what is input and output from
    a state by using Reference Paths.
    """

    def __init__(
        self, *args: Any, input_path: str = "$", output_path: str = "$", **kwargs: Any
    ):
        """Initialize subclasses.

        Args:
            args: Args to pass to parent classes.
            input_path: Used to select a portion of the state input. Default is
                $ (pass everything).
            output_path: Used to select a portion of the state output. Default
                is $ (pass everything).
            kwargs: Kwargs to pass to parent classes.
        """
        super().__init__(*args, **kwargs)
        self.input_path = ReferencePath(input_path)
        self.output_path = ReferencePath(output_path)

    def simulate(self, state_input: Any, resource_to_mock_fn: ResourceToMockFn) -> Any:
        """Simulate the state including input and output processing.

        Args:
            state_input: The input to the state.
            resource_to_mock_fn: A mapping of resource URIs to mock functions to
                use if the state performs a task.

        Returns:
            The output of the state after applying any output processing.
        """
        state_input = self._apply_input_path(state_input)
        state_output = self._execute(state_input, resource_to_mock_fn) or {}
        return self._apply_output_path(state_output)

    def _apply_input_path(self, state_input: Any) -> Any:
        """Apply input path to some state input."""
        state_input = self.input_path.apply(state_input)
        self.print(
            f"State input after applying input path of {self.input_path}:",
            state_input,
            style=Style.DIM,
        )
        return state_input

    def _apply_output_path(self, state_output: Any) -> Any:
        """Apply output path to some state output."""
        state_output = self.output_path.apply(state_output)
        self.print(
            f"State output after applying output path of {self.output_path}:",
            state_output,
            style=Style.DIM,
        )
        return state_output

    def compile(self) -> Dict[str, Any]:  # noqa: A003
        """Compile the state to Amazon States Language.

        Returns:
            A dictionary representing the compiled state in Amazon States
            Language.
        """
        compiled = super().compile()
        if input_path := self.input_path:
            compiled["InputPath"] = str(input_path)
        if output_path := self.output_path:
            compiled["OutputPath"] = str(output_path)
Пример #2
0
class ChoiceRule:
    """Choice Rules are used in Choices.

    When initializing a Choice Rule, a data test expression must be provided. A
    Choice Rule evalulates to `True` or `False` based on the data-test
    expression on some data.
    """

    def __init__(self, variable: str, **data_test_expression: Any):
        """Initialize a Choice Rule.

        Args:
            variable: The Reference Path to a variable in the state input.
            data_test_expression: The data-test expression to use.

        Raises:
            AWSStepFuncsValueError: Raised when there is not exactly one data-test
                expression defined.
        """
        self.variable = ReferencePath(variable)

        if len(data_test_expression) != 1:
            raise AWSStepFuncsValueError(
                "Exactly one data-test expression must be defined"
            )

        self.data_test_expression = DataTestExpression(
            *list(data_test_expression.items())[0]
        )

    def __repr__(self) -> str:
        """Return a string representation of the Choice Rule.

        Returns:
            A string representing the Choice Rule.
        """
        return f"{self.__class__.__name__}({self.variable!r}, {self.data_test_expression.type}={self.data_test_expression.expression!r})"

    def evaluate(self, data: Any) -> bool:
        """Evaulate the Choice Rule with a data-test expression on some data.

        Args:
            data: Input data to evaluate.

        Returns:
            True or false based on the data and the Choice Rule.
        """
        variable_value = self.variable.apply(data)

        if variable_value is None:
            return False

        if "path" in self.data_test_expression.type:
            return eval(f"self._{self.data_test_expression.type}(data, variable_value)")
        else:
            return eval(f"self._{self.data_test_expression.type}(variable_value)")

    def _is_present(self, variable_value: Any) -> bool:
        return variable_value is not None

    def _string_equals(self, variable_value: str) -> bool:
        return variable_value == self.data_test_expression.expression

    def _string_equals_path(self, data: Any, variable_value: str) -> bool:
        string_equals = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (isinstance(string_equals, str)):
            raise AWSStepFuncsValueError(
                "string_equals_path must evaluate to a string value"
            )
        return variable_value == string_equals

    def _string_greater_than(self, variable_value: str) -> bool:
        return variable_value > self.data_test_expression.expression  # type: ignore

    def _string_greater_than_path(self, data: Any, variable_value: str) -> bool:
        string_greater_than = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (isinstance(string_greater_than, str)):  # pragma: no cover
            raise AWSStepFuncsValueError(
                "string_greater_than_path must evaluate to a string value"
            )
        return variable_value > string_greater_than

    def _string_less_than(self, variable_value: str) -> bool:
        return variable_value < self.data_test_expression.expression  # type: ignore

    def _string_less_than_path(self, data: Any, variable_value: str) -> bool:
        string_less_than = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (isinstance(string_less_than, str)):  # pragma: no cover
            raise AWSStepFuncsValueError(
                "string_less_than_path must evaluate to a string value"
            )
        return variable_value < string_less_than

    def _string_greater_than_equals(self, variable_value: str) -> bool:
        return variable_value >= self.data_test_expression.expression  # type: ignore

    def _string_greater_than_equals_path(self, data: Any, variable_value: str) -> bool:
        string_greater_than_equals = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (isinstance(string_greater_than_equals, str)):  # pragma: no cover
            raise AWSStepFuncsValueError(
                "string_greater_than_equals_path must evaluate to a string value"
            )
        return variable_value >= string_greater_than_equals

    def _string_less_than_equals(self, variable_value: str) -> bool:
        return variable_value <= self.data_test_expression.expression  # type: ignore

    def _string_less_than_equals_path(self, data: Any, variable_value: str) -> bool:
        string_less_than_equals = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (isinstance(string_less_than_equals, str)):  # pragma: no cover
            raise AWSStepFuncsValueError(
                "string_less_than_equals_path must evaluate to a string value"
            )
        return variable_value <= string_less_than_equals

    def _numeric_greater_than_equals(self, variable_value: Union[float, int]) -> bool:
        return variable_value >= self.data_test_expression.expression  # type: ignore

    def _numeric_greater_than_path(
        self, data: Any, variable_value: Union[float, int]
    ) -> bool:
        numeric_greater_than = self.data_test_expression.expression.apply(data)  # type: ignore
        if not (
            isinstance(numeric_greater_than, int)
            or isinstance(numeric_greater_than, float)
        ):
            raise AWSStepFuncsValueError(
                "numeric_greater_than_path must evaluate to a numeric value"
            )
        return variable_value > numeric_greater_than

    def _numeric_less_than(self, variable_value: Union[float, int]) -> bool:
        return variable_value < self.data_test_expression.expression  # type: ignore