pickle.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 pickle
  12. from datetime import datetime, timedelta
  13. from typing import List, Optional, Set
  14. from taipy.config.common.scope import Scope
  15. from .._version._version_manager_factory import _VersionManagerFactory
  16. from ._file_datanode_mixin import _FileDataNodeMixin
  17. from .data_node import DataNode
  18. from .data_node_id import DataNodeId, Edit
  19. class PickleDataNode(DataNode, _FileDataNodeMixin):
  20. """Data Node stored as a pickle file.
  21. Attributes:
  22. config_id (str): Identifier of the data node configuration. It must be a valid Python
  23. identifier.
  24. scope (Scope^): The scope of this data node.
  25. id (str): The unique identifier of this data node.
  26. owner_id (str): The identifier of the owner (sequence_id, scenario_id, cycle_id) or
  27. `None`.
  28. parent_ids (Optional[Set[str]]): The identifiers of the parent tasks or `None`.
  29. last_edit_date (datetime): The date and time of the last modification.
  30. edits (List[Edit^]): The ordered list of edits for that job.
  31. version (str): The string indicates the application version of the data node to instantiate. If not provided,
  32. the current version is used.
  33. validity_period (Optional[timedelta]): The duration implemented as a timedelta since the last edit date for
  34. which the data node can be considered up-to-date. Once the validity period has passed, the data node is
  35. considered stale and relevant tasks will run even if they are skippable (see the
  36. [Task management page](../core/entities/task-mgt.md) for more details).
  37. If _validity_period_ is set to `None`, the data node is always up-to-date.
  38. edit_in_progress (bool): True if a task computing the data node has been submitted
  39. and not completed yet. False otherwise.
  40. editor_id (Optional[str]): The identifier of the user who is currently editing the data node.
  41. editor_expiration_date (Optional[datetime]): The expiration date of the editor lock.
  42. properties (dict[str, Any]): A dictionary of additional properties.
  43. When creating a pickle data node, if the _properties_ dictionary contains a
  44. _"default_data"_ entry, the data node is automatically written with the corresponding
  45. _"default_data"_ value.
  46. If the _properties_ dictionary contains a _"default_path"_ or _"path"_ entry, the data will be stored
  47. using the corresponding value as the name of the pickle file.
  48. """
  49. __STORAGE_TYPE = "pickle"
  50. _REQUIRED_PROPERTIES: List[str] = []
  51. def __init__(
  52. self,
  53. config_id: str,
  54. scope: Scope,
  55. id: Optional[DataNodeId] = None,
  56. owner_id: Optional[str] = None,
  57. parent_ids: Optional[Set[str]] = None,
  58. last_edit_date: Optional[datetime] = None,
  59. edits: Optional[List[Edit]] = None,
  60. version: str = None,
  61. validity_period: Optional[timedelta] = None,
  62. edit_in_progress: bool = False,
  63. editor_id: Optional[str] = None,
  64. editor_expiration_date: Optional[datetime] = None,
  65. properties=None,
  66. ):
  67. self.id = id or self._new_id(config_id)
  68. if properties is None:
  69. properties = {}
  70. default_value = properties.pop(self._DEFAULT_DATA_KEY, None)
  71. _FileDataNodeMixin.__init__(self, properties)
  72. DataNode.__init__(
  73. self,
  74. config_id,
  75. scope,
  76. self.id,
  77. owner_id,
  78. parent_ids,
  79. last_edit_date,
  80. edits,
  81. version or _VersionManagerFactory._build_manager()._get_latest_version(),
  82. validity_period,
  83. edit_in_progress,
  84. editor_id,
  85. editor_expiration_date,
  86. **properties,
  87. )
  88. self._write_default_data(default_value)
  89. self._TAIPY_PROPERTIES.update(
  90. {
  91. self._PATH_KEY,
  92. self._DEFAULT_PATH_KEY,
  93. self._DEFAULT_DATA_KEY,
  94. self._IS_GENERATED_KEY,
  95. }
  96. )
  97. @classmethod
  98. def storage_type(cls) -> str:
  99. return cls.__STORAGE_TYPE
  100. def _read(self):
  101. with open(self._path, "rb") as pf:
  102. return pickle.load(pf)
  103. def _write(self, data):
  104. with open(self._path, "wb") as pf:
  105. pickle.dump(data, pf)