2017-03-01 15:46:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import pkg_resources
|
|
|
|
|
|
|
|
|
2017-03-10 15:45:46 +01:00
|
|
|
def get_plugins(group, point='', name=None, fallback=None):
|
|
|
|
"""
|
|
|
|
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-24 13:31:27 +01:00
|
|
|
for ep in pkg_resources.iter_entry_points('bitmessage.' + group):
|
2017-03-10 15:45:46 +01:00
|
|
|
if name and ep.name == name or ep.name.startswith(point):
|
2017-03-01 15:46:13 +01:00
|
|
|
try:
|
2017-03-10 15:45:46 +01:00
|
|
|
plugin = ep.load().connect_plugin
|
|
|
|
if ep.name == fallback:
|
|
|
|
_fallback = plugin
|
|
|
|
else:
|
|
|
|
yield plugin
|
2017-03-10 00:00:54 +01:00
|
|
|
except (AttributeError,
|
2017-03-10 15:45:46 +01:00
|
|
|
ImportError,
|
|
|
|
ValueError,
|
2017-03-10 00:00:54 +01:00
|
|
|
pkg_resources.DistributionNotFound,
|
|
|
|
pkg_resources.UnknownExtra):
|
2017-03-01 15:46:13 +01:00
|
|
|
continue
|
2017-03-10 15:45:46 +01:00
|
|
|
try:
|
|
|
|
yield _fallback
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def get_plugin(*args, **kwargs):
|
|
|
|
"""Returns first available plugin `from get_plugins()` if any."""
|
|
|
|
for plugin in get_plugins(*args, **kwargs):
|
|
|
|
return plugin
|