From 9a7fc031d951775f499316e47a354188711a4963 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 30 Jun 2023 23:01:09 +0200 Subject: [PATCH] A more robust logic to detect plugins by class type in get_plugin. --- platypush/context/__init__.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 3c0cb1e62..6040102e3 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -130,20 +130,26 @@ def get_plugin(plugin, plugin_name=None, reload=False): else: raise TypeError(f'Invalid plugin type/name: {plugin}') + assert name, 'No plugin name provided' if name not in plugins_init_locks: plugins_init_locks[name] = RLock() if name in _ctx.plugins and not reload: return _ctx.plugins[name] + module_name = None if isinstance(plugin, str): - try: - plugin = importlib.import_module( - 'platypush.plugins.' + name - ) # type: ignore - except ImportError as e: - logger.warning('No such plugin: %s', name) - raise RuntimeError(e) from e + module_name = 'platypush.plugins.{name}' + elif issubclass(plugin, Plugin): + module_name = plugin.__module__ + else: + raise RuntimeError(f'Invalid plugin type/name: {plugin}') + + try: + plugin = importlib.import_module(module_name) + except ImportError as e: + logger.warning('No such plugin: %s', name) + raise RuntimeError(e) from e # e.g. plugins.music.mpd main class: MusicMpdPlugin cls_name = ''