baseimage-tutorial-framework/tfw/internals/event_handling/event_handler.py
2019-08-30 14:44:57 +02:00

37 lines
912 B
Python

from .type_id_registry import TypeIdRegistryMixin
class EventHandler(TypeIdRegistryMixin):
_instances = set()
_type_id_registry = {}
def __init__(self, connector):
type(self)._instances.add(self)
self.connector = connector
def start(self):
self.connector.register_callback(self._event_callback)
def _event_callback(self, message):
if self._validate_message(message):
self.handle_event(message, self.connector)
def _validate_message(self, message):
# pylint: disable=unused-argument,no-self-use
return True
def handle_event(self, message, connector):
raise NotImplementedError()
@classmethod
def stop_all_instances(cls):
for instance in cls._instances:
instance.stop()
def stop(self):
self.connector.close()
self.cleanup()
def cleanup(self):
pass