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,20 +130,26 @@ 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):
try: module_name = 'platypush.plugins.{name}'
plugin = importlib.import_module( elif issubclass(plugin, Plugin):
'platypush.plugins.' + name module_name = plugin.__module__
) # type: ignore else:
except ImportError as e: raise RuntimeError(f'Invalid plugin type/name: {plugin}')
logger.warning('No such plugin: %s', name)
raise RuntimeError(e) from e 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 # e.g. plugins.music.mpd main class: MusicMpdPlugin
cls_name = '' cls_name = ''