浏览代码

added reason class

Toan Quach 1 年之前
父节点
当前提交
51632e8fbf

+ 13 - 18
taipy/core/_entity/_ready_to_run_property.py

@@ -11,6 +11,7 @@
 
 from typing import TYPE_CHECKING, Dict, Set, Union
 
+from ..common._reason import Reason
 from ..notification import EventOperation, Notifier, _make_event
 
 if TYPE_CHECKING:
@@ -28,7 +29,7 @@ class _ReadyToRunProperty:
 
     # A nested dictionary of the submittable entities (Scenario, Sequence, Task) and
     # the data nodes that make it not ready_to_run with the reason(s)
-    _submittable_id_datanodes: Dict[Union["ScenarioId", "SequenceId", "TaskId"], Dict["DataNodeId", Set[str]]] = {}
+    _submittable_id_datanodes: Dict[Union["ScenarioId", "SequenceId", "TaskId"], Reason] = {}
 
     @classmethod
     def _add(cls, dn: "DataNode", reason: str) -> None:
@@ -40,13 +41,13 @@ class _ReadyToRunProperty:
 
         for scenario_parent in parent_entities.get(Scenario._MANAGER_NAME, []):
             if dn in scenario_parent.get_inputs():
-                _ReadyToRunProperty.__add(scenario_parent, dn, reason)
+                cls.__add(scenario_parent, dn, reason)
         for sequence_parent in parent_entities.get(Sequence._MANAGER_NAME, []):
             if dn in sequence_parent.get_inputs():
-                _ReadyToRunProperty.__add(sequence_parent, dn, reason)
+                cls.__add(sequence_parent, dn, reason)
         for task_parent in parent_entities.get(Task._MANAGER_NAME, []):
             if dn in task_parent.input.values():
-                _ReadyToRunProperty.__add(task_parent, dn, reason)
+                cls.__add(task_parent, dn, reason)
 
     @classmethod
     def _remove(cls, datanode: "DataNode", reason: str) -> None:
@@ -58,18 +59,14 @@ class _ReadyToRunProperty:
         to_remove_dn = False
         for submittable_id in submittable_ids:
             # check remove the reason
-            if reason in cls._submittable_id_datanodes.get(submittable_id, {}).get(datanode.id, set()):
-                cls._submittable_id_datanodes[submittable_id][datanode.id].remove(reason)
-            if len(cls._submittable_id_datanodes.get(submittable_id, {}).get(datanode.id, set())) == 0:
-                to_remove_dn = True
-                cls._submittable_id_datanodes.get(submittable_id, {}).pop(datanode.id, None)
-                if (
-                    submittable_id in cls._submittable_id_datanodes
-                    and len(cls._submittable_id_datanodes[submittable_id]) == 0
-                ):
+            reason_entity = cls._submittable_id_datanodes.get(submittable_id)
+            if reason_entity is not None:
+                reason_entity._remove_reason(datanode.id, reason)
+                to_remove_dn = not reason_entity._entity_id_exists_in_reason(datanode.id)
+                if reason_entity:
                     submittable = tp_get(submittable_id)
                     cls.__publish_submittable_property_event(submittable, True)
-                    cls._submittable_id_datanodes.pop(submittable_id, None)
+                    cls._submittable_id_datanodes.pop(submittable_id)
 
         if to_remove_dn:
             cls._datanode_id_submittables.pop(datanode.id)
@@ -84,10 +81,8 @@ class _ReadyToRunProperty:
             cls.__publish_submittable_property_event(submittable, False)
 
         if submittable.id not in cls._submittable_id_datanodes:
