base.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. """
  3. Base Condition - Abstrakte Basis für alle Conditions.
  4. """
  5. from abc import ABC, abstractmethod
  6. from typing import Any, TYPE_CHECKING
  7. if TYPE_CHECKING:
  8. from trixy_core.scheduler.form_fields import FormField, FormValidationError
  9. class Condition(ABC):
  10. """
  11. Abstrakte Basis für Conditions.
  12. Eine Condition prüft zusätzliche Bedingungen für Job-Ausführung.
  13. """
  14. def __init__(self, name: str | None = None, invert: bool = False):
  15. """
  16. Initialisiert die Condition.
  17. Args:
  18. name: Condition-Name
  19. invert: Ergebnis invertieren (NOT)
  20. """
  21. self._name = name or self.__class__.__name__
  22. self._invert = invert
  23. self._enabled = True
  24. @property
  25. def name(self) -> str:
  26. """Condition-Name."""
  27. return self._name
  28. @property
  29. def is_inverted(self) -> bool:
  30. """Prüft ob Ergebnis invertiert wird."""
  31. return self._invert
  32. @property
  33. def is_enabled(self) -> bool:
  34. """Prüft ob Condition aktiv ist."""
  35. return self._enabled
  36. def enable(self) -> None:
  37. """Aktiviert die Condition."""
  38. self._enabled = True
  39. def disable(self) -> None:
  40. """Deaktiviert die Condition."""
  41. self._enabled = False
  42. @abstractmethod
  43. def _evaluate(self, context: dict) -> bool:
  44. """
  45. Interne Evaluation.
  46. Args:
  47. context: Kontext mit Variablen
  48. Returns:
  49. True wenn Bedingung erfüllt
  50. """
  51. pass
  52. def evaluate(self, context: dict | None = None) -> bool:
  53. """
  54. Evaluiert die Condition.
  55. Args:
  56. context: Kontext mit Variablen
  57. Returns:
  58. True wenn Bedingung erfüllt (ggf. invertiert)
  59. """
  60. if not self._enabled:
  61. return True # Deaktivierte Conditions blockieren nicht
  62. result = self._evaluate(context or {})
  63. if self._invert:
  64. return not result
  65. return result
  66. @abstractmethod
  67. def to_dict(self) -> dict:
  68. """Konvertiert zu Dictionary."""
  69. pass
  70. def _base_dict(self) -> dict:
  71. """Basis-Dictionary für Serialisierung."""
  72. return {
  73. "type": self.__class__.__name__,
  74. "name": self._name,
  75. "invert": self._invert,
  76. "enabled": self._enabled,
  77. }
  78. @classmethod
  79. def form_fields(cls) -> list["FormField"]:
  80. """Gibt die Formular-Felder fuer die TUI zurueck."""
  81. return []
  82. @classmethod
  83. def validate_form(cls, data: dict) -> list["FormValidationError"]:
  84. """Validiert Formular-Daten. Gibt Liste von Fehlern zurueck."""
  85. return []
  86. @classmethod
  87. def display_name(cls) -> str:
  88. """Anzeigename fuer die TUI."""
  89. return cls.__name__
  90. def __repr__(self) -> str:
  91. inv = " (inverted)" if self._invert else ""
  92. return f"{self.__class__.__name__}(name={self._name}{inv})"