Преглед на файлове

prepare reason refactoring

jrobinAV преди 10 месеца
родител
ревизия
26103fd0bd

+ 4 - 3
taipy/core/_entity/submittable.py

@@ -19,8 +19,9 @@ from ..common._listattributes import _ListAttributes
 from ..common._utils import _Subscriber
 from ..data.data_node import DataNode
 from ..job.job import Job
-from ..reason._reason_factory import _build_data_node_is_being_edited_reason, _build_data_node_is_not_written
-from ..reason.reason import Reasons
+from ..reason import DataNodeEditInProgress, Reasons
+from ..reason.reason import _build_data_node_is_not_written
+from ..reason.reasons import Reasons
 from ..submission.submission import Submission
 from ..task.task import Task
 from ._dag import _DAG
@@ -94,7 +95,7 @@ class Submittable:
 
         for node in self.get_inputs():
             if node._edit_in_progress:
-                reason._add_reason(node.id, _build_data_node_is_being_edited_reason(node.id))
+                reason._add_reason(node.id, DataNodeEditInProgress(node.id))
             if not node._last_edit_date:
                 reason._add_reason(node.id, _build_data_node_is_not_written(node.id))
 

+ 2 - 1
taipy/core/reason/__init__.py

@@ -9,4 +9,5 @@
 # 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 .reason import Reasons
+from .reasons import Reasons
+from .reason import *

+ 0 - 37
taipy/core/reason/_reason_factory.py

@@ -1,37 +0,0 @@
-# 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 Optional
-
-from ..data.data_node import DataNodeId
-
-
-def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
-    return f"DataNode {dn_id} is being edited"
-
-
-def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
-    return f"DataNode {dn_id} is not written"
-
-
-def _build_not_submittable_entity_reason(entity_id: str) -> str:
-    return f"Entity {entity_id} is not a submittable entity"
-
-
-def _build_wrong_config_type_reason(config_id: str, config_type: Optional[str]) -> str:
-    if config_type:
-        return f'Object "{config_id}" must be a valid {config_type}'
-
-    return f'Object "{config_id}" is not a valid config to be created'
-
-
-def _build_not_global_scope_reason(config_id: str) -> str:
-    return f'Data node config "{config_id}" does not have GLOBAL scope'

+ 60 - 22
taipy/core/reason/reason.py

@@ -9,33 +9,71 @@
 # 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
+from typing import Optional
 
+from ..data.data_node import DataNodeId
 
-class Reasons:
-    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) -> "Reasons":
-        if entity_id not in self._reasons:
-            self._reasons[entity_id] = set()
-        self._reasons[entity_id].add(reason)
-        return self
+class Reason:
+    """
+    TODO - NOT DOCUMENTED
+    """
+    def __init__(self, reason: str):
+        self.reason = reason
 
-    def _remove_reason(self, entity_id: str, reason: str) -> "Reasons":
-        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]
-        return self
+    def __str__(self):
+        return self.reason
 
-    def _entity_id_exists_in_reason(self, entity_id: str) -> bool:
-        return entity_id in self._reasons
+    def __repr__(self):
+        return self.reason
 
-    def __bool__(self) -> bool:
-        return len(self._reasons) == 0
+
+class _DataNodeReasonMixin:
+    def __init__(self, datanode_id: DataNodeId):
+        self.datanode_id = datanode_id
 
     @property
-    def reasons(self) -> str:
-        return "; ".join("; ".join(reason) for reason in self._reasons.values()) + "." if self._reasons else ""
+    def datanode(self):
+        from ..data._data_manager_factory import _DataManagerFactory
+        return _DataManagerFactory._build_manager()._get(self.datanode_id)
+
+
+class DataNodeEditInProgress(Reason, _DataNodeReasonMixin):
+    """
+    TODO - NOT DOCUMENTED
+    """
+    def __init__(self, datanode_id: DataNodeId):
+        Reason.__init__(self, f"DataNode {datanode_id} is being edited")
+        _DataNodeReasonMixin.__init__(self, datanode_id)
+
+
+class DataNodeIsNotWritten(Reason, _DataNodeReasonMixin):
+    """
+    TODO - NOT DOCUMENTED
+    """
+    def __init__(self, datanode_id: DataNodeId):
+        Reason.__init__(self, f"DataNode {datanode_id} is not written")
+        _DataNodeReasonMixin.__init__(self, datanode_id)
+
+
+def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
+    return f"DataNode {dn_id} is not written"
+
+
+def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
+    return f"DataNode {dn_id} is not written"
+
+
+def _build_not_submittable_entity_reason(entity_id: str) -> str:
+    return f"Entity {entity_id} is not a submittable entity"
+
+
+def _build_wrong_config_type_reason(config_id: str, config_type: Optional[str]) -> str:
+    if config_type:
+        return f'Object "{config_id}" must be a valid {config_type}'
+
+    return f'Object "{config_id}" is not a valid config to be created'
+
+
+def _build_not_global_scope_reason(config_id: str) -> str:
+    return f'Data node config "{config_id}" does not have GLOBAL scope'

+ 47 - 0
taipy/core/reason/reasons.py

@@ -0,0 +1,47 @@
+# 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
+from .reason import Reason
+
+
+class Reasons:
+    """
+    This class is used to store all the reasons to explain why some Taipy operations are not allowed.
+
+    TODO - Add more details about the class.
+    """
+    def __init__(self, entity_id: str) -> None:
+        self.entity_id: str = entity_id
+        self._reasons: Dict[str, Set[Reason]] = {}
+
+    def _add_reason(self, entity_id: str, reason: str) -> "Reasons":
+        if entity_id not in self._reasons:
+            self._reasons[entity_id] = set()
+        self._reasons[entity_id].add(reason)
+        return self
+
+    def _remove_reason(self, entity_id: str, reason: str) -> "Reasons":
+        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]
+        return self
+
+    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
+
+    @property
+    def reasons(self) -> str:
+        return "; ".join("; ".join(reason) for reason in self._reasons.values()) + "." if self._reasons else ""

+ 1 - 5
taipy/core/task/_task_manager.py

@@ -26,11 +26,7 @@ from ..cycle.cycle_id import CycleId
 from ..data._data_manager_factory import _DataManagerFactory
 from ..exceptions.exceptions import NonExistingTask
 from ..notification import EventEntityType, EventOperation, Notifier, _make_event
-from ..reason._reason_factory import (
-    _build_data_node_is_being_edited_reason,
-    _build_data_node_is_not_written,
-    _build_not_submittable_entity_reason,
-)
+from ..reason import Reasons, DataNodeEditInProgress, DataNodeIsNotWritten, _build_not_submittable_entity_reason
 from ..reason.reason import Reasons
 from ..scenario.scenario_id import ScenarioId
 from ..sequence.sequence_id import SequenceId