_adapters.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 typing as t
  12. from enum import Enum
  13. from taipy.core import (
  14. Cycle,
  15. DataNode,
  16. Job,
  17. Scenario,
  18. Sequence,
  19. Task,
  20. is_deletable,
  21. is_editable,
  22. is_promotable,
  23. is_readable,
  24. is_submittable,
  25. )
  26. from taipy.core import get as core_get
  27. from taipy.gui._warnings import _warn
  28. from taipy.gui.gui import _DoNotUpdate
  29. from taipy.gui.utils import _TaipyBase
  30. # prevent gui from trying to push scenario instances to the front-end
  31. class _GCDoNotUpdate(_DoNotUpdate):
  32. def __repr__(self):
  33. return self.get_label() if hasattr(self, "get_label") else super().__repr__()
  34. Scenario.__bases__ += (_GCDoNotUpdate,)
  35. Sequence.__bases__ += (_GCDoNotUpdate,)
  36. DataNode.__bases__ += (_GCDoNotUpdate,)
  37. Cycle.__bases__ += (_GCDoNotUpdate,)
  38. Job.__bases__ += (_GCDoNotUpdate,)
  39. Task.__bases__ += (_GCDoNotUpdate,)
  40. class _EntityType(Enum):
  41. CYCLE = 0
  42. SCENARIO = 1
  43. SEQUENCE = 2
  44. DATANODE = 3
  45. class _GuiCoreScenarioAdapter(_TaipyBase):
  46. __INNER_PROPS = ["name"]
  47. def get(self):
  48. data = super().get()
  49. if isinstance(data, Scenario):
  50. try:
  51. scenario = core_get(data.id)
  52. if scenario:
  53. return [
  54. scenario.id,
  55. scenario.is_primary,
  56. scenario.config_id,
  57. scenario.creation_date.isoformat(),
  58. scenario.cycle.get_simple_label() if scenario.cycle else "",
  59. scenario.get_simple_label(),
  60. list(scenario.tags) if scenario.tags else [],
  61. [
  62. (k, v)
  63. for k, v in scenario.properties.items()
  64. if k not in _GuiCoreScenarioAdapter.__INNER_PROPS
  65. ]
  66. if scenario.properties
  67. else [],
  68. [
  69. (
  70. s.get_simple_label(),
  71. [t.id for t in s.tasks.values()] if hasattr(s, "tasks") else [],
  72. is_submittable(s),
  73. is_editable(s),
  74. )
  75. for s in scenario.sequences.values()
  76. ]
  77. if hasattr(scenario, "sequences") and scenario.sequences
  78. else [],
  79. {t.id: t.get_simple_label() for t in scenario.tasks.values()}
  80. if hasattr(scenario, "tasks")
  81. else {},
  82. list(scenario.properties.get("authorized_tags", [])) if scenario.properties else [],
  83. is_deletable(scenario),
  84. is_promotable(scenario),
  85. is_submittable(scenario),
  86. is_readable(scenario),
  87. is_editable(scenario),
  88. ]
  89. except Exception as e:
  90. _warn(f"Access to scenario ({data.id if hasattr(data, 'id') else 'No_id'}) failed", e)
  91. return None
  92. @staticmethod
  93. def get_hash():
  94. return _TaipyBase._HOLDER_PREFIX + "Sc"
  95. class _GuiCoreScenarioDagAdapter(_TaipyBase):
  96. @staticmethod
  97. def get_entity_type(node: t.Any):
  98. return DataNode.__name__ if isinstance(node.entity, DataNode) else node.type
  99. def get(self):
  100. data = super().get()
  101. if isinstance(data, Scenario):
  102. try:
  103. scenario = core_get(data.id)
  104. if scenario:
  105. dag = data._get_dag()
  106. nodes = dict()
  107. for id, node in dag.nodes.items():
  108. entityType = _GuiCoreScenarioDagAdapter.get_entity_type(node)
  109. cat = nodes.get(entityType)
  110. if cat is None:
  111. cat = dict()
  112. nodes[entityType] = cat
  113. cat[id] = {
  114. "name": node.entity.get_simple_label(),
  115. "type": node.entity.storage_type() if hasattr(node.entity, "storage_type") else None,
  116. }
  117. return [
  118. data.id,
  119. nodes,
  120. [
  121. (
  122. _GuiCoreScenarioDagAdapter.get_entity_type(e.src),
  123. e.src.entity.id,
  124. _GuiCoreScenarioDagAdapter.get_entity_type(e.dest),
  125. e.dest.entity.id,
  126. )
  127. for e in dag.edges
  128. ],
  129. ]
  130. except Exception as e:
  131. _warn(f"Access to scenario ({data.id if hasattr(data, 'id') else 'No_id'}) failed", e)
  132. return None
  133. @staticmethod
  134. def get_hash():
  135. return _TaipyBase._HOLDER_PREFIX + "ScG"
  136. class _GuiCoreScenarioNoUpdate(_TaipyBase, _DoNotUpdate):
  137. @staticmethod
  138. def get_hash():
  139. return _TaipyBase._HOLDER_PREFIX + "ScN"
  140. class _GuiCoreDatanodeAdapter(_TaipyBase):
  141. __INNER_PROPS = ["name"]
  142. def get(self):
  143. data = super().get()
  144. if isinstance(data, DataNode):
  145. try:
  146. datanode = core_get(data.id)
  147. if datanode:
  148. owner = core_get(datanode.owner_id) if datanode.owner_id else None
  149. return [
  150. datanode.id,
  151. datanode.storage_type() if hasattr(datanode, "storage_type") else "",
  152. datanode.config_id,
  153. f"{datanode.last_edit_date}" if datanode.last_edit_date else "",
  154. f"{datanode.expiration_date}" if datanode.last_edit_date else "",
  155. datanode.get_simple_label(),
  156. datanode.owner_id or "",
  157. owner.get_simple_label() if owner else "GLOBAL",
  158. _EntityType.CYCLE.value
  159. if isinstance(owner, Cycle)
  160. else _EntityType.SCENARIO.value
  161. if isinstance(owner, Scenario)
  162. else -1,
  163. [
  164. (k, f"{v}")
  165. for k, v in datanode._get_user_properties().items()
  166. if k not in _GuiCoreDatanodeAdapter.__INNER_PROPS
  167. ],
  168. datanode._edit_in_progress,
  169. datanode._editor_id,
  170. is_readable(datanode),
  171. is_editable(datanode),
  172. ]
  173. except Exception as e:
  174. _warn(f"Access to datanode ({data.id if hasattr(data, 'id') else 'No_id'}) failed", e)
  175. return None
  176. @staticmethod
  177. def get_hash():
  178. return _TaipyBase._HOLDER_PREFIX + "Dn"