taipy.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 os
  12. import pathlib
  13. import shutil
  14. import tempfile
  15. from datetime import datetime
  16. from typing import Any, Callable, Dict, List, Literal, Optional, Set, Type, Union, overload
  17. from taipy.config import Scope
  18. from taipy.logger._taipy_logger import _TaipyLogger
  19. from ._core import Core
  20. from ._entity._entity import _Entity
  21. from ._manager._manager import _Manager
  22. from ._version._version_manager_factory import _VersionManagerFactory
  23. from .common._check_instance import (
  24. _is_cycle,
  25. _is_data_node,
  26. _is_job,
  27. _is_scenario,
  28. _is_sequence,
  29. _is_submission,
  30. _is_task,
  31. )
  32. from .common._warnings import _warn_deprecated, _warn_no_core_service
  33. from .config.data_node_config import DataNodeConfig
  34. from .config.scenario_config import ScenarioConfig
  35. from .cycle._cycle_manager_factory import _CycleManagerFactory
  36. from .cycle.cycle import Cycle
  37. from .cycle.cycle_id import CycleId
  38. from .data._data_manager_factory import _DataManagerFactory
  39. from .data.data_node import DataNode
  40. from .data.data_node_id import DataNodeId
  41. from .exceptions.exceptions import (
  42. DataNodeConfigIsNotGlobal,
  43. ExportPathAlreadyExists,
  44. ModelNotFound,
  45. NonExistingVersion,
  46. VersionIsNotProductionVersion,
  47. )
  48. from .job._job_manager_factory import _JobManagerFactory
  49. from .job.job import Job
  50. from .job.job_id import JobId
  51. from .reason._reason_factory import _build_not_submittable_entity_reason, _build_wrong_config_type_reason
  52. from .reason.reason import Reasons
  53. from .scenario._scenario_manager_factory import _ScenarioManagerFactory
  54. from .scenario.scenario import Scenario
  55. from .scenario.scenario_id import ScenarioId
  56. from .sequence._sequence_manager_factory import _SequenceManagerFactory
  57. from .sequence.sequence import Sequence
  58. from .sequence.sequence_id import SequenceId
  59. from .submission._submission_manager_factory import _SubmissionManagerFactory
  60. from .submission.submission import Submission, SubmissionId
  61. from .task._task_manager_factory import _TaskManagerFactory
  62. from .task.task import Task
  63. from .task.task_id import TaskId
  64. __logger = _TaipyLogger._get_logger()
  65. def set(entity: Union[DataNode, Task, Sequence, Scenario, Cycle, Submission]):
  66. """Save or update an entity.
  67. This function allows you to save or update an entity in Taipy.
  68. Parameters:
  69. entity (Union[DataNode^, Task^, Sequence^, Scenario^, Cycle^, Submission^]): The
  70. entity to save or update.
  71. """
  72. if isinstance(entity, Cycle):
  73. return _CycleManagerFactory._build_manager()._set(entity)
  74. if isinstance(entity, Scenario):
  75. return _ScenarioManagerFactory._build_manager()._set(entity)
  76. if isinstance(entity, Sequence):
  77. return _SequenceManagerFactory._build_manager()._set(entity)
  78. if isinstance(entity, Task):
  79. return _TaskManagerFactory._build_manager()._set(entity)
  80. if isinstance(entity, DataNode):
  81. return _DataManagerFactory._build_manager()._set(entity)
  82. if isinstance(entity, Submission):
  83. return _SubmissionManagerFactory._build_manager()._set(entity)
  84. def is_submittable(entity: Union[Scenario, ScenarioId, Sequence, SequenceId, Task, TaskId, str]) -> Reasons:
  85. """Indicate if an entity can be submitted.
  86. This function checks if the given entity can be submitted for execution.
  87. Returns:
  88. True if the given entity can be submitted. False otherwise.
  89. """
  90. if isinstance(entity, Scenario):
  91. return _ScenarioManagerFactory._build_manager()._is_submittable(entity)
  92. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  93. return _ScenarioManagerFactory._build_manager()._is_submittable(ScenarioId(entity))
  94. if isinstance(entity, Sequence):
  95. return _SequenceManagerFactory._build_manager()._is_submittable(entity)
  96. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  97. return _SequenceManagerFactory._build_manager()._is_submittable(SequenceId(entity))
  98. if isinstance(entity, Task):
  99. return _TaskManagerFactory._build_manager()._is_submittable(entity)
  100. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  101. return _TaskManagerFactory._build_manager()._is_submittable(TaskId(entity))
  102. return Reasons(str(entity))._add_reason(str(entity), _build_not_submittable_entity_reason(str(entity)))
  103. def is_editable(
  104. entity: Union[
  105. DataNode,
  106. Task,
  107. Job,
  108. Sequence,
  109. Scenario,
  110. Cycle,
  111. Submission,
  112. DataNodeId,
  113. TaskId,
  114. JobId,
  115. SequenceId,
  116. ScenarioId,
  117. CycleId,
  118. SubmissionId,
  119. ],
  120. ) -> bool:
  121. """Indicate if an entity can be edited.
  122. This function checks if the given entity can be edited.
  123. Returns:
  124. True if the given entity can be edited. False otherwise.
  125. """
  126. if isinstance(entity, Cycle):
  127. return _CycleManagerFactory._build_manager()._is_editable(entity)
  128. if isinstance(entity, str) and entity.startswith(Cycle._ID_PREFIX):
  129. return _CycleManagerFactory._build_manager()._is_editable(CycleId(entity))
  130. if isinstance(entity, Scenario):
  131. return _ScenarioManagerFactory._build_manager()._is_editable(entity)
  132. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  133. return _ScenarioManagerFactory._build_manager()._is_editable(ScenarioId(entity))
  134. if isinstance(entity, Sequence):
  135. return _SequenceManagerFactory._build_manager()._is_editable(entity)
  136. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  137. return _SequenceManagerFactory._build_manager()._is_editable(SequenceId(entity))
  138. if isinstance(entity, Task):
  139. return _TaskManagerFactory._build_manager()._is_editable(entity)
  140. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  141. return _TaskManagerFactory._build_manager()._is_editable(TaskId(entity))
  142. if isinstance(entity, Job):
  143. return _JobManagerFactory._build_manager()._is_editable(entity)
  144. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  145. return _JobManagerFactory._build_manager()._is_editable(JobId(entity))
  146. if isinstance(entity, DataNode):
  147. return _DataManagerFactory._build_manager()._is_editable(entity)
  148. if isinstance(entity, str) and entity.startswith(DataNode._ID_PREFIX):
  149. return _DataManagerFactory._build_manager()._is_editable(DataNodeId(entity))
  150. if isinstance(entity, Submission):
  151. return _SubmissionManagerFactory._build_manager()._is_editable(entity)
  152. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  153. return _SubmissionManagerFactory._build_manager()._is_editable(SequenceId(entity))
  154. return False
  155. def is_readable(
  156. entity: Union[
  157. DataNode,
  158. Task,
  159. Job,
  160. Sequence,
  161. Scenario,
  162. Cycle,
  163. Submission,
  164. DataNodeId,
  165. TaskId,
  166. JobId,
  167. SequenceId,
  168. ScenarioId,
  169. CycleId,
  170. SubmissionId,
  171. ],
  172. ) -> bool:
  173. """Indicate if an entity can be read.
  174. This function checks if the given entity can be read.
  175. Returns:
  176. True if the given entity can be read. False otherwise.
  177. """
  178. if isinstance(entity, Cycle):
  179. return _CycleManagerFactory._build_manager()._is_readable(entity)
  180. if isinstance(entity, str) and entity.startswith(Cycle._ID_PREFIX):
  181. return _CycleManagerFactory._build_manager()._is_readable(CycleId(entity))
  182. if isinstance(entity, Scenario):
  183. return _ScenarioManagerFactory._build_manager()._is_readable(entity)
  184. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  185. return _ScenarioManagerFactory._build_manager()._is_readable(ScenarioId(entity))
  186. if isinstance(entity, Sequence):
  187. return _SequenceManagerFactory._build_manager()._is_readable(entity)
  188. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  189. return _SequenceManagerFactory._build_manager()._is_readable(SequenceId(entity))
  190. if isinstance(entity, Task):
  191. return _TaskManagerFactory._build_manager()._is_readable(entity)
  192. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  193. return _TaskManagerFactory._build_manager()._is_readable(TaskId(entity))
  194. if isinstance(entity, Job):
  195. return _JobManagerFactory._build_manager()._is_readable(entity)
  196. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  197. return _JobManagerFactory._build_manager()._is_readable(JobId(entity))
  198. if isinstance(entity, DataNode):
  199. return _DataManagerFactory._build_manager()._is_readable(entity)
  200. if isinstance(entity, str) and entity.startswith(DataNode._ID_PREFIX):
  201. return _DataManagerFactory._build_manager()._is_readable(DataNodeId(entity))
  202. if isinstance(entity, Submission):
  203. return _SubmissionManagerFactory._build_manager()._is_readable(entity)
  204. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  205. return _SubmissionManagerFactory._build_manager()._is_readable(SequenceId(entity))
  206. return False
  207. @_warn_no_core_service("The submitted entity will not be executed until the Core service is running.")
  208. def submit(
  209. entity: Union[Scenario, Sequence, Task],
  210. force: bool = False,
  211. wait: bool = False,
  212. timeout: Optional[Union[float, int]] = None,
  213. **properties,
  214. ) -> Submission:
  215. """Submit a scenario, sequence or task entity for execution.
  216. This function submits the given entity for execution and returns the created job(s).
  217. If the entity is a sequence or a scenario, all the tasks of the entity are
  218. submitted for execution.
  219. Parameters:
  220. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to submit.
  221. force (bool): If True, the execution is forced even if for skippable tasks.
  222. wait (bool): Wait for the orchestrated jobs created from the submission to be finished
  223. in asynchronous mode.
  224. timeout (Union[float, int]): The optional maximum number of seconds to wait
  225. for the jobs to be finished before returning.
  226. **properties (dict[str, any]): A key-worded variable length list of user additional arguments
  227. that will be stored within the `Submission^`. It can be accessed via `Submission.properties^`.
  228. Returns:
  229. The created `Submission^` containing the information about the submission.
  230. """
  231. if isinstance(entity, Scenario):
  232. return _ScenarioManagerFactory._build_manager()._submit(
  233. entity, force=force, wait=wait, timeout=timeout, **properties
  234. )
  235. if isinstance(entity, Sequence):
  236. return _SequenceManagerFactory._build_manager()._submit(
  237. entity, force=force, wait=wait, timeout=timeout, **properties
  238. )
  239. if isinstance(entity, Task):
  240. return _TaskManagerFactory._build_manager()._submit(
  241. entity, force=force, wait=wait, timeout=timeout, **properties
  242. )
  243. return None
  244. @overload
  245. def exists(entity_id: TaskId) -> bool:
  246. ...
  247. @overload
  248. def exists(entity_id: DataNodeId) -> bool:
  249. ...
  250. @overload
  251. def exists(entity_id: SequenceId) -> bool:
  252. ...
  253. @overload
  254. def exists(entity_id: ScenarioId) -> bool:
  255. ...
  256. @overload
  257. def exists(entity_id: CycleId) -> bool:
  258. ...
  259. @overload
  260. def exists(entity_id: JobId) -> bool:
  261. ...
  262. @overload
  263. def exists(entity_id: SubmissionId) -> bool:
  264. ...
  265. @overload
  266. def exists(entity_id: str) -> bool:
  267. ...
  268. def exists(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str]) -> bool:
  269. """Check if an entity with the specified identifier exists.
  270. This function checks if an entity with the given identifier exists.
  271. It supports various types of entity identifiers, including `TaskId^`,
  272. `DataNodeId^`, `SequenceId^`, `ScenarioId^`, `JobId^`, `CycleId^`, `SubmissionId^`, and string
  273. representations.
  274. Parameters:
  275. entity_id (Union[DataNodeId^, TaskId^, SequenceId^, ScenarioId^, JobId^, CycleId^, SubmissionId^, str]): The
  276. identifier of the entity to check for existence.
  277. Returns:
  278. True if the given entity exists. False otherwise.
  279. Raises:
  280. ModelNotFound: If the entity's type cannot be determined.
  281. Note:
  282. The function performs checks for various entity types
  283. (`Job^`, `Cycle^`, `Scenario^`, `Sequence^`, `Task^`, `DataNode^`, `Submission^`)
  284. based on their respective identifier prefixes.
  285. """
  286. if _is_job(entity_id):
  287. return _JobManagerFactory._build_manager()._exists(JobId(entity_id))
  288. if _is_cycle(entity_id):
  289. return _CycleManagerFactory._build_manager()._exists(CycleId(entity_id))
  290. if _is_scenario(entity_id):
  291. return _ScenarioManagerFactory._build_manager()._exists(ScenarioId(entity_id))
  292. if _is_sequence(entity_id):
  293. return _SequenceManagerFactory._build_manager()._exists(SequenceId(entity_id))
  294. if _is_task(entity_id):
  295. return _TaskManagerFactory._build_manager()._exists(TaskId(entity_id))
  296. if _is_data_node(entity_id):
  297. return _DataManagerFactory._build_manager()._exists(DataNodeId(entity_id))
  298. if _is_submission(entity_id):
  299. return _SubmissionManagerFactory._build_manager()._exists(SubmissionId(entity_id))
  300. raise ModelNotFound("NOT_DETERMINED", entity_id)
  301. @overload
  302. def get(entity_id: TaskId) -> Task:
  303. ...
  304. @overload
  305. def get(entity_id: DataNodeId) -> DataNode:
  306. ...
  307. @overload
  308. def get(entity_id: SequenceId) -> Sequence:
  309. ...
  310. @overload
  311. def get(entity_id: ScenarioId) -> Scenario:
  312. ...
  313. @overload
  314. def get(entity_id: CycleId) -> Cycle:
  315. ...
  316. @overload
  317. def get(entity_id: JobId) -> Job:
  318. ...
  319. @overload
  320. def get(entity_id: SubmissionId) -> Submission:
  321. ...
  322. @overload
  323. def get(entity_id: str) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  324. ...
  325. def get(
  326. entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str],
  327. ) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  328. """Retrieve an entity by its unique identifier.
  329. This function allows you to retrieve an entity by specifying its identifier.
  330. The identifier must match the pattern of one of the supported entity types:
  331. `Task^`, `DataNode^`, `Sequence^`, `Job^`, `Cycle^`, `Submission^`, or `Scenario^`.
  332. Parameters:
  333. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, str]):
  334. The identifier of the entity to retrieve.<br/>
  335. It should conform to the identifier pattern of one of the entities (`Task^`, `DataNode^`,
  336. `Sequence^`, `Job^`, `Cycle^` or `Scenario^`).
  337. Returns:
  338. The entity that corresponds to the provided identifier. Returns None if no matching entity is found.
  339. Raises:
  340. ModelNotFound^: If the provided *entity_id* does not match any known entity pattern.
  341. """
  342. if _is_job(entity_id):
  343. return _JobManagerFactory._build_manager()._get(JobId(entity_id))
  344. if _is_cycle(entity_id):
  345. return _CycleManagerFactory._build_manager()._get(CycleId(entity_id))
  346. if _is_scenario(entity_id):
  347. return _ScenarioManagerFactory._build_manager()._get(ScenarioId(entity_id))
  348. if _is_sequence(entity_id):
  349. return _SequenceManagerFactory._build_manager()._get(SequenceId(entity_id))
  350. if _is_task(entity_id):
  351. return _TaskManagerFactory._build_manager()._get(TaskId(entity_id))
  352. if _is_data_node(entity_id):
  353. return _DataManagerFactory._build_manager()._get(DataNodeId(entity_id))
  354. if _is_submission(entity_id):
  355. return _SubmissionManagerFactory._build_manager()._get(SubmissionId(entity_id))
  356. raise ModelNotFound("NOT_DETERMINED", entity_id)
  357. def get_tasks() -> List[Task]:
  358. """Retrieve a list of all existing tasks.
  359. This function returns a list of all tasks that currently exist.
  360. Returns:
  361. A list containing all the tasks.
  362. """
  363. return _TaskManagerFactory._build_manager()._get_all()
  364. def is_deletable(entity: Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]) -> bool:
  365. """Check if a `Scenario^`, a `Job^` or a `Submission^` can be deleted.
  366. This function determines whether a scenario or a job can be safely
  367. deleted without causing conflicts or issues.
  368. Parameters:
  369. entity (Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]): The scenario,
  370. job or submission to check.
  371. Returns:
  372. True if the given scenario, job or submission can be deleted. False otherwise.
  373. """
  374. if isinstance(entity, Job):
  375. return _JobManagerFactory._build_manager()._is_deletable(entity)
  376. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  377. return _JobManagerFactory._build_manager()._is_deletable(JobId(entity))
  378. if isinstance(entity, Scenario):
  379. return _ScenarioManagerFactory._build_manager()._is_deletable(entity)
  380. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  381. return _ScenarioManagerFactory._build_manager()._is_deletable(ScenarioId(entity))
  382. if isinstance(entity, Submission):
  383. return _SubmissionManagerFactory._build_manager()._is_deletable(entity)
  384. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  385. return _SubmissionManagerFactory._build_manager()._is_deletable(SubmissionId(entity))
  386. return True
  387. def delete(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId]):
  388. """Delete an entity and its nested entities.
  389. This function deletes the specified entity and recursively deletes all its nested entities.
  390. The behavior varies depending on the type of entity provided:
  391. - If a `CycleId` is provided, the nested scenarios, tasks, data nodes, and jobs are deleted.
  392. - If a `ScenarioId` is provided, the nested sequences, tasks, data nodes, submissions and jobs are deleted.
  393. If the scenario is primary, it can only be deleted if it is the only scenario in the cycle.
  394. In that case, its cycle is also deleted. Use the `is_deletable()^` function to check if
  395. the scenario can be deleted.
  396. - If a `SequenceId` is provided, the related jobs are deleted.
  397. - If a `TaskId` is provided, the related data nodes, and jobs are deleted.
  398. - If a `DataNodeId` is provided, the data node is deleted.
  399. - If a `SubmissionId^` is provided, the related jobs are deleted.
  400. The submission can only be deleted if the execution has been finished.
  401. - If a `JobId^` is provided, the job entity can only be deleted if the execution has been finished.
  402. Parameters:
  403. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, SubmissionId, JobId, CycleId]):
  404. The identifier of the entity to delete.
  405. Raises:
  406. ModelNotFound: No entity corresponds to the specified *entity_id*.
  407. """
  408. if _is_job(entity_id):
  409. job_manager = _JobManagerFactory._build_manager()
  410. return job_manager._delete(job_manager._get(JobId(entity_id)))
  411. if _is_cycle(entity_id):
  412. return _CycleManagerFactory._build_manager()._hard_delete(CycleId(entity_id))
  413. if _is_scenario(entity_id):
  414. return _ScenarioManagerFactory._build_manager()._hard_delete(ScenarioId(entity_id))
  415. if _is_sequence(entity_id):
  416. return _SequenceManagerFactory._build_manager()._hard_delete(SequenceId(entity_id))
  417. if _is_task(entity_id):
  418. return _TaskManagerFactory._build_manager()._hard_delete(TaskId(entity_id))
  419. if _is_data_node(entity_id):
  420. return _DataManagerFactory._build_manager()._delete(DataNodeId(entity_id))
  421. if _is_submission(entity_id):
  422. return _SubmissionManagerFactory._build_manager()._hard_delete(SubmissionId(entity_id))
  423. raise ModelNotFound("NOT_DETERMINED", entity_id)
  424. def get_scenarios(
  425. cycle: Optional[Cycle] = None,
  426. tag: Optional[str] = None,
  427. is_sorted: bool = False,
  428. descending: bool = False,
  429. sort_key: Literal["name", "id", "config_id", "creation_date", "tags"] = "name",
  430. ) -> List[Scenario]:
  431. """Retrieve a list of existing scenarios filtered by cycle or tag.
  432. This function allows you to retrieve a list of scenarios based on optional
  433. filtering criteria. If both a *cycle* and a *tag* are provided, the returned
  434. list contains scenarios that belong to the specified *cycle* and also
  435. have the specified *tag*.
  436. Parameters:
  437. cycle (Optional[Cycle^]): The optional `Cycle^` to filter scenarios by.
  438. tag (Optional[str]): The optional tag to filter scenarios by.
  439. is_sorted (bool): If True, sort the output list of scenarios using the sorting key.
  440. The default value is False.
  441. descending (bool): If True, sort the output list of scenarios in descending order.
  442. The default value is False.
  443. sort_key (Literal["name", "id", "creation_date", "tags"]): The optional sort_key to
  444. decide upon what key scenarios are sorted. The sorting is in increasing order for
  445. dates, in alphabetical order for name and id, and in lexicographical order for tags.
  446. The default value is "name".<br/>
  447. If an incorrect sorting key is provided, the scenarios are sorted by name.
  448. Returns:
  449. The list of scenarios filtered by cycle or tag.
  450. """
  451. scenario_manager = _ScenarioManagerFactory._build_manager()
  452. if not cycle and not tag:
  453. scenarios = scenario_manager._get_all()
  454. elif cycle and not tag:
  455. scenarios = scenario_manager._get_all_by_cycle(cycle)
  456. elif not cycle and tag:
  457. scenarios = scenario_manager._get_all_by_tag(tag)
  458. elif cycle and tag:
  459. cycles_scenarios = scenario_manager._get_all_by_cycle(cycle)
  460. scenarios = [scenario for scenario in cycles_scenarios if scenario.has_tag(tag)]
  461. else:
  462. scenarios = []
  463. if is_sorted:
  464. scenario_manager._sort_scenarios(scenarios, descending, sort_key)
  465. return scenarios
  466. def get_primary(cycle: Cycle) -> Optional[Scenario]:
  467. """Retrieve the primary scenario associated with a cycle.
  468. Parameters:
  469. cycle (Cycle^): The cycle for which to retrieve the primary scenario.
  470. Returns:
  471. The primary scenario of the given _cycle_. If the cycle has no
  472. primary scenario, this method returns None.
  473. """
  474. return _ScenarioManagerFactory._build_manager()._get_primary(cycle)
  475. def get_primary_scenarios(
  476. is_sorted: bool = False,
  477. descending: bool = False,
  478. sort_key: Literal["name", "id", "config_id", "creation_date", "tags"] = "name",
  479. ) -> List[Scenario]:
  480. """Retrieve a list of all primary scenarios.
  481. Parameters:
  482. is_sorted (bool): If True, sort the output list of scenarios using the sorting key.
  483. The default value is False.
  484. descending (bool): If True, sort the output list of scenarios in descending order.
  485. The default value is False.
  486. sort_key (Literal["name", "id", "creation_date", "tags"]): The optional sort_key to
  487. decide upon what key scenarios are sorted. The sorting is in increasing order for
  488. dates, in alphabetical order for name and id, and in lexicographical order for tags.
  489. The default value is "name".<br/>
  490. If an incorrect sorting key is provided, the scenarios are sorted by name.
  491. Returns:
  492. A list contains all primary scenarios.
  493. """
  494. scenario_manager = _ScenarioManagerFactory._build_manager()
  495. scenarios = scenario_manager._get_primary_scenarios()
  496. if is_sorted:
  497. scenario_manager._sort_scenarios(scenarios, descending, sort_key)
  498. return scenarios
  499. def is_promotable(scenario: Union[Scenario, ScenarioId]) -> bool:
  500. """Determine if a scenario can be promoted to become a primary scenario.
  501. This function checks whether the given scenario is eligible to be promoted
  502. as a primary scenario.
  503. Parameters:
  504. scenario (Union[Scenario, ScenarioId]): The scenario to be evaluated for promotion.
  505. Returns:
  506. True if the given scenario can be promoted to be a primary scenario. False otherwise.
  507. """
  508. return _ScenarioManagerFactory._build_manager()._is_promotable_to_primary(scenario)
  509. def set_primary(scenario: Scenario):
  510. """Promote a scenario as the primary scenario of its cycle.
  511. This function promotes the given scenario as the primary scenario of its associated cycle.
  512. If the cycle already has a primary scenario, that scenario is demoted and is
  513. no longer considered the primary scenario for its cycle.
  514. Parameters:
  515. scenario (Scenario^): The scenario to promote as the new _primary_ scenario.
  516. """
  517. return _ScenarioManagerFactory._build_manager()._set_primary(scenario)
  518. def tag(scenario: Scenario, tag: str):
  519. """Add a tag to a scenario.
  520. This function adds a user-defined tag to the specified scenario. If another scenario
  521. within the same cycle already has the same tag applied, the previous scenario is untagged.
  522. Parameters:
  523. scenario (Scenario^): The scenario to which the tag will be added.
  524. tag (str): The tag to apply to the scenario.
  525. """
  526. return _ScenarioManagerFactory._build_manager()._tag(scenario, tag)
  527. def untag(scenario: Scenario, tag: str):
  528. """Remove a tag from a scenario.
  529. This function removes a specified tag from the given scenario. If the scenario does
  530. not have the specified tag, it has no effect.
  531. Parameters:
  532. scenario (Scenario^): The scenario from which the tag will be removed.
  533. tag (str): The tag to remove from the scenario.
  534. """
  535. return _ScenarioManagerFactory._build_manager()._untag(scenario, tag)
  536. def compare_scenarios(*scenarios: Scenario, data_node_config_id: Optional[str] = None) -> Dict[str, Any]:
  537. """Compare the data nodes of several scenarios.
  538. You can specify which data node config identifier should the comparison be performed
  539. on.
  540. Parameters:
  541. *scenarios (*Scenario^): The list of the scenarios to compare.
  542. data_node_config_id (Optional[str]): The config identifier of the DataNode to perform
  543. the comparison on. <br/>
  544. If _data_node_config_id_ is not provided, the scenarios are
  545. compared on all defined comparators.<br/>
  546. Returns:
  547. The comparison results. The key is the data node config identifier used for
  548. comparison.
  549. Raises:
  550. InsufficientScenarioToCompare^: Raised when only one or no scenario for comparison
  551. is provided.
  552. NonExistingComparator^: Raised when the scenario comparator does not exist.
  553. DifferentScenarioConfigs^: Raised when the provided scenarios do not share the
  554. same scenario config.
  555. NonExistingScenarioConfig^: Raised when the scenario config of the provided
  556. scenarios could not be found.
  557. """
  558. return _ScenarioManagerFactory._build_manager()._compare(*scenarios, data_node_config_id=data_node_config_id)
  559. def subscribe_scenario(
  560. callback: Callable[[Scenario, Job], None],
  561. params: Optional[List[Any]] = None,
  562. scenario: Optional[Scenario] = None,
  563. ):
  564. """Subscribe a function to be called on job status change.
  565. The subscription is applied to all jobs created for the execution of _scenario_.
  566. If no scenario is provided, the subscription applies to all scenarios.
  567. Parameters:
  568. callback (Callable[[Scenario^, Job^], None]): The function to be called on
  569. status change.
  570. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  571. scenario (Optional[Scenario^]): The scenario to which the callback is applied.
  572. If None, the subscription is registered for all scenarios.
  573. Note:
  574. Notifications are applied only for jobs created **after** this subscription.
  575. """
  576. params = [] if params is None else params
  577. return _ScenarioManagerFactory._build_manager()._subscribe(callback, params, scenario)
  578. def unsubscribe_scenario(
  579. callback: Callable[[Scenario, Job], None], params: Optional[List[Any]] = None, scenario: Optional[Scenario] = None
  580. ):
  581. """Unsubscribe a function that is called when the status of a `Job^` changes.
  582. If no scenario is provided, the subscription is removed for all scenarios.
  583. Parameters:
  584. callback (Callable[[Scenario^, Job^], None]): The function to unsubscribe from.
  585. params (Optional[List[Any]]): The parameters to be passed to the callback.
  586. scenario (Optional[Scenario]): The scenario to unsubscribe from. If None, it
  587. applies to all scenarios.
  588. Note:
  589. The callback function will continue to be called for ongoing jobs.
  590. """
  591. return _ScenarioManagerFactory._build_manager()._unsubscribe(callback, params, scenario)
  592. def subscribe_sequence(
  593. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  594. ):
  595. """Subscribe a function to be called on job status change.
  596. The subscription is applied to all jobs created for the execution of _sequence_.
  597. Parameters:
  598. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  599. status change.
  600. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  601. sequence (Optional[Sequence^]): The sequence to subscribe on. If None, the subscription
  602. is applied to all sequences.
  603. Note:
  604. Notifications are applied only for jobs created **after** this subscription.
  605. """
  606. return _SequenceManagerFactory._build_manager()._subscribe(callback, params, sequence)
  607. def unsubscribe_sequence(
  608. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  609. ):
  610. """Unsubscribe a function that is called when the status of a Job changes.
  611. Parameters:
  612. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  613. status change.
  614. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  615. sequence (Optional[Sequence^]): The sequence to unsubscribe to. If None, it applies
  616. to all sequences.
  617. Note:
  618. The function will continue to be called for ongoing jobs.
  619. """
  620. return _SequenceManagerFactory._build_manager()._unsubscribe(callback, params, sequence)
  621. def get_sequences() -> List[Sequence]:
  622. """Return all existing sequences.
  623. Returns:
  624. The list of all sequences.
  625. """
  626. return _SequenceManagerFactory._build_manager()._get_all()
  627. def get_jobs() -> List[Job]:
  628. """Return all the existing jobs.
  629. Returns:
  630. The list of all jobs.
  631. """
  632. return _JobManagerFactory._build_manager()._get_all()
  633. def get_submissions() -> List[Submission]:
  634. """Return all the existing submissions.
  635. Returns:
  636. The list of all submissions.
  637. """
  638. return _SubmissionManagerFactory._build_manager()._get_all()
  639. def delete_job(job: Job, force: Optional[bool] = False):
  640. """Delete a job.
  641. This function deletes the specified job. If the job is not completed and
  642. *force* is not set to True, a `JobNotDeletedException^` may be raised.
  643. Parameters:
  644. job (Job^): The job to delete.
  645. force (Optional[bool]): If True, forces the deletion of _job_, even
  646. if it is not completed yet.
  647. Raises:
  648. JobNotDeletedException^: If the job is not finished.
  649. """
  650. return _JobManagerFactory._build_manager()._delete(job, force)
  651. def delete_jobs():
  652. """Delete all jobs."""
  653. return _JobManagerFactory._build_manager()._delete_all()
  654. def cancel_job(job: Union[str, Job]):
  655. """Cancel a job and set the status of the subsequent jobs to ABANDONED.
  656. This function cancels the specified job and sets the status of any subsequent jobs to ABANDONED.
  657. Parameters:
  658. job (Job^): The job to cancel.
  659. """
  660. _JobManagerFactory._build_manager()._cancel(job)
  661. def get_latest_job(task: Task) -> Optional[Job]:
  662. """Return the latest job of a task.
  663. This function retrieves the latest job associated with a task.
  664. Parameters:
  665. task (Task^): The task to retrieve the latest job from.
  666. Returns:
  667. The latest job created from _task_, or None if no job has been created from _task_.
  668. """
  669. return _JobManagerFactory._build_manager()._get_latest(task)
  670. def get_latest_submission(entity: Union[Scenario, Sequence, Task]) -> Optional[Submission]:
  671. """Return the latest submission of a scenario, sequence or task.
  672. This function retrieves the latest submission associated with a scenario, sequence or task.
  673. Parameters:
  674. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to
  675. retrieve the latest submission from.
  676. Returns:
  677. The latest submission created from _scenario_, _sequence_ and _task_, or None
  678. if no submission has been created from _scenario_, _sequence_ and _task_.
  679. """
  680. return _SubmissionManagerFactory._build_manager()._get_latest(entity)
  681. def get_data_nodes() -> List[DataNode]:
  682. """Return all the existing data nodes.
  683. Returns:
  684. The list of all data nodes.
  685. """
  686. return _DataManagerFactory._build_manager()._get_all()
  687. def get_cycles() -> List[Cycle]:
  688. """Return the list of all existing cycles.
  689. Returns:
  690. The list of all cycles.
  691. """
  692. return _CycleManagerFactory._build_manager()._get_all()
  693. def can_create(config: Union[ScenarioConfig, DataNodeConfig]) -> Reasons:
  694. """Indicate if a config can be created.
  695. This function checks if the given scenario or data node config can be created.
  696. Returns:
  697. True if the given config can be created. False otherwise.
  698. """
  699. if isinstance(config, ScenarioConfig):
  700. return _ScenarioManagerFactory._build_manager()._can_create(config)
  701. elif isinstance(config, DataNodeConfig):
  702. return _DataManagerFactory._build_manager()._can_create(config)
  703. else:
  704. config_id = getattr(config, "id", None) or str(config)
  705. return Reasons(config_id)._add_reason(config_id, _build_wrong_config_type_reason(config_id))
  706. def create_scenario(
  707. config: ScenarioConfig,
  708. creation_date: Optional[datetime] = None,
  709. name: Optional[str] = None,
  710. ) -> Scenario:
  711. """Create and return a new scenario based on a scenario configuration.
  712. This function checks and locks the configuration, manages application's version,
  713. and creates a new scenario from the scenario configuration provided.
  714. If the scenario belongs to a cycle, the cycle (corresponding to the _creation_date_
  715. and the configuration frequency attribute) is created if it does not exist yet.
  716. Parameters:
  717. config (ScenarioConfig^): The scenario configuration used to create a new scenario.
  718. creation_date (Optional[datetime.datetime]): The creation date of the scenario.
  719. If None, the current date time is used.
  720. name (Optional[str]): The displayable name of the scenario.
  721. Returns:
  722. The new scenario.
  723. Raises:
  724. SystemExit: If the configuration check returns some errors.
  725. """
  726. Core._manage_version_and_block_config()
  727. return _ScenarioManagerFactory._build_manager()._create(config, creation_date, name)
  728. def create_global_data_node(config: DataNodeConfig) -> DataNode:
  729. """Create and return a new GLOBAL data node from a data node configuration.
  730. This function checks and locks the configuration, manages application's version,
  731. and creates the new data node from the data node configuration provided.
  732. Parameters:
  733. config (DataNodeConfig^): The data node configuration. It must have a `GLOBAL` scope.
  734. Returns:
  735. The new global data node.
  736. Raises:
  737. DataNodeConfigIsNotGlobal^: If the data node configuration does not have GLOBAL scope.
  738. SystemExit: If the configuration check returns some errors.
  739. """
  740. # Check if the data node config has GLOBAL scope
  741. if config.scope is not Scope.GLOBAL:
  742. raise DataNodeConfigIsNotGlobal(config.id)
  743. Core._manage_version_and_block_config()
  744. if dns := _DataManagerFactory._build_manager()._get_by_config_id(config.id):
  745. return dns[0]
  746. return _DataManagerFactory._build_manager()._create_and_set(config, None, None)
  747. def clean_all_entities_by_version(version_number=None) -> bool:
  748. """Deprecated. Use `clean_all_entities` function instead."""
  749. _warn_deprecated("'clean_all_entities_by_version'", suggest="the 'clean_all_entities' function")
  750. return clean_all_entities(version_number)
  751. def clean_all_entities(version_number: str) -> bool:
  752. """Deletes all entities associated with the specified version.
  753. Parameters:
  754. version_number (str): The version number of the entities to be deleted.
  755. The version_number should not be a production version.
  756. Returns:
  757. True if the operation succeeded, False otherwise.
  758. Notes:
  759. - If the specified version does not exist, the operation will be aborted, and False will be returned.
  760. - If the specified version is a production version, the operation will be aborted, and False will be returned.
  761. - This function cleans all entities, including jobs, submissions, scenarios, cycles, sequences, tasks,
  762. and data nodes.
  763. """
  764. version_manager = _VersionManagerFactory._build_manager()
  765. try:
  766. version_number = version_manager._replace_version_number(version_number)
  767. except NonExistingVersion as e:
  768. __logger.warning(f"{e.message} Abort cleaning the entities of version '{version_number}'.")
  769. return False
  770. if version_number in version_manager._get_production_versions():
  771. __logger.warning(
  772. f"Abort cleaning the entities of version '{version_number}'. A production version can not be deleted."
  773. )
  774. return False
  775. _JobManagerFactory._build_manager()._delete_by_version(version_number)
  776. _SubmissionManagerFactory._build_manager()._delete_by_version(version_number)
  777. _ScenarioManagerFactory._build_manager()._delete_by_version(version_number)
  778. _SequenceManagerFactory._build_manager()._delete_by_version(version_number)
  779. _TaskManagerFactory._build_manager()._delete_by_version(version_number)
  780. _DataManagerFactory._build_manager()._delete_by_version(version_number)
  781. version_manager._delete(version_number)
  782. try:
  783. version_manager._delete_production_version(version_number)
  784. except VersionIsNotProductionVersion:
  785. pass
  786. return True
  787. def export_scenario(
  788. scenario_id: ScenarioId,
  789. output_path: Union[str, pathlib.Path],
  790. override: bool = False,
  791. include_data: bool = False,
  792. ):
  793. """Export all related entities of a scenario to an archive zip file.
  794. This function exports all related entities of the specified scenario to the
  795. specified archive zip file.
  796. Parameters:
  797. scenario_id (ScenarioId): The ID of the scenario to export.
  798. output_path (Union[str, pathlib.Path]): The path to export the scenario to.
  799. The path should include the file name without the extension or with the `.zip` extension.
  800. If the path exists and the override parameter is False, an exception is raised.
  801. override (bool): If True, the existing folder will be overridden. The default value is False.
  802. include_data (bool): If True, the file-based data nodes are exported as well.
  803. This includes Pickle, CSV, Excel, Parquet, and JSON data nodes.
  804. If the scenario has a data node that is not file-based, a warning will be logged, and the data node
  805. will not be exported. The default value is False.
  806. Raises:
  807. ExportPathAlreadyExists^: If the `output_path` already exists and the override parameter is False.
  808. """
  809. manager = _ScenarioManagerFactory._build_manager()
  810. scenario = manager._get(scenario_id)
  811. entity_ids = manager._get_children_entity_ids(scenario)
  812. entity_ids.scenario_ids = {scenario_id}
  813. if scenario.cycle:
  814. entity_ids.cycle_ids = {scenario.cycle.id}
  815. output_filename = os.path.splitext(output_path)[0] if str(output_path).endswith(".zip") else str(output_path)
  816. output_zip_path = pathlib.Path(output_filename + ".zip")
  817. if output_zip_path.exists():
  818. if override:
  819. __logger.warning(f"Override the existing path '{output_zip_path}' to export scenario {scenario_id}.")
  820. output_zip_path.unlink()
  821. else:
  822. raise ExportPathAlreadyExists(str(output_zip_path), scenario_id)
  823. with tempfile.TemporaryDirectory() as tmp_dir:
  824. for data_node_id in entity_ids.data_node_ids:
  825. _DataManagerFactory._build_manager()._export(data_node_id, tmp_dir, include_data=include_data)
  826. for task_id in entity_ids.task_ids:
  827. _TaskManagerFactory._build_manager()._export(task_id, tmp_dir)
  828. for sequence_id in entity_ids.sequence_ids:
  829. _SequenceManagerFactory._build_manager()._export(sequence_id, tmp_dir)
  830. for cycle_id in entity_ids.cycle_ids:
  831. _CycleManagerFactory._build_manager()._export(cycle_id, tmp_dir)
  832. for scenario_id in entity_ids.scenario_ids:
  833. _ScenarioManagerFactory._build_manager()._export(scenario_id, tmp_dir)
  834. for job_id in entity_ids.job_ids:
  835. _JobManagerFactory._build_manager()._export(job_id, tmp_dir)
  836. for submission_id in entity_ids.submission_ids:
  837. _SubmissionManagerFactory._build_manager()._export(submission_id, tmp_dir)
  838. _VersionManagerFactory._build_manager()._export(scenario.version, tmp_dir)
  839. shutil.make_archive(output_filename, "zip", tmp_dir)
  840. def import_scenario(input_path: Union[str, pathlib.Path], override: bool = False) -> Optional[Scenario]:
  841. """Import from an archive zip file containing an exported scenario into the current Taipy application.
  842. The zip file should be created by the `taipy.import()^` method, which contains all related entities
  843. of the scenario.
  844. All entities should belong to the same version that is compatible with the current Taipy application version.
  845. Parameters:
  846. input_path (Union[str, pathlib.Path]): The path to the archive scenario to import.
  847. If the path doesn't exist, an exception is raised.
  848. override (bool): If True, override the entities if existed. The default value is False.
  849. Return:
  850. The imported scenario.
  851. Raises:
  852. FileNotFoundError: If the import path does not exist.
  853. ImportArchiveDoesntContainAnyScenario: If the unzip folder doesn't contain any scenario.
  854. ConflictedConfigurationError: If the configuration of the imported scenario is conflicted with the current one.
  855. """
  856. if isinstance(input_path, str):
  857. zip_file_path: pathlib.Path = pathlib.Path(input_path)
  858. else:
  859. zip_file_path = input_path
  860. if not zip_file_path.exists():
  861. raise FileNotFoundError(f"The import archive path '{zip_file_path}' does not exist.")
  862. entity_managers: Dict[str, Type[_Manager]] = {
  863. "cycles": _CycleManagerFactory._build_manager(),
  864. "cycle": _CycleManagerFactory._build_manager(),
  865. "data_nodes": _DataManagerFactory._build_manager(),
  866. "data_node": _DataManagerFactory._build_manager(),
  867. "tasks": _TaskManagerFactory._build_manager(),
  868. "task": _TaskManagerFactory._build_manager(),
  869. "scenarios": _ScenarioManagerFactory._build_manager(),
  870. "scenario": _ScenarioManagerFactory._build_manager(),
  871. "jobs": _JobManagerFactory._build_manager(),
  872. "job": _JobManagerFactory._build_manager(),
  873. "submission": _SubmissionManagerFactory._build_manager(),
  874. "version": _VersionManagerFactory._build_manager(),
  875. }
  876. return _ScenarioManagerFactory._build_manager()._import_scenario_and_children_entities(
  877. zip_file_path, override, entity_managers
  878. )
  879. def get_parents(
  880. entity: Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence], parent_dict=None
  881. ) -> Dict[str, Set[_Entity]]:
  882. """Get the parents of an entity from itself or its identifier.
  883. Parameters:
  884. entity (Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence]): The entity or its
  885. identifier to get the parents.
  886. Returns:
  887. The dictionary of all parent entities.
  888. They are grouped by their type (Scenario^, Sequences^, or tasks^) so each key corresponds
  889. to a level of the parents and the value is a set of the parent entities.
  890. An empty dictionary is returned if the entity does not have parents.<br/>
  891. Example: The following instruction returns all the scenarios that include the
  892. datanode identified by "my_datanode_id".
  893. `taipy.get_parents("id_of_my_datanode")["scenario"]`
  894. Raises:
  895. ModelNotFound^: If _entity_ does not match a correct entity pattern.
  896. """
  897. def update_parent_dict(parents_set, parent_dict):
  898. for k, value in parents_set.items():
  899. if k in parent_dict.keys():
  900. parent_dict[k].update(value)
  901. else:
  902. parent_dict[k] = value
  903. if isinstance(entity, str):
  904. entity = get(entity)
  905. parent_dict = parent_dict or {}
  906. if isinstance(entity, (Scenario, Cycle)):
  907. return parent_dict
  908. current_parent_dict: Dict[str, Set] = {}
  909. for parent in entity.parent_ids:
  910. parent_entity = get(parent)
  911. if parent_entity._MANAGER_NAME in current_parent_dict.keys():
  912. current_parent_dict[parent_entity._MANAGER_NAME].add(parent_entity)
  913. else:
  914. current_parent_dict[parent_entity._MANAGER_NAME] = {parent_entity}
  915. if isinstance(entity, Sequence):
  916. update_parent_dict(current_parent_dict, parent_dict)
  917. if isinstance(entity, Task):
  918. parent_entity_key_to_search_next = "scenario"
  919. update_parent_dict(current_parent_dict, parent_dict)
  920. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  921. get_parents(parent, parent_dict)
  922. if isinstance(entity, DataNode):
  923. parent_entity_key_to_search_next = "task"
  924. update_parent_dict(current_parent_dict, parent_dict)
  925. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  926. get_parents(parent, parent_dict)
  927. return parent_dict
  928. def get_cycles_scenarios() -> Dict[Optional[Cycle], List[Scenario]]:
  929. """Get the scenarios grouped by cycles.
  930. Returns:
  931. The dictionary of all cycles and their corresponding scenarios.
  932. """
  933. cycles_scenarios: Dict[Optional[Cycle], List[Scenario]] = {}
  934. for scenario in get_scenarios():
  935. if scenario.cycle in cycles_scenarios.keys():
  936. cycles_scenarios[scenario.cycle].append(scenario)
  937. else:
  938. cycles_scenarios[scenario.cycle] = [scenario]
  939. return cycles_scenarios
  940. def get_entities_by_config_id(
  941. config_id: str,
  942. ) -> Union[List, List[Task], List[DataNode], List[Sequence], List[Scenario]]:
  943. """Get the entities by its config id.
  944. Parameters:
  945. config_id (str): The config id of the entities
  946. Returns:
  947. The list of all entities by the config id.
  948. """
  949. entities: List = []
  950. if entities := _ScenarioManagerFactory._build_manager()._get_by_config_id(config_id):
  951. return entities
  952. if entities := _TaskManagerFactory._build_manager()._get_by_config_id(config_id):
  953. return entities
  954. if entities := _DataManagerFactory._build_manager()._get_by_config_id(config_id):
  955. return entities
  956. return entities