driver.py 1.7 KB

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