slight cleanup of plugin register() functionality

--HG--
branch : plugin_no_pytest
This commit is contained in:
holger krekel 2015-04-22 14:54:28 +02:00
parent d632a0d5c2
commit f746c190ac
2 changed files with 21 additions and 17 deletions

View File

@ -121,6 +121,12 @@ class PytestPluginManager(PluginManager):
self._globalplugins.append(plugin) self._globalplugins.append(plugin)
return ret return ret
def _do_register(self, plugin, name):
# called from core PluginManager class
if hasattr(self, "config"):
self.config._register_plugin(plugin, name)
return super(PytestPluginManager, self)._do_register(plugin, name)
def unregister(self, plugin): def unregister(self, plugin):
super(PytestPluginManager, self).unregister(plugin) super(PytestPluginManager, self).unregister(plugin)
try: try:
@ -701,7 +707,6 @@ class Config(object):
self._opt2dest = {} self._opt2dest = {}
self._cleanup = [] self._cleanup = []
self.pluginmanager.register(self, "pytestconfig") self.pluginmanager.register(self, "pytestconfig")
self.pluginmanager.set_register_callback(self._register_plugin)
self._configured = False self._configured = False
def _register_plugin(self, plugin, name): def _register_plugin(self, plugin, name):

View File

@ -167,10 +167,6 @@ class PluginManager(object):
# backward compatibility # backward compatibility
config.do_configure() config.do_configure()
def set_register_callback(self, callback):
assert not hasattr(self, "_registercallback")
self._registercallback = callback
def make_hook_caller(self, name, plugins): def make_hook_caller(self, name, plugins):
caller = getattr(self.hook, name) caller = getattr(self.hook, name)
methods = self.listattr(name, plugins=plugins) methods = self.listattr(name, plugins=plugins)
@ -204,22 +200,26 @@ class PluginManager(object):
", ".join(hook.argnames)) ", ".join(hook.argnames))
yield hook yield hook
def _get_canonical_name(self, plugin):
return getattr(plugin, "__name__", None) or str(id(plugin))
def register(self, plugin, name=None): def register(self, plugin, name=None):
name = name or self._get_canonical_name(plugin)
if self._name2plugin.get(name, None) == -1: if self._name2plugin.get(name, None) == -1:
return return
name = name or getattr(plugin, '__name__', str(id(plugin))) if self.hasplugin(name):
if self.isregistered(plugin, name):
raise ValueError("Plugin already registered: %s=%s\n%s" %( raise ValueError("Plugin already registered: %s=%s\n%s" %(
name, plugin, self._name2plugin)) name, plugin, self._name2plugin))
#self.trace("registering", name, plugin) #self.trace("registering", name, plugin)
reg = getattr(self, "_registercallback", None) # allow subclasses to intercept here by calling a helper
if reg is not None: return self._do_register(plugin, name)
reg(plugin, name) # may call addhooks
def _do_register(self, plugin, name):
hookcallers = list(self._scan_plugin(plugin)) hookcallers = list(self._scan_plugin(plugin))
self._plugin2hookcallers[plugin] = hookcallers self._plugin2hookcallers[plugin] = hookcallers
self._name2plugin[name] = plugin self._name2plugin[name] = plugin
self._plugins.append(plugin) self._plugins.append(plugin)
# finally make sure that the methods of the new plugin take part # rescan all methods for the hookcallers we found
for hookcaller in hookcallers: for hookcaller in hookcallers:
hookcaller.scan_methods() hookcaller.scan_methods()
return True return True
@ -243,11 +243,6 @@ class PluginManager(object):
self._plugins = [] self._plugins = []
self._name2plugin.clear() self._name2plugin.clear()
def isregistered(self, plugin, name=None):
if self.getplugin(name) is not None:
return True
return plugin in self._plugins
def addhooks(self, module_or_class): def addhooks(self, module_or_class):
isclass = int(inspect.isclass(module_or_class)) isclass = int(inspect.isclass(module_or_class))
names = [] names = []
@ -266,8 +261,12 @@ class PluginManager(object):
def getplugins(self): def getplugins(self):
return self._plugins return self._plugins
def isregistered(self, plugin):
return self.hasplugin(self._get_canonical_name(plugin)) or \
plugin in self._plugins
def hasplugin(self, name): def hasplugin(self, name):
return bool(self.getplugin(name)) return name in self._name2plugin
def getplugin(self, name): def getplugin(self, name):
return self._name2plugin.get(name) return self._name2plugin.get(name)