modifiableobject.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from abc import (
  2. ABCMeta,
  3. abstractmethod
  4. )
  5. from typing import (
  6. Iterable,
  7. Optional,
  8. )
  9. class ModifiableObject(metaclass=ABCMeta):
  10. """
  11. Modifiable objects track their modification status.
  12. including the modification status of potential
  13. contained objects.
  14. If one of the contained objects is modified, the
  15. containing object is considered modified as well.
  16. Objects determine their modification-relevant
  17. sub-objects by implementing their version of the
  18. method "get_modifiable_sub_objects".
  19. """
  20. def __init__(self, saved_on: Optional[str] = None):
  21. self.saved_on = set()
  22. if saved_on:
  23. self.saved_on.add(saved_on)
  24. def touch(self):
  25. self.set_unsaved()
  26. def set_saved_on(self, destination: str):
  27. self.saved_on.add(destination)
  28. def set_unsaved(self):
  29. self.saved_on = set()
  30. def is_saved_on(self, destination: str) -> bool:
  31. """
  32. Check whether the object is saved on the given
  33. destination. This check includes whether all
  34. modifiable sub-objects are saved on the
  35. given destination.
  36. """
  37. if destination not in self.saved_on:
  38. return False
  39. return all(
  40. map(
  41. lambda element: element.is_saved_on(destination),
  42. self.modifiable_sub_objects))
  43. @abstractmethod
  44. def modifiable_sub_objects(self) -> Iterable["ModifiableObject"]:
  45. raise NotImplementedError