driver.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from logger import Logger
  2. class EventDriver(Logger):
  3. """
  4. The event driver receives driver-specific messages, which are delegated to
  5. the root area in the form of common events such as 'piont_down' pr
  6. 'point_up'.
  7. """
  8. def __init__(self, root_area=None):
  9. self.root_area = root_area
  10. def get_root_area(self):
  11. return self.root_area
  12. def set_root_area(self, area):
  13. self.root_area = area
  14. def delegate_event(self, event):
  15. """
  16. Delegate an event that has been triggered by the event driver to the
  17. area tree.
  18. """
  19. if self.root_area.contains_event(event):
  20. self.root_area.delegate_event(event)
  21. def start(self):
  22. """
  23. Start the event loop. A root area is needed to be able to delegate
  24. events, so check if it exists first.
  25. """
  26. if not self.root_area:
  27. raise ValueError('Cannot start event server without root area.')
  28. self.start_loop()
  29. def start_loop(self):
  30. """
  31. Start the event loop.
  32. """
  33. raise NotImplementedError
  34. def stop(self):
  35. """
  36. Stop the event loop.
  37. """
  38. raise NotImplementedError
  39. def run(self):
  40. """
  41. Execute a single loop iteration. If implemented, this method provides a
  42. way to run the driver within another event loop.
  43. """
  44. raise NotImplementedError