mtreenodemapper.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from dataladmetadatamodel.mapper.gitmapper.gitbackend.subprocess import (
  2. git_read_tree_node,
  3. git_save_tree_node,
  4. )
  5. from dataladmetadatamodel.mapper.mapper import Mapper
  6. from dataladmetadatamodel.mapper.reference import Reference
  7. class MTreeNodeGitMapper(Mapper):
  8. def map_in_impl(self,
  9. mtree_node: "MTreeNode",
  10. realm: str,
  11. reference: Reference) -> None:
  12. from dataladmetadatamodel.mtreenode import MTreeNode
  13. if reference.is_none_reference():
  14. mtree_node.child_nodes = dict()
  15. return
  16. assert isinstance(mtree_node, MTreeNode)
  17. lines = git_read_tree_node(realm,
  18. reference.location)
  19. for entry in [line.split() for line in lines]:
  20. if entry[1] == "tree":
  21. child = MTreeNode(
  22. leaf_class=mtree_node.leaf_class,
  23. realm=realm,
  24. reference=Reference("MTreeNode",
  25. entry[2]))
  26. elif entry[1] == "blob":
  27. child = mtree_node.leaf_class.get_empty_instance(
  28. realm=realm,
  29. reference=Reference(mtree_node.leaf_class_name,
  30. entry[2]))
  31. else:
  32. raise ValueError(f"unknown git tree entry type: {entry[1]}")
  33. mtree_node.child_nodes[entry[3]] = child
  34. def map_out_impl(self,
  35. mtree_node: "MTreeNode",
  36. realm: str,
  37. force_write: bool) -> Reference:
  38. from dataladmetadatamodel.mtreenode import MTreeNode
  39. assert isinstance(mtree_node, MTreeNode)
  40. if not mtree_node.child_nodes:
  41. return Reference.get_none_reference("MTreeNode")
  42. for child_node in mtree_node.child_nodes.values():
  43. child_node.write_out(realm)
  44. dir_entries = [
  45. (
  46. "040000" if isinstance(child_node, MTreeNode) else "100644",
  47. "tree" if isinstance(child_node, MTreeNode) else "blob",
  48. child_node.reference.location,
  49. child_name
  50. )
  51. for child_name, child_node in mtree_node.child_nodes.items()
  52. ]
  53. mtree_node_location = git_save_tree_node(realm, dir_entries)
  54. return Reference("MTreeNode", mtree_node_location)