-            cls._submittable_id_datanodes[submittable.id] = {}
-        if datanode.id not in cls._submittable_id_datanodes[submittable.id]:
-            cls._submittable_id_datanodes[submittable.id][datanode.id] = set()
-        cls._submittable_id_datanodes[submittable.id][datanode.id].add(reason)
+            cls._submittable_id_datanodes[submittable.id] = Reason(submittable.id)
+        cls._submittable_id_datanodes[submittable.id]._add_reason(datanode.id, reason)
 
     @staticmethod
     def __publish_submittable_property_event(

+ 35 - 0
taipy/core/common/_reason.py

@@ -0,0 +1,35 @@
+# Copyright 2021-2024 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+
+from typing import Dict, Set
+
+
+class Reason:
+    def __init__(self, entity_id: str) -> None:
+        self.entity_id: str = entity_id
+        self.reasons: Dict[str, Set[str]] = {}
+
+    def _add_reason(self, entity_id: str, reason: str) -> None:
+        if entity_id not in self.reasons:
+            self.reasons[entity_id] = set()
+        self.reasons[entity_id].add(reason)
+
+    def _remove_reason(self, entity_id: str, reason: str) -> None:
+        if entity_id in self.reasons and reason in self.reasons[entity_id]:
+            self.reasons[entity_id].remove(reason)
+            if len(self.reasons[entity_id]) == 0:
+                del self.reasons[entity_id]
+
+    def _entity_id_exists_in_reason(self, entity_id: str) -> bool:
+        return entity_id in self.reasons
+
+    def __bool__(self) -> bool:
+        return len(self.reasons) == 0

+ 8 - 8
tests/core/_entity/test_ready_to_run_property.py

@@ -95,10 +95,10 @@ def test_scenario_not_submittable_if_one_input_edit_in_progress():
     assert not scenario_manager._is_submittable(scenario)
 
     assert scenario.id in _ReadyToRunProperty._submittable_id_datanodes
-    assert dn_1.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id]
+    assert dn_1.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons
     assert dn_1.id in _ReadyToRunProperty._datanode_id_submittables
     assert scenario.id in _ReadyToRunProperty._datanode_id_submittables[dn_1.id]
-    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id][dn_1.id] == {
+    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons[dn_1.id] == {
         f"DataNode {dn_1.id} is being edited"
     }
 
@@ -121,16 +121,16 @@ def test_scenario_not_submittable_for_multiple_reasons():
     assert not scenario_manager._is_submittable(scenario)
 
     assert scenario.id in _ReadyToRunProperty._submittable_id_datanodes
-    assert dn_1.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id]
-    assert dn_2.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id]
+    assert dn_1.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons
+    assert dn_2.id in _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons
     assert dn_1.id in _ReadyToRunProperty._datanode_id_submittables
     assert dn_2.id in _ReadyToRunProperty._datanode_id_submittables
     assert scenario.id in _ReadyToRunProperty._datanode_id_submittables[dn_1.id]
-    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id][dn_1.id] == {
+    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons[dn_1.id] == {
         f"DataNode {dn_1.id} is being edited"
     }
     assert scenario.id in _ReadyToRunProperty._datanode_id_submittables[dn_2.id]
-    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id][dn_2.id] == {
+    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons[dn_2.id] == {
         f"DataNode {dn_2.id} is being edited",
         f"DataNode {dn_2.id} is not written",
     }
@@ -150,7 +150,7 @@ def test_writing_input_remove_reasons():
     assert scenario.id not in _ReadyToRunProperty._submittable_id_datanodes
 
     dn_1.lock_edit()
-    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id][dn_1.id] == {
+    assert _ReadyToRunProperty._submittable_id_datanodes[scenario.id].reasons[dn_1.id] == {
         f"DataNode {dn_1.id} is being edited",
         f"DataNode {dn_1.id} is not written",
     }
@@ -172,7 +172,7 @@ def __assert_not_submittable_becomes_submittable_when_dn_edited(sequence, manage
     assert sequence.id not in _ReadyToRunProperty._submittable_id_datanodes
 
     dn.lock_edit()
-    assert _ReadyToRunProperty._submittable_id_datanodes[sequence.id][dn.id] == {
+    assert _ReadyToRunProperty._submittable_id_datanodes[sequence.id].reasons[dn.id] == {
         f"DataNode {dn.id} is being edited",
         f"DataNode {dn.id} is not written",
     }