From 0771a068e015fac6257a75543048a0014dcbadc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 2 May 2019 14:27:48 +0200 Subject: [PATCH] Implement helper to get all EventHandler instances in a given stack frame --- lib/tfw/event_handler_base.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index aaf408c..04bcaf4 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -2,6 +2,7 @@ # All Rights Reserved. See LICENSE file for details. from abc import ABC, abstractmethod +from inspect import currentframe from tfw.networking.event_handlers import ServerConnector from tfw.crypto import message_checksum, KeyManager, verify_message @@ -105,6 +106,18 @@ class EventHandlerBase(ABC): """ 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): # pylint: disable=abstract-method