PyBitmessage/src/plugins/plugin.py

52 lines
1.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-09-23 10:38:18 +00:00
"""
Operating with plugins
2019-09-23 10:38:18 +00:00
"""
import logging
import pkg_resources
logger = logging.getLogger('default')
2017-03-10 14:45:46 +00:00
def get_plugins(group, point='', name=None, fallback=None):
"""
:param str group: plugin group
:param str point: plugin name prefix
:param name: exact plugin name
:param fallback: fallback plugin name
Iterate through plugins (``connect_plugin`` attribute of entry point)
which name starts with ``point`` or equals to ``name``.
If ``fallback`` kwarg specified, plugin with that name yield last.
2017-03-10 14:45:46 +00:00
"""
for ep in pkg_resources.iter_entry_points('bitmessage.' + group):
if name and ep.name == name or not point or ep.name.startswith(point):
try:
2017-03-10 14:45:46 +00:00
plugin = ep.load().connect_plugin
if ep.name == fallback:
_fallback = plugin
else:
yield plugin
except (AttributeError,
2017-03-10 14:45:46 +00:00
ImportError,
ValueError,
pkg_resources.DistributionNotFound,
pkg_resources.UnknownExtra):
logger.debug(
'Problem while loading %s', ep.name, exc_info=True)
continue
2017-03-10 14:45:46 +00:00
try:
yield _fallback
except NameError:
pass
def get_plugin(*args, **kwargs):
"""
:return: first available plugin from :func:`get_plugins` if any.
"""
2017-03-10 14:45:46 +00:00
for plugin in get_plugins(*args, **kwargs):
return plugin