Toan Quach 1 рік тому
батько
коміт
072b366272

+ 1 - 0
taipy/core/_entity/_entity.py

@@ -16,6 +16,7 @@ from ..notification import Notifier
 
 
 class _Entity:
+    _ID_PREFIX: str
     _MANAGER_NAME: str
     _is_in_context = False
     _in_context_attributes_changed_collector: List

+ 25 - 0
taipy/core/common/_check_entity_and_get_manager.py

@@ -0,0 +1,25 @@
+# Copyright 2023 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 List, Optional, Union
+
+from .._entity._entity import _Entity
+from .._entity._reload import _get_manager
+from .._manager._manager import _Manager
+
+
+def _check_entity_and_get_manager(entity: Union[_Entity, str], possible_classes: List) -> Optional[_Manager]:
+    for possible_class in possible_classes:
+        if isinstance(entity, possible_class) or (
+            isinstance(entity, str) and entity.startswith(possible_class._ID_PREFIX)  # type: ignore
+        ):
+            return _get_manager(possible_class._MANAGER_NAME)  # type: ignore
+    return None

+ 0 - 49
taipy/core/common/_check_instance.py

@@ -1,49 +0,0 @@
-# Copyright 2023 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 Union
-
-from .._entity._entity import _Entity
-from ..cycle.cycle import Cycle
-from ..data.data_node import DataNode
-from ..job.job import Job
-from ..scenario.scenario import Scenario
-from ..sequence.sequence import Sequence
-from ..submission.submission import Submission
-from ..task.task import Task
-
-
-def _is_cycle(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Cycle) or (isinstance(entity, str) and entity.startswith(Cycle._ID_PREFIX))
-
-
-def _is_scenario(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Scenario) or (isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX))
-
-
-def _is_sequence(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Sequence) or (isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX))
-
-
-def _is_task(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Task) or (isinstance(entity, str) and entity.startswith(Task._ID_PREFIX))
-
-
-def _is_job(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Job) or (isinstance(entity, str) and entity.startswith(Job._ID_PREFIX))
-
-
-def _is_data_node(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, DataNode) or (isinstance(entity, str) and entity.startswith(DataNode._ID_PREFIX))
-
-
-def _is_submission(entity: Union[_Entity, str]) -> bool:
-    return isinstance(entity, Submission) or (isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX))

+ 1 - 1
taipy/core/exceptions/exceptions.py

@@ -154,7 +154,7 @@ class NonExistingJob(RuntimeError):
 class SubmissionNotDeletedException(RuntimeError):
     """Raised if there is an attempt to delete a submission that cannot be deleted.
 
-    This exception can be raised by `taipy.delete_job()^`.
+    This exception can be raised by `taipy.delete()^`.
     """
 
     def __init__(self, submission_id: str):

+ 1 - 1
taipy/core/job/_job_manager.py

@@ -64,7 +64,7 @@ class _JobManager(_Manager[Job], _VersionMixin):
             _JobDispatcher._pop_dispatched_process(job.id)
         else:
             err = JobNotDeletedException(job.id)
-            cls._logger.warning(err)
+            cls._logger.error(err)
             raise err
 
     @classmethod

+ 3 - 3
taipy/core/submission/_submission_manager.py

@@ -18,7 +18,7 @@ from ..exceptions.exceptions import SubmissionNotDeletedException
 from ..notification import EventEntityType, EventOperation, Notifier, _make_event
 from ..scenario.scenario import Scenario
 from ..sequence.sequence import Sequence
-from ..submission.submission import Submission, SubmissionId
+from ..submission.submission import Submission, SubmissionId, SubmissionStatus
 from ..task.task import Task
 
 
@@ -67,11 +67,11 @@ class _SubmissionManager(_Manager[Submission], _VersionMixin):
             super()._delete(submission.id)
         else:
             err = SubmissionNotDeletedException(submission.id)
