uuidset.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from typing import (
  2. Dict,
  3. Iterable,
  4. Optional
  5. )
  6. from uuid import UUID
  7. from dataladmetadatamodel.mappableobject import MappableObject
  8. from dataladmetadatamodel.versionlist import VersionList
  9. from dataladmetadatamodel.mapper.reference import Reference
  10. class UUIDSet(MappableObject):
  11. def __init__(self,
  12. initial_set: Optional[Dict[UUID, VersionList]] = None,
  13. reference: Optional[Reference] = None):
  14. assert isinstance(initial_set, (type(None), dict))
  15. assert isinstance(reference, (type(None), Reference))
  16. super().__init__(reference)
  17. self.uuid_set = initial_set or dict()
  18. def get_modifiable_sub_objects_impl(self) -> Iterable[MappableObject]:
  19. return self.uuid_set.values()
  20. def purge_impl(self):
  21. for version_list in self.uuid_set.values():
  22. version_list.purge()
  23. self.uuid_set = dict()
  24. def uuids(self):
  25. return self.uuid_set.keys()
  26. def set_version_list(self,
  27. uuid: UUID,
  28. version_list: VersionList):
  29. """
  30. Set a new or updated version list for the uuid.
  31. Existing references are deleted.
  32. The entry is marked as dirty.
  33. """
  34. self.touch()
  35. self.uuid_set[uuid] = version_list
  36. def get_version_list(self, uuid) -> VersionList:
  37. """
  38. Get the version list for uuid. If it is not mapped yet,
  39. it will be mapped.
  40. """
  41. return self.uuid_set[uuid].read_in()
  42. def unget_version_list(self, uuid):
  43. """
  44. Remove a version list from memory. First, persist the
  45. current status, if it was changed.
  46. """
  47. self.uuid_set[uuid].write_out()
  48. self.uuid_set[uuid].purge()
  49. def deepcopy_impl(self,
  50. new_mapper_family: Optional[str] = None,
  51. new_destination: Optional[str] = None,
  52. **kwargs) -> "UUIDSet":
  53. copied_uuid_set = UUIDSet()
  54. for uuid, version_list in self.uuid_set.items():
  55. copied_uuid_set.uuid_set[uuid] = version_list.deepcopy(
  56. new_mapper_family,
  57. new_destination)
  58. copied_uuid_set.write_out(new_destination)
  59. copied_uuid_set.purge()
  60. return copied_uuid_set