baseimage-tutorial-framework/tfw/internals/event_handling/event_handler.py

37 lines
912 B
Python
Raw Normal View History

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):
2019-08-26 09:10:39 +00:00
if self._validate_message(message):
self.handle_event(message, self.connector)
def _validate_message(self, message):
2019-08-28 14:40:23 +00:00
# pylint: disable=unused-argument,no-self-use
2019-08-26 09:10:39 +00:00
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