-            cls._logger.warning(err)
+            cls._logger.error(err)
             raise err
 
     @classmethod
     def _is_deletable(cls, submission: Union[Submission, SubmissionId]) -> bool:
         if isinstance(submission, str):
             submission = cls._get(submission)
-        return submission.is_finished()
+        return submission.is_finished() or submission.submission_status == SubmissionStatus.UNDEFINED

+ 5 - 7
taipy/core/submission/submission.py

@@ -207,13 +207,11 @@ class Submission(_Entity, _Labeled):
         Returns:
             True if the submission is finished.
         """
-        submission_status = self.submission_status
-        return (
-            submission_status == SubmissionStatus.COMPLETED
-            or submission_status == SubmissionStatus.FAILED
-            or submission_status == SubmissionStatus.CANCELED
-            or submission_status == SubmissionStatus.UNDEFINED
-        )
+        return self.submission_status in [
+            SubmissionStatus.COMPLETED,
+            SubmissionStatus.FAILED,
+            SubmissionStatus.CANCELED,
+        ]
 
     def is_deletable(self) -> bool:
         """Indicate if the submission can be deleted.

+ 29 - 113
taipy/core/taipy.py

@@ -20,15 +20,7 @@ from taipy.logger._taipy_logger import _TaipyLogger
 from ._entity._entity import _Entity
 from ._entity._reload import _get_manager
 from ._version._version_manager_factory import _VersionManagerFactory
-from .common._check_instance import (
-    _is_cycle,
-    _is_data_node,
-    _is_job,
-    _is_scenario,
-    _is_sequence,
-    _is_submission,
-    _is_task,
-)
+from .common._check_entity_and_get_manager import _check_entity_and_get_manager
 from .common._warnings import _warn_no_core_service
 from .config.data_node_config import DataNodeConfig
 from .config.scenario_config import ScenarioConfig
@@ -71,16 +63,8 @@ def set(entity: Union[DataNode, Task, Sequence, Scenario, Cycle]):
         entity (Union[DataNode^, Task^, Sequence^, Scenario^, Cycle^]): The
             entity to save or update.
     """
-    if _is_cycle(entity):
-        return _get_manager(Cycle._MANAGER_NAME)._set(entity)
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._set(entity)
-    if _is_sequence(entity):
-        return _get_manager(Sequence._MANAGER_NAME)._set(entity)
-    if _is_task(entity):
-        return _get_manager(Task._MANAGER_NAME)._set(entity)
-    if _is_data_node(entity):
-        return _get_manager(DataNode._MANAGER_NAME)._set(entity)
+    if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, DataNode]):
+        manager._set(entity)
 
 
 def is_submittable(entity: Union[Scenario, ScenarioId, Sequence, SequenceId, Task, TaskId]) -> bool:
@@ -91,12 +75,8 @@ def is_submittable(entity: Union[Scenario, ScenarioId, Sequence, SequenceId, Tas
     Returns:
         True if the given entity can be submitted. False otherwise.
     """
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._is_submittable(entity)  # type: ignore
-    if _is_sequence(entity):
-        return _get_manager(Sequence._MANAGER_NAME)._is_submittable(entity)  # type: ignore
-    if _is_task(entity):
-        return _get_manager(Task._MANAGER_NAME)._is_submittable(entity)  # type: ignore
+    if manager := _check_entity_and_get_manager(entity, [Scenario, Sequence, Task]):
+        return manager._is_submittable(entity)  # type: ignore
     return False
 
 
