| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # -*- coding: utf-8 -*-
- """
- Base Condition - Abstrakte Basis für alle Conditions.
- """
- from abc import ABC, abstractmethod
- from typing import Any, TYPE_CHECKING
- if TYPE_CHECKING:
- from trixy_core.scheduler.form_fields import FormField, FormValidationError
- class Condition(ABC):
- """
- Abstrakte Basis für Conditions.
- Eine Condition prüft zusätzliche Bedingungen für Job-Ausführung.
- """
- def __init__(self, name: str | None = None, invert: bool = False):
- """
- Initialisiert die Condition.
- Args:
- name: Condition-Name
- invert: Ergebnis invertieren (NOT)
- """
- self._name = name or self.__class__.__name__
- self._invert = invert
- self._enabled = True
- @property
- def name(self) -> str:
- """Condition-Name."""
- return self._name
- @property
- def is_inverted(self) -> bool:
- """Prüft ob Ergebnis invertiert wird."""
- return self._invert
- @property
- def is_enabled(self) -> bool:
- """Prüft ob Condition aktiv ist."""
- return self._enabled
- def enable(self) -> None:
- """Aktiviert die Condition."""
- self._enabled = True
- def disable(self) -> None:
- """Deaktiviert die Condition."""
- self._enabled = False
- @abstractmethod
- def _evaluate(self, context: dict) -> bool:
- """
- Interne Evaluation.
- Args:
- context: Kontext mit Variablen
- Returns:
- True wenn Bedingung erfüllt
- """
- pass
- def evaluate(self, context: dict | None = None) -> bool:
- """
- Evaluiert die Condition.
- Args:
- context: Kontext mit Variablen
- Returns:
- True wenn Bedingung erfüllt (ggf. invertiert)
- """
- if not self._enabled:
- return True # Deaktivierte Conditions blockieren nicht
- result = self._evaluate(context or {})
- if self._invert:
- return not result
- return result
- @abstractmethod
- def to_dict(self) -> dict:
- """Konvertiert zu Dictionary."""
- pass
- def _base_dict(self) -> dict:
- """Basis-Dictionary für Serialisierung."""
- return {
- "type": self.__class__.__name__,
- "name": self._name,
- "invert": self._invert,
- "enabled": self._enabled,
- }
- @classmethod
- def form_fields(cls) -> list["FormField"]:
- """Gibt die Formular-Felder fuer die TUI zurueck."""
- return []
- @classmethod
- def validate_form(cls, data: dict) -> list["FormValidationError"]:
- """Validiert Formular-Daten. Gibt Liste von Fehlern zurueck."""
- return []
- @classmethod
- def display_name(cls) -> str:
- """Anzeigename fuer die TUI."""
- return cls.__name__
- def __repr__(self) -> str:
- inv = " (inverted)" if self._invert else ""
- return f"{self.__class__.__name__}(name={self._name}{inv})"
|