Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

mapper.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from abc import (
  2. ABCMeta,
  3. abstractmethod
  4. )
  5. from typing import Optional
  6. from dataladmetadatamodel.log import logger
  7. from dataladmetadatamodel.mapper.reference import Reference
  8. class Mapper(metaclass=ABCMeta):
  9. """
  10. Mapper are responsible for populating an existing
  11. object of a certain class from a backend. The mapper
  12. will delegate sub-object mapping to the mapper for
  13. the class of the sub-object.
  14. This base class handles default destination
  15. """
  16. def __init__(self,
  17. class_name: str,
  18. default_destination: Optional[str] = None):
  19. self.class_name = class_name
  20. self.destination = default_destination
  21. def map_in(self,
  22. mappable_object: "MappableObject",
  23. reference: Reference) -> None:
  24. # Check for old-style file tree and dataset tree references
  25. if reference.class_name in ("FileTree", "DatasetTree"):
  26. logger.warning(f"Mapper.map_in(): converting reference to class "
  27. f"{reference.class_name} to a reference to class "
  28. f"MTreeNode")
  29. reference.class_name = "MTreeNode"
  30. # TODO: this is not too nice, but required since FileTree and
  31. # DatasetTree are proxies for MTreeNode which just add some
  32. # methods.
  33. mo_class_name = type(mappable_object).__name__
  34. if mo_class_name in ("FileTree", "DatasetTree"):
  35. assert self.class_name == "MTreeNode", \
  36. f"Mappable object class: {mo_class_name} " \
  37. f"can not be mapped in by a mapper for class: {self.class_name}"
  38. else:
  39. assert mo_class_name == self.class_name, \
  40. f"Mappable object class name ({mo_class_name}) " \
  41. f"does not match this mapper's class_name ({self.class_name})"
  42. assert reference.class_name == self.class_name, \
  43. f"Reference class name ({reference.class_name}) " \
  44. f"does not match self.class_name ({self.class_name})"
  45. self.map_in_impl(mappable_object, reference)
  46. def map_out(self,
  47. mappable_object: "MappableObject",
  48. destination: Optional[str] = None,
  49. force_write: bool = False) -> Reference:
  50. if destination is None:
  51. if self.destination is None:
  52. raise Exception(
  53. "'destination' not set and no default destination provided")
  54. destination = self.destination
  55. return self.map_out_impl(mappable_object, destination, force_write)
  56. @abstractmethod
  57. def map_in_impl(self,
  58. mappable_object: "MappableObject",
  59. reference: Reference) -> None:
  60. raise NotImplementedError
  61. @abstractmethod
  62. def map_out_impl(self,
  63. mappable_object: "MappableObject",
  64. destination: str,
  65. force_write: bool) -> Reference:
  66. raise NotImplementedError