common.py 4.7 KB

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