modifiableobject.py 1.5 KB

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