metadatarootrecordmapper.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from uuid import UUID
  2. from dataladmetadatamodel.mapper.gitmapper.gitbackend.subprocess import (
  3. git_load_json,
  4. git_save_json,
  5. )
  6. from dataladmetadatamodel.mapper.mapper import Mapper
  7. from dataladmetadatamodel.mapper.reference import Reference
  8. class Strings:
  9. GIT = "git"
  10. DATASET_IDENTIFIER = "dataset_identifier"
  11. DATASET_VERSION = "dataset_version"
  12. DATASET_LEVEL_METADATA = "dataset_level_metadata"
  13. FILE_TREE = "file_tree"
  14. class MetadataRootRecordGitMapper(Mapper):
  15. def map_in_impl(self,
  16. metadata_root_record: "MetadataRootRecord",
  17. realm: str,
  18. reference: Reference) -> None:
  19. from dataladmetadatamodel.filetree import FileTree
  20. from dataladmetadatamodel.metadata import Metadata
  21. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  22. assert isinstance(metadata_root_record, MetadataRootRecord)
  23. assert isinstance(realm, str)
  24. assert isinstance(reference, Reference)
  25. json_object = git_load_json(realm, reference.location)
  26. metadata_reference = Reference.from_json_obj(
  27. json_object[Strings.DATASET_LEVEL_METADATA])
  28. if metadata_reference.is_none_reference():
  29. metadata = None
  30. else:
  31. metadata = Metadata(realm=realm, reference=metadata_reference)
  32. file_tree_reference = Reference.from_json_obj(
  33. json_object[Strings.FILE_TREE])
  34. if file_tree_reference.is_none_reference():
  35. file_tree = None
  36. else:
  37. file_tree = FileTree(realm=realm, reference=file_tree_reference)
  38. MetadataRootRecord.__init__(
  39. metadata_root_record,
  40. UUID(json_object[Strings.DATASET_IDENTIFIER]),
  41. json_object[Strings.DATASET_VERSION],
  42. metadata,
  43. file_tree,
  44. realm=realm,
  45. reference=reference)
  46. def map_out_impl(self,
  47. mrr: "MetadataRootRecord",
  48. realm: str,
  49. force_write: bool) -> Reference:
  50. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  51. assert isinstance(mrr, MetadataRootRecord)
  52. if mrr._file_tree is None:
  53. file_tree_reference = Reference.get_none_reference("FileTree")
  54. else:
  55. file_tree_reference = mrr._file_tree.write_out(
  56. realm,
  57. "git",
  58. force_write)
  59. if mrr.dataset_level_metadata is None:
  60. dataset_level_metadata_reference = Reference.get_none_reference("Metadata")
  61. else:
  62. dataset_level_metadata_reference = mrr.dataset_level_metadata.write_out(
  63. realm,
  64. "git",
  65. force_write)
  66. json_object = {
  67. Strings.DATASET_IDENTIFIER: str(mrr.dataset_identifier),
  68. Strings.DATASET_VERSION: str(mrr.dataset_version),
  69. Strings.DATASET_LEVEL_METADATA: dataset_level_metadata_reference.to_json_obj(),
  70. Strings.FILE_TREE: file_tree_reference.to_json_obj()}
  71. return Reference(
  72. "MetadataRootRecord",
  73. git_save_json(realm, json_object))