[svn r63899] more consistent naming
--HG-- branch : trunk
This commit is contained in:
parent
a4863c3f7b
commit
248093e61a
32
py/_com.py
32
py/_com.py
|
@ -71,27 +71,27 @@ class Registry:
|
||||||
def __init__(self, plugins=None):
|
def __init__(self, plugins=None):
|
||||||
if plugins is None:
|
if plugins is None:
|
||||||
plugins = []
|
plugins = []
|
||||||
self.plugins = plugins
|
self._plugins = plugins
|
||||||
|
|
||||||
def register(self, plugin):
|
def register(self, plugin):
|
||||||
assert not isinstance(plugin, str)
|
assert not isinstance(plugin, str)
|
||||||
self.call_each("pytest_plugin_registered", plugin)
|
self.call_each("pytest_plugin_registered", plugin)
|
||||||
self.plugins.append(plugin)
|
self._plugins.append(plugin)
|
||||||
|
|
||||||
def unregister(self, plugin):
|
def unregister(self, plugin):
|
||||||
self.call_each("pytest_plugin_unregistered", plugin)
|
self.call_each("pytest_plugin_unregistered", plugin)
|
||||||
self.plugins.remove(plugin)
|
self._plugins.remove(plugin)
|
||||||
|
|
||||||
def isregistered(self, plugin):
|
def isregistered(self, plugin):
|
||||||
return plugin in self.plugins
|
return plugin in self._plugins
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self.plugins)
|
return iter(self._plugins)
|
||||||
|
|
||||||
def listattr(self, attrname, plugins=None, extra=(), reverse=False):
|
def listattr(self, attrname, plugins=None, extra=(), reverse=False):
|
||||||
l = []
|
l = []
|
||||||
if plugins is None:
|
if plugins is None:
|
||||||
plugins = self.plugins
|
plugins = self._plugins
|
||||||
if extra:
|
if extra:
|
||||||
plugins += list(extra)
|
plugins += list(extra)
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
|
@ -117,31 +117,31 @@ class Registry:
|
||||||
|
|
||||||
|
|
||||||
class PluginAPI:
|
class PluginAPI:
|
||||||
def __init__(self, apiclass, plugins=None):
|
def __init__(self, apiclass, registry=None):
|
||||||
self._apiclass = apiclass
|
self._apiclass = apiclass
|
||||||
if plugins is None:
|
if registry is None:
|
||||||
plugins = comregistry
|
registry = comregistry
|
||||||
self.plugins = plugins
|
self.registry = registry
|
||||||
for name, method in vars(apiclass).items():
|
for name, method in vars(apiclass).items():
|
||||||
if name[:2] != "__":
|
if name[:2] != "__":
|
||||||
firstresult = getattr(method, 'firstresult', False)
|
firstresult = getattr(method, 'firstresult', False)
|
||||||
mm = ApiCall(plugins, name, firstresult=firstresult)
|
mm = ApiCall(registry, name, firstresult=firstresult)
|
||||||
setattr(self, name, mm)
|
setattr(self, name, mm)
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<PluginAPI %r %r>" %(self._apiclass, self.plugins)
|
return "<PluginAPI %r %r>" %(self._apiclass, self._plugins)
|
||||||
|
|
||||||
class ApiCall:
|
class ApiCall:
|
||||||
def __init__(self, plugins, name, firstresult):
|
def __init__(self, registry, name, firstresult):
|
||||||
self.plugins = plugins
|
self.registry = registry
|
||||||
self.name = name
|
self.name = name
|
||||||
self.firstresult = firstresult
|
self.firstresult = firstresult
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
mode = self.firstresult and "firstresult" or "each"
|
mode = self.firstresult and "firstresult" or "each"
|
||||||
return "<ApiCall %r mode=%s %s>" %(self.name, mode, self.plugins)
|
return "<ApiCall %r mode=%s %s>" %(self.name, mode, self.registry)
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
mc = MultiCall(self.plugins.listattr(self.name), *args, **kwargs)
|
mc = MultiCall(self.registry.listattr(self.name), *args, **kwargs)
|
||||||
#print "making multicall", self
|
#print "making multicall", self
|
||||||
return mc.execute(firstresult=self.firstresult)
|
return mc.execute(firstresult=self.firstresult)
|
||||||
|
|
||||||
|
|
|
@ -165,37 +165,37 @@ def test_api_and_defaults():
|
||||||
|
|
||||||
class TestPluginAPI:
|
class TestPluginAPI:
|
||||||
def test_happypath(self):
|
def test_happypath(self):
|
||||||
plugins = Registry()
|
registry = Registry()
|
||||||
class Api:
|
class Api:
|
||||||
def hello(self, arg):
|
def hello(self, arg):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
mcm = PluginAPI(apiclass=Api, plugins=plugins)
|
mcm = PluginAPI(apiclass=Api, registry=registry)
|
||||||
assert hasattr(mcm, 'hello')
|
assert hasattr(mcm, 'hello')
|
||||||
assert repr(mcm.hello).find("hello") != -1
|
assert repr(mcm.hello).find("hello") != -1
|
||||||
class Plugin:
|
class Plugin:
|
||||||
def hello(self, arg):
|
def hello(self, arg):
|
||||||
return arg + 1
|
return arg + 1
|
||||||
plugins.register(Plugin())
|
registry.register(Plugin())
|
||||||
l = mcm.hello(3)
|
l = mcm.hello(3)
|
||||||
assert l == [4]
|
assert l == [4]
|
||||||
assert not hasattr(mcm, 'world')
|
assert not hasattr(mcm, 'world')
|
||||||
|
|
||||||
def test_firstresult(self):
|
def test_firstresult(self):
|
||||||
plugins = Registry()
|
registry = Registry()
|
||||||
class Api:
|
class Api:
|
||||||
def hello(self, arg): pass
|
def hello(self, arg): pass
|
||||||
hello.firstresult = True
|
hello.firstresult = True
|
||||||
|
|
||||||
mcm = PluginAPI(apiclass=Api, plugins=plugins)
|
mcm = PluginAPI(apiclass=Api, registry=registry)
|
||||||
class Plugin:
|
class Plugin:
|
||||||
def hello(self, arg):
|
def hello(self, arg):
|
||||||
return arg + 1
|
return arg + 1
|
||||||
plugins.register(Plugin())
|
registry.register(Plugin())
|
||||||
res = mcm.hello(3)
|
res = mcm.hello(3)
|
||||||
assert res == 4
|
assert res == 4
|
||||||
|
|
||||||
def test_default_plugins(self):
|
def test_default_plugins(self):
|
||||||
class Api: pass
|
class Api: pass
|
||||||
mcm = PluginAPI(apiclass=Api)
|
mcm = PluginAPI(apiclass=Api)
|
||||||
assert mcm.plugins == py._com.comregistry
|
assert mcm.registry == py._com.comregistry
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
handling py.test plugins.
|
managing loading and interacting with pytest plugins.
|
||||||
"""
|
"""
|
||||||
import py
|
import py
|
||||||
from py.__.test.plugin import api
|
from py.__.test.plugin import api
|
||||||
|
@ -10,11 +10,11 @@ class PluginManager(object):
|
||||||
comregistry = py._com.Registry()
|
comregistry = py._com.Registry()
|
||||||
self.comregistry = comregistry
|
self.comregistry = comregistry
|
||||||
self.MultiCall = self.comregistry.MultiCall
|
self.MultiCall = self.comregistry.MultiCall
|
||||||
self.plugins = {}
|
self.impname2plugin = {}
|
||||||
|
|
||||||
self.api = py._com.PluginAPI(
|
self.api = py._com.PluginAPI(
|
||||||
apiclass=api.PluginHooks,
|
apiclass=api.PluginHooks,
|
||||||
plugins=self.comregistry)
|
registry=self.comregistry)
|
||||||
|
|
||||||
def register(self, plugin):
|
def register(self, plugin):
|
||||||
self.comregistry.register(plugin)
|
self.comregistry.register(plugin)
|
||||||
|
@ -24,13 +24,13 @@ class PluginManager(object):
|
||||||
return self.comregistry.isregistered(plugin)
|
return self.comregistry.isregistered(plugin)
|
||||||
|
|
||||||
def getplugins(self):
|
def getplugins(self):
|
||||||
return self.comregistry.plugins
|
return list(self.comregistry)
|
||||||
|
|
||||||
# API for bootstrapping
|
# API for bootstrapping
|
||||||
#
|
#
|
||||||
def getplugin(self, importname):
|
def getplugin(self, importname):
|
||||||
impname, clsname = canonical_names(importname)
|
impname, clsname = canonical_names(importname)
|
||||||
return self.plugins[impname]
|
return self.impname2plugin[impname]
|
||||||
|
|
||||||
def _envlist(self, varname):
|
def _envlist(self, varname):
|
||||||
val = py.std.os.environ.get(varname, None)
|
val = py.std.os.environ.get(varname, None)
|
||||||
|
@ -44,8 +44,8 @@ class PluginManager(object):
|
||||||
|
|
||||||
def consider_conftest(self, conftestmodule):
|
def consider_conftest(self, conftestmodule):
|
||||||
cls = getattr(conftestmodule, 'ConftestPlugin', None)
|
cls = getattr(conftestmodule, 'ConftestPlugin', None)
|
||||||
if cls is not None and cls not in self.plugins:
|
if cls is not None and cls not in self.impname2plugin:
|
||||||
self.plugins[cls] = True
|
self.impname2plugin[cls] = True
|
||||||
self.register(cls())
|
self.register(cls())
|
||||||
self.consider_module(conftestmodule)
|
self.consider_module(conftestmodule)
|
||||||
|
|
||||||
|
@ -60,11 +60,11 @@ class PluginManager(object):
|
||||||
def import_plugin(self, spec):
|
def import_plugin(self, spec):
|
||||||
assert isinstance(spec, str)
|
assert isinstance(spec, str)
|
||||||
modname, clsname = canonical_names(spec)
|
modname, clsname = canonical_names(spec)
|
||||||
if modname in self.plugins:
|
if modname in self.impname2plugin:
|
||||||
return
|
return
|
||||||
mod = importplugin(modname)
|
mod = importplugin(modname)
|
||||||
plugin = registerplugin(self.comregistry.register, mod, clsname)
|
plugin = registerplugin(self.comregistry.register, mod, clsname)
|
||||||
self.plugins[modname] = plugin
|
self.impname2plugin[modname] = plugin
|
||||||
self.consider_module(mod)
|
self.consider_module(mod)
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
|
@ -386,8 +386,8 @@ class Function(FunctionMixin, py.test.collect.Item):
|
||||||
def _raisefuncargerror(self, argname, prefix="pytest_funcarg__"):
|
def _raisefuncargerror(self, argname, prefix="pytest_funcarg__"):
|
||||||
metainfo = self.repr_metainfo()
|
metainfo = self.repr_metainfo()
|
||||||
available = []
|
available = []
|
||||||
plugins = self.config.pluginmanager.plugins.values()
|
plugins = list(self.config.pluginmanager.comregistry)
|
||||||
plugins.extend(self.config.pluginmanager.comregistry.plugins)
|
#plugins.extend(self.config.pluginmanager.registry.plugins)
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
for name in vars(plugin.__class__):
|
for name in vars(plugin.__class__):
|
||||||
if name.startswith(prefix):
|
if name.startswith(prefix):
|
||||||
|
|
Loading…
Reference in New Issue