filetree.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from typing import (
  2. Iterable,
  3. Optional,
  4. Tuple
  5. )
  6. from dataladmetadatamodel import JSONObject
  7. from dataladmetadatamodel.metadata import (
  8. ExtractorConfiguration,
  9. Metadata
  10. )
  11. from dataladmetadatamodel.mappableobject import MappableObject
  12. from dataladmetadatamodel.metadatapath import MetadataPath
  13. from dataladmetadatamodel.mtreenode import MTreeNode
  14. from dataladmetadatamodel.mtreeproxy import MTreeProxy
  15. from dataladmetadatamodel.mapper.reference import Reference
  16. class FileTree(MTreeProxy):
  17. def __init__(self,
  18. mtree: Optional[MTreeNode] = None,
  19. realm: Optional[str] = None,
  20. reference: Optional[Reference] = None):
  21. assert isinstance(mtree, (type(None), MTreeNode))
  22. assert isinstance(reference, (type(None), Reference))
  23. super().__init__(Metadata, mtree, realm, reference)
  24. def add_metadata(self,
  25. path: MetadataPath,
  26. metadata: Metadata):
  27. self.mtree.add_child_at(metadata, path)
  28. def get_metadata(self,
  29. path: MetadataPath
  30. ) -> Optional[Metadata]:
  31. metadata = self.mtree.get_object_at_path(path)
  32. if metadata is None:
  33. return None
  34. assert isinstance(metadata, Metadata)
  35. metadata.ensure_mapped()
  36. return metadata
  37. def unget_metadata(self,
  38. metadata: Metadata,
  39. destination: Optional[str] = None):
  40. assert isinstance(metadata, Metadata)
  41. metadata.write_out(destination)
  42. metadata.purge()
  43. def add_extractor_run(self,
  44. path,
  45. time_stamp: Optional[float],
  46. extractor_name: str,
  47. author_name: str,
  48. author_email: str,
  49. configuration: ExtractorConfiguration,
  50. metadata_content: JSONObject):
  51. try:
  52. metadata = self.get_metadata(path)
  53. except AttributeError:
  54. metadata = Metadata()
  55. self.add_metadata(path, metadata)
  56. metadata.add_extractor_run(
  57. time_stamp,
  58. extractor_name,
  59. author_name,
  60. author_email,
  61. configuration,
  62. metadata_content
  63. )
  64. def get_paths_recursive(self,
  65. show_intermediate: Optional[bool] = False
  66. ) -> Iterable[Tuple[MetadataPath, MappableObject]]:
  67. yield from self.mtree.get_paths_recursive(show_intermediate)