versionlistmapper.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from dataladmetadatamodel.mapper.gitmapper.objectreference import GitReference
  2. from dataladmetadatamodel.mapper.gitmapper.gitbackend.subprocess import (
  3. git_load_json,
  4. git_save_json,
  5. git_update_ref
  6. )
  7. from dataladmetadatamodel.mapper.mapper import Mapper
  8. from dataladmetadatamodel.mapper.reference import Reference
  9. class VersionListGitMapper(Mapper):
  10. """
  11. Map version lists to git objects.
  12. The objects are blobs containing json strings that
  13. define a list of primary data-metadata associations.
  14. """
  15. def _get_version_records(self,
  16. realm: str,
  17. reference: Reference) -> dict:
  18. from dataladmetadatamodel.datasettree import DatasetTree
  19. from dataladmetadatamodel.metadatapath import MetadataPath
  20. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  21. from dataladmetadatamodel.versionlist import VersionRecord
  22. assert isinstance(reference, Reference)
  23. json_object = git_load_json(realm, reference.location)
  24. version_records = {}
  25. for pdm_assoc in json_object:
  26. reference = Reference.from_json_obj(pdm_assoc["dataset_tree"])
  27. if reference.class_name in ("DatasetTree", "MTreeNode"):
  28. mrr_or_tree = DatasetTree(realm=realm, reference=reference)
  29. elif reference.class_name == "MetadataRootRecord":
  30. mrr_or_tree = MetadataRootRecord(None, None, None, None, realm, reference)
  31. else:
  32. raise ValueError(
  33. f"unexpected tree class in primary data-metadata "
  34. f"assoc: ({reference.class_name})")
  35. version_records[pdm_assoc["primary_data_version"]] = VersionRecord(
  36. pdm_assoc["time_stamp"],
  37. MetadataPath(pdm_assoc["path"]),
  38. mrr_or_tree)
  39. return version_records
  40. def map_in_impl(self,
  41. version_list: "VersionList",
  42. realm: str,
  43. reference: Reference) -> None:
  44. from dataladmetadatamodel.versionlist import VersionList
  45. assert isinstance(version_list, VersionList)
  46. version_list.version_set = self._get_version_records(realm, reference)
  47. def map_out_impl(self,
  48. version_list: "VersionList",
  49. realm: str,
  50. force_write: bool
  51. ) -> Reference:
  52. from dataladmetadatamodel.versionlist import VersionList
  53. assert isinstance(version_list, VersionList)
  54. json_object = [
  55. {
  56. "primary_data_version": primary_data_version,
  57. "time_stamp": version_record.time_stamp,
  58. "path": version_record.path.as_posix(),
  59. "dataset_tree": version_record.element.write_out(realm).to_json_obj()
  60. }
  61. for primary_data_version, version_record in version_list.version_set.items()
  62. ]
  63. location = git_save_json(realm, json_object)
  64. return Reference("VersionList", location)
  65. class TreeVersionListGitMapper(VersionListGitMapper):
  66. def map_in_impl(self,
  67. tree_version_list: "TreeVersionList",
  68. realm: str,
  69. reference: Reference) -> None:
  70. from dataladmetadatamodel.versionlist import TreeVersionList
  71. assert isinstance(tree_version_list, TreeVersionList)
  72. tree_version_list.version_set = self._get_version_records(realm, reference)
  73. def map_out_impl(self,
  74. tree_version_list: "TreeVersionList",
  75. realm: str,
  76. force_write: bool
  77. ) -> Reference:
  78. from dataladmetadatamodel.versionlist import TreeVersionList
  79. assert isinstance(tree_version_list, TreeVersionList)
  80. reference = super().map_out_impl(tree_version_list, realm, force_write)
  81. git_update_ref(
  82. realm,
  83. GitReference.TREE_VERSION_LIST.value,
  84. reference.location)
  85. return reference