浏览代码

refactor: make the EventEntityType, _UNSUBMITTABLE_ENTITY_TYPES expandable from elsewhere

trgiangdo 7 月之前
父节点
当前提交
c2218c58ef
共有 3 个文件被更改,包括 36 次插入22 次删除
  1. 1 1
      taipy/core/notification/__init__.py
  2. 2 2
      taipy/core/notification/_topic.py
  3. 33 19
      taipy/core/notification/event.py

+ 1 - 1
taipy/core/notification/__init__.py

@@ -26,6 +26,6 @@ object) must be instantiated with an associated event queue.
 from ._registration import _Registration
 from ._topic import _Topic
 from .core_event_consumer import CoreEventConsumerBase
-from .event import _ENTITY_TO_EVENT_ENTITY_TYPE, Event, EventEntityType, EventOperation, _make_event
+from .event import Event, EventEntityType, EventOperation, _make_event
 from .notifier import Notifier, _publish_event
 from .registration_id import RegistrationId

+ 2 - 2
taipy/core/notification/_topic.py

@@ -12,7 +12,7 @@
 from typing import Optional
 
 from ..exceptions.exceptions import InvalidEventOperation
-from .event import _UNSUBMITTABLE_ENTITY_TYPES, EventEntityType, EventOperation
+from .event import Event, EventEntityType, EventOperation
 
 
 class _Topic:
@@ -35,7 +35,7 @@ class _Topic:
         if (
             entity_type
             and operation
-            and entity_type in _UNSUBMITTABLE_ENTITY_TYPES
+            and entity_type in Event._UNSUBMITTABLE_ENTITY_TYPES
             and operation == EventOperation.SUBMISSION
         ):
             raise InvalidEventOperation

+ 33 - 19
taipy/core/notification/event.py

@@ -62,23 +62,20 @@ class EventEntityType(_ReprEnum):
     JOB = 6
     SUBMISSION = 7
 
-
-_NO_ATTRIBUTE_NAME_OPERATIONS = {EventOperation.CREATION, EventOperation.DELETION, EventOperation.SUBMISSION}
-_UNSUBMITTABLE_ENTITY_TYPES = (
-    EventEntityType.CYCLE,
-    EventEntityType.DATA_NODE,
-    EventEntityType.JOB,
-    EventEntityType.SUBMISSION,
-)
-_ENTITY_TO_EVENT_ENTITY_TYPE = {
-    "scenario": EventEntityType.SCENARIO,
-    "sequence": EventEntityType.SEQUENCE,
-    "task": EventEntityType.TASK,
-    "data": EventEntityType.DATA_NODE,
-    "job": EventEntityType.JOB,
-    "cycle": EventEntityType.CYCLE,
-    "submission": EventEntityType.SUBMISSION,
-}
+    @classmethod
+    def add_member(cls, name, value):
+        # Check if the member already exists to prevent duplication
+        if name in cls._member_map_:
+            return
+
+        # Create a new enum member
+        new_member = object.__new__(cls)
+        new_member._name_ = name
+        new_member._value_ = value
+        # Update the class dictionary and member maps
+        setattr(cls, name, new_member)
+        cls._member_map_[name] = new_member
+        cls._value2member_map_[value] = new_member
 
 
 @dataclass(frozen=True)
@@ -88,6 +85,23 @@ class Event:
     An event holds the necessary attributes to identify the change.
     """
 
+    _NO_ATTRIBUTE_NAME_OPERATIONS = {EventOperation.CREATION, EventOperation.DELETION, EventOperation.SUBMISSION}
+    _UNSUBMITTABLE_ENTITY_TYPES = {
+        EventEntityType.CYCLE,
+        EventEntityType.DATA_NODE,
+        EventEntityType.JOB,
+        EventEntityType.SUBMISSION,
+    }
+    _ENTITY_TO_EVENT_ENTITY_TYPE = {
+        "scenario": EventEntityType.SCENARIO,
+        "sequence": EventEntityType.SEQUENCE,
+        "task": EventEntityType.TASK,
+        "data": EventEntityType.DATA_NODE,
+        "job": EventEntityType.JOB,
+        "cycle": EventEntityType.CYCLE,
+        "submission": EventEntityType.SUBMISSION,
+    }
+
     entity_type: EventEntityType
     """Type of the entity that was changed (`DataNode^`, `Scenario^`, `Cycle^`, etc. )."""
     operation: EventOperation
@@ -112,11 +126,11 @@ class Event:
         super().__setattr__("creation_date", datetime.now())
 
         # Check operation:
-        if self.entity_type in _UNSUBMITTABLE_ENTITY_TYPES and self.operation == EventOperation.SUBMISSION:
+        if self.entity_type in self._UNSUBMITTABLE_ENTITY_TYPES and self.operation == EventOperation.SUBMISSION:
             raise InvalidEventOperation
 
         # Check attribute name:
-        if self.operation in _NO_ATTRIBUTE_NAME_OPERATIONS and self.attribute_name is not None:
+        if self.operation in self._NO_ATTRIBUTE_NAME_OPERATIONS and self.attribute_name is not None:
             raise InvalidEventAttributeName