From 35155c345fd17903cce2ae8f91fb9bf8ed9bfeb3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:36:59 -0400 Subject: [PATCH] Common: Use EventSubscriber for IEventQueue type hints --- monkey/common/event_queue/i_event_queue.py | 18 +++++++++--------- .../common/event_queue/pypubsub_event_queue.py | 11 +++++------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 17fdb6356..f13b0d4b8 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,8 +1,10 @@ from abc import ABC, abstractmethod -from typing import Callable, Type +from typing import Type from common.events import AbstractEvent +from . import EventSubscriber + class IEventQueue(ABC): """ @@ -10,35 +12,33 @@ class IEventQueue(ABC): """ @abstractmethod - def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: EventSubscriber): """ Subscribes a subscriber to all events - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass @abstractmethod - def subscribe_type( - self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): + def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): """ Subscribes a subscriber to the specifed event type :param event_type: Event type to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass @abstractmethod - def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): + def subscribe_tag(self, tag: str, subscriber: EventSubscriber): """ Subscribes a subscriber to the specified event tag :param tag: Event tag to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index bdf292fdd..6398db4ff 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,9 +1,10 @@ -from typing import Callable, Type +from typing import Type from pubsub.core import Publisher from common.events import AbstractEvent +from . import EventSubscriber from .i_event_queue import IEventQueue _ALL_EVENTS_TOPIC = "all_events_topic" @@ -13,17 +14,15 @@ class PyPubSubEventQueue(IEventQueue): def __init__(self, pypubsub_publisher: Publisher): self._pypubsub_publisher = pypubsub_publisher - def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: EventSubscriber): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC) - def subscribe_type( - self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): + def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_topic = PyPubSubEventQueue._get_type_topic(event_type) self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_topic) - def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): + def subscribe_tag(self, tag: str, subscriber: EventSubscriber): tag_topic = PyPubSubEventQueue._get_tag_topic(tag) self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic)