A more robust logic to detect plugins by class type in get_plugin.

This commit is contained in:
Fabio Manganiello 2023-06-30 23:01:09 +02:00
parent f3b2386ed6
commit 9a7fc031d9
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -130,17 +130,23 @@ def get_plugin(plugin, plugin_name=None, reload=False):
else: else:
raise TypeError(f'Invalid plugin type/name: {plugin}') raise TypeError(f'Invalid plugin type/name: {plugin}')
assert name, 'No plugin name provided'
if name not in plugins_init_locks: if name not in plugins_init_locks:
plugins_init_locks[name] = RLock() plugins_init_locks[name] = RLock()
if name in _ctx.plugins and not reload: if name in _ctx.plugins and not reload:
return _ctx.plugins[name] return _ctx.plugins[name]
module_name = None
if isinstance(plugin, str): if isinstance(plugin, str):
module_name = 'platypush.plugins.{name}'
elif issubclass(plugin, Plugin):
module_name = plugin.__module__
else:
raise RuntimeError(f'Invalid plugin type/name: {plugin}')
try: try:
plugin = importlib.import_module( plugin = importlib.import_module(module_name)
'platypush.plugins.' + name
) # type: ignore
except ImportError as e: except ImportError as e:
logger.warning('No such plugin: %s', name) logger.warning('No such plugin: %s', name)
raise RuntimeError(e) from e raise RuntimeError(e) from e