_manager.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import pathlib
  12. from typing import Dict, Generic, Iterable, List, Optional, TypeVar, Union
  13. from taipy.logger._taipy_logger import _TaipyLogger
  14. from .._entity._entity_ids import _EntityIds
  15. from .._repository._abstract_repository import _AbstractRepository
  16. from ..exceptions.exceptions import ModelNotFound
  17. from ..notification import Event, EventOperation, Notifier
  18. EntityType = TypeVar("EntityType")
  19. class _Manager(Generic[EntityType]):
  20. _repository: _AbstractRepository
  21. _logger = _TaipyLogger._get_logger()
  22. _ENTITY_NAME: str = "Entity"
  23. @classmethod
  24. def _delete_all(cls):
  25. """
  26. Deletes all entities.
  27. """
  28. cls._repository._delete_all()
  29. if hasattr(cls, "_EVENT_ENTITY_TYPE"):
  30. Notifier.publish(
  31. Event(
  32. cls._EVENT_ENTITY_TYPE,
  33. EventOperation.DELETION,
  34. metadata={"delete_all": True},
  35. )
  36. )
  37. @classmethod
  38. def _delete_many(cls, ids: Iterable):
  39. """
  40. Deletes entities by a list of ids.
  41. """
  42. cls._repository._delete_many(ids)
  43. if hasattr(cls, "_EVENT_ENTITY_TYPE"):
  44. for entity_id in ids:
  45. Notifier.publish(
  46. Event(
  47. cls._EVENT_ENTITY_TYPE, # type: ignore
  48. EventOperation.DELETION,
  49. entity_id=entity_id,
  50. metadata={"delete_all": True},
  51. )
  52. )
  53. @classmethod
  54. def _delete_by_version(cls, version_number: str):
  55. """
  56. Deletes entities by version number.
  57. """
  58. cls._repository._delete_by(attribute="version", value=version_number)
  59. if hasattr(cls, "_EVENT_ENTITY_TYPE"):
  60. Notifier.publish(
  61. Event(
  62. cls._EVENT_ENTITY_TYPE, # type: ignore
  63. EventOperation.DELETION,
  64. metadata={"delete_by_version": version_number},
  65. )
  66. )
  67. @classmethod
  68. def _delete(cls, id):
  69. """
  70. Deletes an entity by id.
  71. """
  72. cls._repository._delete(id)
  73. if hasattr(cls, "_EVENT_ENTITY_TYPE"):
  74. Notifier.publish(
  75. Event(
  76. cls._EVENT_ENTITY_TYPE,
  77. EventOperation.DELETION,
  78. entity_id=id,
  79. )
  80. )
  81. @classmethod
  82. def _set(cls, entity: EntityType):
  83. """
  84. Save or update an entity.
  85. """
  86. cls._repository._save(entity)
  87. @classmethod
  88. def _get_all(cls, version_number: Optional[str] = "all") -> List[EntityType]:
  89. """
  90. Returns all entities.
  91. """
  92. filters: List[Dict] = []
  93. return cls._repository._load_all(filters)
  94. @classmethod
  95. def _get_all_by(cls, filters: Optional[List[Dict]] = None) -> List[EntityType]:
  96. """
  97. Returns all entities based on a criteria.
  98. """
  99. if not filters:
  100. filters = []
  101. return cls._repository._load_all(filters)
  102. @classmethod
  103. def _get(cls, entity: Union[str, EntityType], default=None) -> EntityType:
  104. """
  105. Returns an entity by id or reference.
  106. """
  107. entity_id = entity if isinstance(entity, str) else entity.id # type: ignore
  108. try:
  109. return cls._repository._load(entity_id)
  110. except ModelNotFound:
  111. cls._logger.error(f"{cls._ENTITY_NAME} not found: {entity_id}")
  112. return default
  113. @classmethod
  114. def _exists(cls, entity_id: str) -> bool:
  115. """
  116. Returns True if the entity id exists.
  117. """
  118. return cls._repository._exists(entity_id)
  119. @classmethod
  120. def _delete_entities_of_multiple_types(cls, _entity_ids: _EntityIds):
  121. """
  122. Deletes entities of multiple types.
  123. """
  124. from ..cycle._cycle_manager_factory import _CycleManagerFactory
  125. from ..data._data_manager_factory import _DataManagerFactory
  126. from ..job._job_manager_factory import _JobManagerFactory
  127. from ..scenario._scenario_manager_factory import _ScenarioManagerFactory
  128. from ..sequence._sequence_manager_factory import _SequenceManagerFactory
  129. from ..submission._submission_manager_factory import _SubmissionManagerFactory
  130. from ..task._task_manager_factory import _TaskManagerFactory
  131. _CycleManagerFactory._build_manager()._delete_many(_entity_ids.cycle_ids)
  132. _SequenceManagerFactory._build_manager()._delete_many(_entity_ids.sequence_ids)
  133. _ScenarioManagerFactory._build_manager()._delete_many(_entity_ids.scenario_ids)
  134. _TaskManagerFactory._build_manager()._delete_many(_entity_ids.task_ids)
  135. _JobManagerFactory._build_manager()._delete_many(_entity_ids.job_ids)
  136. _DataManagerFactory._build_manager()._delete_many(_entity_ids.data_node_ids)
  137. _SubmissionManagerFactory._build_manager()._delete_many(_entity_ids.submission_ids)
  138. @classmethod
  139. def _export(cls, id: str, folder_path: Union[str, pathlib.Path], **kwargs):
  140. """
  141. Export an entity.
  142. """
  143. return cls._repository._export(id, folder_path)
  144. @classmethod
  145. def _is_editable(cls, entity: Union[EntityType, str]) -> bool:
  146. return True
  147. @classmethod
  148. def _is_readable(cls, entity: Union[EntityType, str]) -> bool:
  149. return True