from threading import Thread from logger import Logger class EventDriver(Logger): """ The event driver receives driver-specific messages, which are delegated to the root area in the form of common events such as 'piont_down' pr 'point_up'. """ def __init__(self, root_area=None): self.root_area = root_area def get_root_area(self): return self.root_area def set_root_area(self, area): self.root_area = area def delegate_event(self, event): """ Delegate an event that has been triggered by the event driver to the area tree. """ if self.root_area.contains_event(event): self.root_area.delegate_event(event) def start(self, threaded=False): """ Start the event loop. A root area is needed to be able to delegate events, so check if it exists first. The argument determines if the driver should start in a new (daemon) thread. """ if not self.root_area: raise ValueError('Cannot start event server without root area.') if threaded: thread = Thread(target=self.start) thread.daemon = True thread.start() return thread self.start_loop() def start_loop(self): """ Start the event loop. """ raise NotImplementedError def stop(self): """ Stop the event loop. """ raise NotImplementedError def run(self): """ Execute a single loop iteration. If implemented, this method provides a way to run the driver within another event loop. """ raise NotImplementedError