mirror of
https://github.com/certbot/certbot.git
synced 2026-08-01 19:14:54 +02:00
PluginsRegistry.find_init
This commit is contained in:
@@ -16,6 +16,9 @@ class PluginEntryPoint(object):
|
|||||||
PREFIX_FREE_DISTRIBUTIONS = ["letsencrypt"]
|
PREFIX_FREE_DISTRIBUTIONS = ["letsencrypt"]
|
||||||
"""Distributions for which prefix will be omitted."""
|
"""Distributions for which prefix will be omitted."""
|
||||||
|
|
||||||
|
# this object is mutable, don't allow it to be hashed!
|
||||||
|
__hash__ = None
|
||||||
|
|
||||||
def __init__(self, entry_point):
|
def __init__(self, entry_point):
|
||||||
self.name = self.entry_point_to_plugin_name(entry_point)
|
self.name = self.entry_point_to_plugin_name(entry_point)
|
||||||
self.plugin_cls = entry_point.load()
|
self.plugin_cls = entry_point.load()
|
||||||
@@ -188,6 +191,29 @@ class PluginsRegistry(collections.Mapping):
|
|||||||
return self.filter(lambda p_ep: p_ep.available)
|
return self.filter(lambda p_ep: p_ep.available)
|
||||||
# succefully prepared + misconfigured
|
# succefully prepared + misconfigured
|
||||||
|
|
||||||
|
def find_init(self, plugin):
|
||||||
|
"""Find an initialized plugin.
|
||||||
|
|
||||||
|
This is particularly useful for finding a name for the plugin
|
||||||
|
(although `.IPluginFactory.__call__` takes ``name`` as one of
|
||||||
|
the arguments, ``IPlugin.name`` is not part of the interface)::
|
||||||
|
|
||||||
|
# plugin is an instance providing IPlugin, initialized
|
||||||
|
# somewhere else in the code
|
||||||
|
plugin_registry.find_init(plugin).name
|
||||||
|
|
||||||
|
Returns ``None`` if ``plugin`` is not found in the registry.
|
||||||
|
|
||||||
|
"""
|
||||||
|
# use list instead of set beacse PluginEntryPoint is not hashable
|
||||||
|
candidates = [plugin_ep for plugin_ep in self.plugins.itervalues()
|
||||||
|
if plugin_ep.initialized and plugin_ep.init() is plugin]
|
||||||
|
assert len(candidates) <= 1
|
||||||
|
if candidates:
|
||||||
|
return candidates[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{0}({1!r})".format(
|
return "{0}({1!r})".format(
|
||||||
self.__class__.__name__, set(self.plugins.itervalues()))
|
self.__class__.__name__, set(self.plugins.itervalues()))
|
||||||
|
|||||||
@@ -216,6 +216,12 @@ class PluginsRegistryTest(unittest.TestCase):
|
|||||||
self.plugin_ep.available = False
|
self.plugin_ep.available = False
|
||||||
self.assertEqual({}, self.reg.available().plugins)
|
self.assertEqual({}, self.reg.available().plugins)
|
||||||
|
|
||||||
|
def test_find_init(self):
|
||||||
|
self.assertTrue(self.reg.find_init(mock.Mock()) is None)
|
||||||
|
self.plugin_ep.initalized = True
|
||||||
|
self.assertTrue(
|
||||||
|
self.reg.find_init(self.plugin_ep.init()) is self.plugin_ep)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock"
|
self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock"
|
||||||
self.assertEqual("PluginsRegistry(set([PluginEntryPoint#mock]))",
|
self.assertEqual("PluginsRegistry(set([PluginEntryPoint#mock]))",
|
||||||
|
|||||||
Reference in New Issue
Block a user