uuidset.py 2.4 KB

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