mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 22:11:32 +00:00
37 lines
912 B
Python
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
|