@@ -125,20 +105,8 @@ def is_editable(
     Returns:
         True if the given entity can be edited. False otherwise.
     """
-    if _is_cycle(entity):
-        return _get_manager(Cycle._MANAGER_NAME)._is_editable(entity)
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._is_editable(entity)
-    if _is_sequence(entity):
-        return _get_manager(Sequence._MANAGER_NAME)._is_editable(entity)
-    if _is_task(entity):
-        return _get_manager(Task._MANAGER_NAME)._is_editable(entity)
-    if _is_job(entity):
-        return _get_manager(Job._MANAGER_NAME)._is_editable(entity)
-    if _is_data_node(entity):
-        return _get_manager(DataNode._MANAGER_NAME)._is_editable(entity)
-    if _is_submission(entity):
-        return _get_manager(Submission._MANAGER_NAME)._is_editable(entity)
+    if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]):
+        return manager._is_editable(entity)
     return False
 
 
@@ -167,20 +135,8 @@ def is_readable(
     Returns:
         True if the given entity can be read. False otherwise.
     """
-    if _is_cycle(entity):
-        return _get_manager(Cycle._MANAGER_NAME)._is_readable(entity)
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._is_readable(entity)
-    if _is_sequence(entity):
-        return _get_manager(Sequence._MANAGER_NAME)._is_readable(entity)
-    if _is_task(entity):
-        return _get_manager(Task._MANAGER_NAME)._is_readable(entity)
-    if _is_job(entity):
-        return _get_manager(Job._MANAGER_NAME)._is_readable(entity)
-    if _is_data_node(entity):
-        return _get_manager(DataNode._MANAGER_NAME)._is_readable(entity)
-    if _is_submission(entity):
-        return _get_manager(Submission._MANAGER_NAME)._is_readable(entity)
+    if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]):
+        return manager._is_readable(entity)
     return False
 
 
@@ -212,16 +168,8 @@ def submit(
             - If a `Scenario^` or a `Sequence^` is provided, it will return a list of `Job^`.
             - If a `Task^` is provided, it will return the created `Job^`.
     """
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._submit(  # type: ignore
-            entity, force=force, wait=wait, timeout=timeout
-        )
-    if _is_sequence(entity):
-        return _get_manager(Sequence._MANAGER_NAME)._submit(  # type: ignore
-            entity, force=force, wait=wait, timeout=timeout
-        )
-    if _is_task(entity):
-        return _get_manager(Task._MANAGER_NAME)._submit(entity, force=force, wait=wait, timeout=timeout)  # type: ignore
+    if manager := _check_entity_and_get_manager(entity, [Scenario, Sequence, Task]):
+        return manager._submit(entity, force=force, wait=wait, timeout=timeout)  # type: ignore
 
 
 @overload
@@ -287,20 +235,11 @@ def exists(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, C
         (`Job^`, `Cycle^`, `Scenario^`, `Sequence^`, `Task^`, `DataNode^`)
         based on their respective identifier prefixes.
     """
-    if _is_job(entity_id):
-        return _get_manager(Job._MANAGER_NAME)._exists(JobId(entity_id))
-    if _is_cycle(entity_id):
-        return _get_manager(Cycle._MANAGER_NAME)._exists(CycleId(entity_id))
-    if _is_scenario(entity_id):
-        return _get_manager(Scenario._MANAGER_NAME)._exists(ScenarioId(entity_id))
-    if _is_sequence(entity_id):
-        return _get_manager(Sequence._MANAGER_NAME)._exists(SequenceId(entity_id))
-    if _is_task(entity_id):
-        return _get_manager(Task._MANAGER_NAME)._exists(TaskId(entity_id))
-    if _is_data_node(entity_id):
-        return _get_manager(DataNode._MANAGER_NAME)._exists(DataNodeId(entity_id))
-    if _is_submission(entity_id):
-        return _get_manager(Submission._MANAGER_NAME)._exists(SubmissionId(entity_id))
+    if manager := _check_entity_and_get_manager(
+        entity_id, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]
+    ):
+        return manager._exists(entity_id)
+
     raise ModelNotFound("NOT_DETERMINED", entity_id)
 
 
