mtreeproxy.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from typing import (
  2. Any,
  3. Iterable,
  4. Optional,
  5. Tuple
  6. )
  7. from dataladmetadatamodel.mappableobject import MappableObject
  8. from dataladmetadatamodel.metadatapath import MetadataPath
  9. from dataladmetadatamodel.mtreenode import MTreeNode
  10. from dataladmetadatamodel.mapper.reference import Reference
  11. class MTreeProxy:
  12. """
  13. Base class for all classes that use MTreeNode internally,
  14. but are not themselves mappable. Those classes typically
  15. require a stored tree, but do not add additional
  16. "data-fields", but just convenience methods. Examples are
  17. "DatasetTree" and "FileTree". Still those classes are
  18. contained within other mappable objects and therefore should
  19. behave like a mappable object.
  20. This class proxies the common MappableObject-interface calls
  21. to the underlying MTreeNode object.
  22. """
  23. def __init__(self,
  24. leaf_class: Any,
  25. mtree: Optional[MTreeNode] = None,
  26. reference: Optional[Reference] = None):
  27. assert isinstance(mtree, (type(None), MTreeNode))
  28. assert isinstance(reference, (type(None), Reference))
  29. if mtree is None:
  30. self.mtree = MTreeNode(leaf_class=leaf_class,
  31. reference=reference)
  32. else:
  33. assert mtree.leaf_class == leaf_class
  34. self.mtree = mtree
  35. def __contains__(self, path: MetadataPath) -> bool:
  36. return self.mtree.get_object_at_path(path) is not None
  37. def ensure_mapped(self) -> bool:
  38. return self.mtree.ensure_mapped()
  39. def read_in(self, backend_type="git") -> MappableObject:
  40. self.mtree.read_in(backend_type)
  41. return self
  42. def write_out(self,
  43. destination: Optional[str] = None,
  44. backend_type: str = "git",
  45. force_write: bool = False) -> Reference:
  46. return self.mtree.write_out(destination,
  47. backend_type,
  48. force_write)
  49. def purge(self):
  50. return self.mtree.purge()
  51. def is_saved_on(self, destination: str):
  52. return self.mtree.is_saved_on(destination)
  53. def deepcopy(self,
  54. new_mapper_family: Optional[str] = None,
  55. new_destination: Optional[str] = None,
  56. **kwargs) -> "MTreeProxy":
  57. return type(self)(self.mtree.deepcopy(new_mapper_family,
  58. new_destination,
  59. **kwargs))
  60. def get_paths_recursive(self,
  61. show_intermediate: Optional[bool] = False
  62. ) -> Iterable[Tuple[MetadataPath, MappableObject]]:
  63. yield from self.mtree.get_paths_recursive(show_intermediate)