ensure proper get_name references

--HG--
branch : more_plugin
This commit is contained in:
holger krekel 2015-04-25 20:42:41 +02:00
parent 3a1374e69c
commit a042c57227
2 changed files with 29 additions and 3 deletions

View File

@ -237,6 +237,10 @@ class PluginManager(object):
return TracedHookExecution(self, before, after).undo return TracedHookExecution(self, before, after).undo
def make_hook_caller(self, name, plugins): def make_hook_caller(self, name, plugins):
""" Return a new HookCaller instance which manages calls to
all methods named "name" in the plugins. The new hook caller
is registered internally such that when one of the plugins gets
unregistered, its method will be removed from the hook caller. """
caller = getattr(self.hook, name) caller = getattr(self.hook, name)
hc = HookCaller(caller.name, self._hookexec, caller._specmodule_or_class) hc = HookCaller(caller.name, self._hookexec, caller._specmodule_or_class)
for plugin in plugins: for plugin in plugins:
@ -248,7 +252,7 @@ class PluginManager(object):
return hc return hc
def get_canonical_name(self, plugin): def get_canonical_name(self, plugin):
""" Return canonical name for the plugin object. """ """ Return canonical name for a plugin object. """
return getattr(plugin, "__name__", None) or str(id(plugin)) return getattr(plugin, "__name__", None) or str(id(plugin))
def register(self, plugin, name=None): def register(self, plugin, name=None):
@ -288,7 +292,7 @@ class PluginManager(object):
be specified. """ be specified. """
if name is None: if name is None:
assert plugin is not None assert plugin is not None
name = self.get_canonical_name(plugin) name = self.get_name(plugin)
if plugin is None: if plugin is None:
plugin = self.get_plugin(name) plugin = self.get_plugin(name)
@ -340,9 +344,15 @@ class PluginManager(object):
""" Return a plugin or None for the given name. """ """ Return a plugin or None for the given name. """
return self._name2plugin.get(name) return self._name2plugin.get(name)
def get_name(self, plugin):
""" Return name for registered plugin or None if not registered. """
for name, val in self._name2plugin.items():
if plugin == val:
return name
def _verify_hook(self, hook, plugin): def _verify_hook(self, hook, plugin):
method = getattr(plugin, hook.name) method = getattr(plugin, hook.name)
pluginname = self.get_canonical_name(plugin) pluginname = self.get_name(plugin)
if hook.is_historic() and hasattr(method, "hookwrapper"): if hook.is_historic() and hasattr(method, "hookwrapper"):
raise PluginValidationError( raise PluginValidationError(

View File

@ -34,6 +34,22 @@ class TestPluginManager:
assert pm.unregister(a1) == a1 assert pm.unregister(a1) == a1
assert not pm.is_registered(a1) assert not pm.is_registered(a1)
def test_pm_name(self, pm):
class A: pass
a1 = A()
name = pm.register(a1, name="hello")
assert name == "hello"
pm.unregister(a1)
assert pm.get_plugin(a1) is None
assert not pm.is_registered(a1)
assert not pm.get_plugins()
name2 = pm.register(a1, name="hello")
assert name2 == name
pm.unregister(name="hello")
assert pm.get_plugin(a1) is None
assert not pm.is_registered(a1)
assert not pm.get_plugins()
def test_set_blocked(self, pm): def test_set_blocked(self, pm):
class A: pass class A: pass
a1 = A() a1 = A()