forked from p15670423/monkey
Common: Add debug logging to PyPubSubEventQueue
This commit is contained in:
parent
0864593176
commit
0b8355c8a4
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
from typing import Type
|
||||
|
||||
from pubsub.core import Publisher
|
||||
|
@ -8,22 +9,33 @@ from . import EventSubscriber, IEventQueue
|
|||
|
||||
_ALL_EVENTS_TOPIC = "all_events_topic"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PyPubSubEventQueue(IEventQueue):
|
||||
def __init__(self, pypubsub_publisher: Publisher):
|
||||
self._pypubsub_publisher = pypubsub_publisher
|
||||
|
||||
def subscribe_all_events(self, subscriber: EventSubscriber):
|
||||
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC)
|
||||
self._subscribe(_ALL_EVENTS_TOPIC, subscriber)
|
||||
|
||||
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)
|
||||
self._subscribe(event_type_topic, subscriber)
|
||||
|
||||
def subscribe_tag(self, tag: str, subscriber: EventSubscriber):
|
||||
tag_topic = PyPubSubEventQueue._get_tag_topic(tag)
|
||||
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic)
|
||||
self._subscribe(tag_topic, subscriber)
|
||||
|
||||
def _subscribe(self, topic: str, subscriber: EventSubscriber):
|
||||
try:
|
||||
subscriber_name = subscriber.__name__
|
||||
except AttributeError:
|
||||
subscriber_name = subscriber.__class__.__name__
|
||||
|
||||
logging.debug(f"Subscriber {subscriber_name} subscribed to {topic}")
|
||||
self._pypubsub_publisher.subscribe(topicName=topic, listener=subscriber)
|
||||
|
||||
def publish(self, event: AbstractEvent):
|
||||
self._publish_to_all_events_topic(event)
|
||||
|
@ -31,16 +43,20 @@ class PyPubSubEventQueue(IEventQueue):
|
|||
self._publish_to_tags_topics(event)
|
||||
|
||||
def _publish_to_all_events_topic(self, event: AbstractEvent):
|
||||
self._pypubsub_publisher.sendMessage(_ALL_EVENTS_TOPIC, event=event)
|
||||
self._publish_event(_ALL_EVENTS_TOPIC, event)
|
||||
|
||||
def _publish_to_type_topic(self, event: AbstractEvent):
|
||||
event_type_topic = PyPubSubEventQueue._get_type_topic(event.__class__)
|
||||
self._pypubsub_publisher.sendMessage(event_type_topic, event=event)
|
||||
self._publish_event(event_type_topic, event)
|
||||
|
||||
def _publish_to_tags_topics(self, event: AbstractEvent):
|
||||
for tag in event.tags:
|
||||
tag_topic = PyPubSubEventQueue._get_tag_topic(tag)
|
||||
self._pypubsub_publisher.sendMessage(tag_topic, event=event)
|
||||
self._publish_event(tag_topic, event)
|
||||
|
||||
def _publish_event(self, topic: str, event: AbstractEvent):
|
||||
logger.debug(f"Publishing a {event.__class__.__name__} event to {topic}")
|
||||
self._pypubsub_publisher.sendMessage(topic, event=event)
|
||||
|
||||
# Appending a unique string to the topics for type and tags prevents bugs caused by collisions
|
||||
# between type names and tag names.
|
||||
|
|
Loading…
Reference in New Issue