mirror of
https://github.com/certbot/certbot.git
synced 2026-06-07 07:42:08 -04:00
PluginsRegistry.find_init
This commit is contained in:
parent
e197f156a7
commit
75a7b7605b
2 changed files with 32 additions and 0 deletions
|
|
@ -16,6 +16,9 @@ class PluginEntryPoint(object):
|
|||
PREFIX_FREE_DISTRIBUTIONS = ["letsencrypt"]
|
||||
"""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):
|
||||
self.name = self.entry_point_to_plugin_name(entry_point)
|
||||
self.plugin_cls = entry_point.load()
|
||||
|
|
@ -188,6 +191,29 @@ class PluginsRegistry(collections.Mapping):
|
|||
return self.filter(lambda p_ep: p_ep.available)
|
||||
# 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):
|
||||
return "{0}({1!r})".format(
|
||||
self.__class__.__name__, set(self.plugins.itervalues()))
|
||||
|
|
|
|||
|
|
@ -216,6 +216,12 @@ class PluginsRegistryTest(unittest.TestCase):
|
|||
self.plugin_ep.available = False
|
||||
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):
|
||||
self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock"
|
||||
self.assertEqual("PluginsRegistry(set([PluginEntryPoint#mock]))",
|
||||
|
|
|
|||
Loading…
Reference in a new issue