common.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """
  2. Commonly used functionality
  3. """
  4. import time
  5. from pathlib import Path
  6. from typing import (
  7. Optional,
  8. Tuple,
  9. Union
  10. )
  11. from uuid import UUID
  12. from dataladmetadatamodel.datasettree import DatasetTree
  13. from dataladmetadatamodel.filetree import FileTree
  14. from dataladmetadatamodel.metadata import Metadata
  15. from dataladmetadatamodel.metadatapath import MetadataPath
  16. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  17. from dataladmetadatamodel.uuidset import UUIDSet
  18. from dataladmetadatamodel.versionlist import (
  19. TreeVersionList,
  20. VersionList
  21. )
  22. from dataladmetadatamodel.mapper.reference import Reference
  23. def _get_tree_version_list_reference(
  24. mapper_family: str,
  25. realm: str,
  26. location: str) -> Reference:
  27. return Reference(
  28. mapper_family,
  29. realm,
  30. "TreeVersionList",
  31. location)
  32. def _get_uuid_set_reference(
  33. mapper_family: str,
  34. realm: str,
  35. location: str) -> Reference:
  36. return Reference(
  37. mapper_family,
  38. realm,
  39. "UUIDSet",
  40. location)
  41. def get_top_level_metadata_objects(
  42. mapper_family: str,
  43. realm: Union[str, Path]
  44. ) -> Tuple[Optional[TreeVersionList], Optional[UUIDSet]]:
  45. """
  46. Load the two top-level metadata elements from a realm, i.e.
  47. the tree version list and the uuid list.
  48. We do this be creating references from known locations
  49. in the mapper family and loading the referenced objects.
  50. """
  51. from dataladmetadatamodel.mapper import (
  52. get_uuid_set_location,
  53. get_tree_version_list_location)
  54. tree_version_list = TreeVersionList(
  55. reference=_get_tree_version_list_reference(
  56. mapper_family,
  57. str(realm),
  58. get_tree_version_list_location(mapper_family)))
  59. uuid_set = UUIDSet(
  60. reference=_get_uuid_set_reference(
  61. mapper_family,
  62. str(realm),
  63. get_uuid_set_location(mapper_family)))
  64. try:
  65. return tree_version_list.read_in(), uuid_set.read_in()
  66. except RuntimeError:
  67. return None, None
  68. def get_top_nodes_and_metadata_root_record(
  69. mapper_family: str,
  70. realm: str,
  71. dataset_id: UUID,
  72. primary_data_version: str,
  73. dataset_tree_path: MetadataPath,
  74. auto_create: Optional[bool] = False
  75. ) -> Tuple[Optional[TreeVersionList], Optional[UUIDSet], Optional[MetadataRootRecord]]:
  76. """
  77. Return the top nodes and metadata root record for a given dataset id
  78. and a given dataset version.
  79. If auto_create is True, create missing entries for the given dataset
  80. version, down to and including the metadata root record with empty
  81. dataset metadata and an empty file-tree connected to it.
  82. If auto_create is False and any of the elements: uuid set, version tree
  83. list, or metadata root record does not exist, (None, None, None) is
  84. returned.
  85. If you want to preserve any changes, are created metadata objects,
  86. you have to save the returned objects and flush the realm
  87. """
  88. tree_version_list, uuid_set = get_top_level_metadata_objects(
  89. mapper_family,
  90. realm)
  91. if tree_version_list is None and auto_create:
  92. tree_version_list = TreeVersionList()
  93. if uuid_set is None and auto_create:
  94. uuid_set = UUIDSet()
  95. if uuid_set is None or tree_version_list is None:
  96. return None, None, None
  97. if dataset_id in uuid_set.uuids():
  98. uuid_version_list = uuid_set.get_version_list(dataset_id)
  99. else:
  100. if auto_create is False:
  101. return None, None, None
  102. uuid_version_list = VersionList()
  103. uuid_set.set_version_list(dataset_id, uuid_version_list)
  104. # Get the dataset tree
  105. if primary_data_version in tree_version_list.versions():
  106. time_stamp, dataset_tree = tree_version_list.get_dataset_tree(
  107. primary_data_version)
  108. else:
  109. if auto_create is False:
  110. return None, None, None
  111. time_stamp = str(time.time())
  112. dataset_tree = DatasetTree()
  113. tree_version_list.set_dataset_tree(
  114. primary_data_version,
  115. time_stamp,
  116. dataset_tree)
  117. if dataset_tree_path not in dataset_tree:
  118. if auto_create is False:
  119. return None, None, None
  120. dataset_level_metadata = Metadata()
  121. file_tree = FileTree()
  122. metadata_root_record = MetadataRootRecord(
  123. dataset_id,
  124. primary_data_version,
  125. dataset_level_metadata,
  126. file_tree)
  127. dataset_tree.add_dataset(dataset_tree_path, metadata_root_record)
  128. else:
  129. metadata_root_record = dataset_tree.get_metadata_root_record(
  130. dataset_tree_path)
  131. uuid_version_list.set_versioned_element(
  132. primary_data_version,
  133. str(time.time()),
  134. dataset_tree_path,
  135. metadata_root_record)
  136. return tree_version_list, uuid_set, metadata_root_record