Common: Remove MRO logic from PypubsubEventQueue

This isn't needed. The Agent's responsibility is to just declare what it
did in the form of events.

For example, although CredentialsStolenEvent is a type of SecurityEvent
technically, the Agent doesn't care about this. The Island does, which
is why this should be handled on the pub/sub implementation on the
Island side. The Agent merely conveys the information that it stole
credentials. Whatever is to be done or however that information is to be
handled after that is the Island's responsibility.

I don't see a use case in the Agent where a concrete event is derived
from another concrete event. So, the MRO logic is removed in this
commit.
This commit is contained in:
Shreya Malviya 2022-08-08 13:29:24 +05:30
parent d773bac8c1
commit 84c800b815
1 changed files with 4 additions and 19 deletions

View File

@ -26,25 +26,10 @@ class PypubsubEventQueue(IEventQueue):
@staticmethod
def publish(event: AbstractEvent, data: Any):
# someClass.mro() returns a list of types that someClass is derived from,
# in order of resolution
# we can be sure that for any valid event, the last three items in the list will be
# <class '__main__.AbstractEvent'>, <class 'abc.ABC'>, and <class 'object'>
# for some event, say, CredentialsStolenEvent which was derived from SecurityEvent,
# we want to publish the data to both events, so, we loop through the super
# classes of the CredentialsStolenEvent which was initially passed as an argument
# to the function, and publish to each class's type and tags (except the last 3 classes)
for event_type in event.mro()[:-3]:
PypubsubEventQueue._publish_to_type(event_type, data)
PypubsubEventQueue._publish_to_tags(event_type, data)
@staticmethod
def _publish_to_type(event_type: AbstractEvent, data: Any):
event_type_name = event_type.__name__
# publish to event type's topic
event_type_name = event.__name__
pub.sendMessage(event_type_name, data)
@staticmethod
def _publish_to_tags(event_type: AbstractEvent, data: Any):
for tag in event_type.tags:
# publish to tags' topics
for tag in event.tags:
pub.sendMessage(tag, data)