diff --git a/py/__init__.py b/py/__init__.py index 3b077bb48..e6fe8dcfb 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -25,8 +25,8 @@ version = "1.0.0b1" initpkg(__name__, description = "pylib and py.test: agile development and test support library", - revision = int('$LastChangedRevision: 63580 $'.split(':')[1][:-1]), - lastchangedate = '$LastChangedDate: 2009-04-03 19:45:25 +0200 (Fri, 03 Apr 2009) $', + revision = int('$LastChangedRevision: 63806 $'.split(':')[1][:-1]), + lastchangedate = '$LastChangedDate: 2009-04-07 22:22:52 +0200 (Tue, 07 Apr 2009) $', version = version, url = "http://pylib.org", download_url = "http://codespeak.net/py/%s/download.html" % version, @@ -57,6 +57,7 @@ initpkg(__name__, '_com.PyPlugins' : ('./_com.py', 'PyPlugins'), '_com.MultiCall' : ('./_com.py', 'MultiCall'), '_com.pyplugins' : ('./_com.py', 'pyplugins'), + '_com.MultiAPI' : ('./_com.py', 'MultiAPI'), # py lib cmdline tools 'cmdline.pytest' : ('./cmdline/pytest.py', 'main',), diff --git a/py/_com.py b/py/_com.py index f9803c854..152a1f374 100644 --- a/py/_com.py +++ b/py/_com.py @@ -158,4 +158,26 @@ class PyPlugins: #print "calling anonymous hooks", args, kwargs MultiCall(self.listattr("pyevent"), eventname, args, kwargs).execute() + +class MultiAPI: + def __init__(self, apiclass, plugins, prefix): + for fullname in vars(apiclass): + if fullname[:2] != "__": + assert fullname.startswith(prefix) + name = fullname[len(prefix):] + mm = CallMaker(plugins, fullname) + setattr(self, name, mm) + +class CallMaker: + def __init__(self, plugins, name): + self.plugins = plugins + self.name = name + + def __repr__(self): + return "" %(self.name, self.plugins) + + def __call__(self, *args, **kwargs): + mc = MultiCall(self.plugins.listattr(self.name), *args, **kwargs) + return mc.execute() + pyplugins = PyPlugins() diff --git a/py/misc/testing/test_com.py b/py/misc/testing/test_com.py index 4e54fe2cb..d56872927 100644 --- a/py/misc/testing/test_com.py +++ b/py/misc/testing/test_com.py @@ -2,6 +2,7 @@ import py import os from py._com import PyPlugins, MultiCall +from py._com import MultiAPI pytest_plugins = "xfail" @@ -248,3 +249,22 @@ class TestPyPluginsEvents: plugins.notify("name", 13, x=15) assert l == [(13, ), {'x':15}] + +class TestMulticallMaker: + def test_happypath(self): + plugins = PyPlugins() + class Api: + def xyz_hello(self, arg): + pass + + mcm = MultiAPI(apiclass=Api, plugins=plugins, prefix="xyz_") + assert hasattr(mcm, 'hello') + assert repr(mcm.hello).find("xyz_hello") != -1 + assert not hasattr(mcm, 'xyz_hello') + class Plugin: + def xyz_hello(self, arg): + return arg + 1 + plugins.register(Plugin()) + l = mcm.hello(3) + assert l == [4] + assert not hasattr(mcm, 'world')