From 86018be736d27884b77255c301581100cfe470d9 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 6 Sep 2022 12:09:48 +0530 Subject: [PATCH] Common: Rename EventSubscriber -> AgentEventSubscriber --- monkey/common/event_queue/__init__.py | 2 +- monkey/common/event_queue/i_agent_event_queue.py | 10 ++++++---- .../event_queue/pypubsub_agent_event_queue.py | 14 ++++++++------ monkey/common/event_queue/types.py | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/monkey/common/event_queue/__init__.py b/monkey/common/event_queue/__init__.py index 34567919c..26d29cb49 100644 --- a/monkey/common/event_queue/__init__.py +++ b/monkey/common/event_queue/__init__.py @@ -1,3 +1,3 @@ -from .types import EventSubscriber +from .types import AgentEventSubscriber from .i_agent_event_queue import IAgentEventQueue from .pypubsub_agent_event_queue import PyPubSubAgentEventQueue diff --git a/monkey/common/event_queue/i_agent_event_queue.py b/monkey/common/event_queue/i_agent_event_queue.py index d1ebf25ca..19ef65e2b 100644 --- a/monkey/common/event_queue/i_agent_event_queue.py +++ b/monkey/common/event_queue/i_agent_event_queue.py @@ -3,7 +3,7 @@ from typing import Type from common.events import AbstractAgentEvent -from . import EventSubscriber +from . import AgentEventSubscriber class IAgentEventQueue(ABC): @@ -12,7 +12,7 @@ class IAgentEventQueue(ABC): """ @abstractmethod - def subscribe_all_events(self, subscriber: EventSubscriber): + def subscribe_all_events(self, subscriber: AgentEventSubscriber): """ Subscribes a subscriber to all events @@ -22,7 +22,9 @@ class IAgentEventQueue(ABC): pass @abstractmethod - def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber): + def subscribe_type( + self, event_type: Type[AbstractAgentEvent], subscriber: AgentEventSubscriber + ): """ Subscribes a subscriber to the specified event type @@ -33,7 +35,7 @@ class IAgentEventQueue(ABC): pass @abstractmethod - def subscribe_tag(self, tag: str, subscriber: EventSubscriber): + def subscribe_tag(self, tag: str, subscriber: AgentEventSubscriber): """ Subscribes a subscriber to the specified event tag diff --git a/monkey/common/event_queue/pypubsub_agent_event_queue.py b/monkey/common/event_queue/pypubsub_agent_event_queue.py index 7decce957..86dbea4af 100644 --- a/monkey/common/event_queue/pypubsub_agent_event_queue.py +++ b/monkey/common/event_queue/pypubsub_agent_event_queue.py @@ -5,7 +5,7 @@ from pubsub.core import Publisher from common.events import AbstractAgentEvent -from . import EventSubscriber, IAgentEventQueue +from . import AgentEventSubscriber, IAgentEventQueue _ALL_EVENTS_TOPIC = "all_events_topic" @@ -17,19 +17,21 @@ class PyPubSubAgentEventQueue(IAgentEventQueue): self._pypubsub_publisher = pypubsub_publisher self._refs = [] - def subscribe_all_events(self, subscriber: EventSubscriber): + def subscribe_all_events(self, subscriber: AgentEventSubscriber): self._subscribe(_ALL_EVENTS_TOPIC, subscriber) - def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber): + def subscribe_type( + self, event_type: Type[AbstractAgentEvent], subscriber: AgentEventSubscriber + ): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_topic = PyPubSubAgentEventQueue._get_type_topic(event_type) self._subscribe(event_type_topic, subscriber) - def subscribe_tag(self, tag: str, subscriber: EventSubscriber): + def subscribe_tag(self, tag: str, subscriber: AgentEventSubscriber): tag_topic = PyPubSubAgentEventQueue._get_tag_topic(tag) self._subscribe(tag_topic, subscriber) - def _subscribe(self, topic: str, subscriber: EventSubscriber): + def _subscribe(self, topic: str, subscriber: AgentEventSubscriber): try: subscriber_name = subscriber.__name__ except AttributeError: @@ -39,7 +41,7 @@ class PyPubSubAgentEventQueue(IAgentEventQueue): self._pypubsub_publisher.subscribe(topicName=topic, listener=subscriber) self._keep_subscriber_strongref(subscriber) - def _keep_subscriber_strongref(self, subscriber: EventSubscriber): + def _keep_subscriber_strongref(self, subscriber: AgentEventSubscriber): # NOTE: PyPubSub stores subscribers by weak reference. From the documentation: # > PyPubSub holds listeners by weak reference so that the lifetime of the # > callable is not affected by PyPubSub: once the application no longer diff --git a/monkey/common/event_queue/types.py b/monkey/common/event_queue/types.py index f3f5f4ef8..6b57ae712 100644 --- a/monkey/common/event_queue/types.py +++ b/monkey/common/event_queue/types.py @@ -2,4 +2,4 @@ from typing import Callable from common.events import AbstractAgentEvent -EventSubscriber = Callable[[AbstractAgentEvent], None] +AgentEventSubscriber = Callable[[AbstractAgentEvent], None]