[py-svn] r63806 - in py/trunk/py: . misc/testing

hpk at codespeak.net hpk at codespeak.net
Tue Apr 7 22:22:54 CEST 2009


Author: hpk
Date: Tue Apr  7 22:22:52 2009
New Revision: 63806

Modified:
   py/trunk/py/__init__.py
   py/trunk/py/_com.py
   py/trunk/py/misc/testing/test_com.py
Log:
adding a MultiAPI helper for managing plugin APIs.


Modified: py/trunk/py/__init__.py
==============================================================================
--- py/trunk/py/__init__.py	(original)
+++ py/trunk/py/__init__.py	Tue Apr  7 22:22:52 2009
@@ -57,6 +57,7 @@
     '_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',),

Modified: py/trunk/py/_com.py
==============================================================================
--- py/trunk/py/_com.py	(original)
+++ py/trunk/py/_com.py	Tue Apr  7 22:22:52 2009
@@ -158,4 +158,26 @@
         #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()

Modified: py/trunk/py/misc/testing/test_com.py
==============================================================================
--- py/trunk/py/misc/testing/test_com.py	(original)
+++ py/trunk/py/misc/testing/test_com.py	Tue Apr  7 22:22:52 2009
@@ -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 @@
         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')



More information about the pytest-commit mailing list