Implement helper to get all EventHandler instances in a given stack frame

This commit is contained in:
Kristóf Tóth 2019-05-02 14:27:48 +02:00
parent 015d8f4355
commit 0771a068e0

View File

@ -2,6 +2,7 @@
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from inspect import currentframe
from tfw.networking.event_handlers import ServerConnector from tfw.networking.event_handlers import ServerConnector
from tfw.crypto import message_checksum, KeyManager, verify_message from tfw.crypto import message_checksum, KeyManager, verify_message
@ -105,6 +106,18 @@ class EventHandlerBase(ABC):
""" """
pass pass
@classmethod
def get_local_instances(cls):
frame = currentframe()
if frame is None:
raise EnvironmentError('inspect.currentframe() is not supported!')
locals_values = frame.f_back.f_locals.values()
return {
instance for instance in locals_values
if isinstance(instance, cls)
}
class FSMAwareEventHandler(EventHandlerBase, ABC): class FSMAwareEventHandler(EventHandlerBase, ABC):
# pylint: disable=abstract-method # pylint: disable=abstract-method