@@ -366,20 +305,11 @@ def get(
     Raises:
         ModelNotFound^: If the provided *entity_id* does not match any known entity pattern.
     """
-    if _is_job(entity_id):
-        return _get_manager(Job._MANAGER_NAME)._get(JobId(entity_id))
-    if _is_cycle(entity_id):
-        return _get_manager(Cycle._MANAGER_NAME)._get(CycleId(entity_id))
-    if _is_scenario(entity_id):
-        return _get_manager(Scenario._MANAGER_NAME)._get(ScenarioId(entity_id))
-    if _is_sequence(entity_id):
-        return _get_manager(Sequence._MANAGER_NAME)._get(SequenceId(entity_id))
-    if _is_task(entity_id):
-        return _get_manager(Task._MANAGER_NAME)._get(TaskId(entity_id))
-    if _is_data_node(entity_id):
-        return _get_manager(DataNode._MANAGER_NAME)._get(DataNodeId(entity_id))
-    if _is_submission(entity_id):
-        return _get_manager(Submission._MANAGER_NAME)._get(SubmissionId(entity_id))
+    if manager := _check_entity_and_get_manager(
+        entity_id, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]
+    ):
+        return manager._get(entity_id)
+
     raise ModelNotFound("NOT_DETERMINED", entity_id)
 
 
@@ -407,12 +337,8 @@ def is_deletable(entity: Union[Scenario, Job, Submission, ScenarioId, JobId, Sub
     Returns:
         True if the given scenario, job or submission can be deleted. False otherwise.
     """
-    if _is_job(entity):
-        return _get_manager(Job._MANAGER_NAME)._is_deletable(entity)  # type: ignore
-    if _is_scenario(entity):
-        return _get_manager(Scenario._MANAGER_NAME)._is_deletable(entity)  # type: ignore
-    if _is_submission(entity):
-        return _get_manager(Submission._MANAGER_NAME)._is_deletable(entity)  # type: ignore
+    if manager := _check_entity_and_get_manager(entity, [Scenario, Job, Submission]):
+        return manager._is_deletable(entity)  # type: ignore
     return True
 
 
@@ -439,22 +365,12 @@ def delete(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, C
     Raises:
         ModelNotFound: No entity corresponds to the specified *entity_id*.
     """
-    if _is_job(entity_id):
-        job_manager = _get_manager(Job._MANAGER_NAME)
-        return job_manager._delete(job_manager._get(JobId(entity_id)))  # type: ignore
-    if _is_cycle(entity_id):
-        return _get_manager(Cycle._MANAGER_NAME)._hard_delete(CycleId(entity_id))  # type: ignore
-    if _is_scenario(entity_id):
-        return _get_manager(Scenario._MANAGER_NAME)._hard_delete(ScenarioId(entity_id))  # type: ignore
-    if _is_sequence(entity_id):
-        return _get_manager(Sequence._MANAGER_NAME)._hard_delete(SequenceId(entity_id))  # type: ignore
-    if _is_task(entity_id):
-        return _get_manager(Task._MANAGER_NAME)._hard_delete(TaskId(entity_id))  # type: ignore
-    if _is_data_node(entity_id):
-        return _get_manager(DataNode._MANAGER_NAME)._delete(DataNodeId(entity_id))  # type: ignore
-    if _is_submission(entity_id):
-        submission_manager = _get_manager(Submission._MANAGER_NAME)
-        return submission_manager._delete(submission_manager._get(SubmissionId(entity_id)))  # type: ignore
+    if manager := _check_entity_and_get_manager(entity_id, [Cycle, Scenario, Sequence, Task]):
+        return manager._hard_delete(entity_id)  # type: ignore
+
+    if manager := _check_entity_and_get_manager(entity_id, [Job, DataNode, Submission]):
+        return manager._delete(entity_id)  # type: ignore
+
     raise ModelNotFound("NOT_DETERMINED", entity_id)
 
 

+ 1 - 1
tests/core/submission/test_submission.py

@@ -751,7 +751,7 @@ def test_is_finished():
 
     submission.submission_status = SubmissionStatus.UNDEFINED
     assert submission.submission_status == SubmissionStatus.UNDEFINED
-    assert submission.is_finished()
+    assert not submission.is_finished()
 
     submission.submission_status = SubmissionStatus.CANCELED
     assert submission.submission_status == SubmissionStatus.CANCELED