From 75a7b7605b82945a8701f34df414da98411afd52 Mon Sep 17 00:00:00 2001 From: Jakub Warmuz Date: Sat, 9 May 2015 07:25:36 +0000 Subject: [PATCH] PluginsRegistry.find_init --- letsencrypt/client/plugins/disco.py | 26 ++++++++++++++++++++++++ letsencrypt/client/plugins/disco_test.py | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/letsencrypt/client/plugins/disco.py b/letsencrypt/client/plugins/disco.py index f4a9faecf..fd636d59a 100644 --- a/letsencrypt/client/plugins/disco.py +++ b/letsencrypt/client/plugins/disco.py @@ -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())) diff --git a/letsencrypt/client/plugins/disco_test.py b/letsencrypt/client/plugins/disco_test.py index 945f72d6b..ca3bc42d4 100644 --- a/letsencrypt/client/plugins/disco_test.py +++ b/letsencrypt/client/plugins/disco_test.py @@ -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]))",