[svn r63806] adding a MultiAPI helper for managing plugin APIs.

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-07 22:22:52 +02:00
parent 9aed6ddcd0
commit 50664c1e17
3 changed files with 45 additions and 2 deletions

View File

@ -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',),

View File

@ -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 "<MulticallMaker %r %s>" %(self.name, self.plugins)
def __call__(self, *args, **kwargs):
mc = MultiCall(self.plugins.listattr(self.name), *args, **kwargs)
return mc.execute()
pyplugins = PyPlugins()

View File

@ -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')