[Zope-Checkins] SVN: Products.Five/trunk/ Now you can use the old registry with the new API for registerig components.

Lennart Regebro regebro at gmail.com
Thu Jul 27 10:15:47 EDT 2006


Log message for revision 69271:
  Now you can use the old registry with the new API for registerig components.
  BBB support shold be pretty much complete.
  

Changed:
  U   Products.Five/trunk/doc/localsite.txt
  U   Products.Five/trunk/site/localsite.py
  U   Products.Five/trunk/site/tests/test_utility.py

-=-
Modified: Products.Five/trunk/doc/localsite.txt
===================================================================
--- Products.Five/trunk/doc/localsite.txt	2006-07-27 13:51:25 UTC (rev 69270)
+++ Products.Five/trunk/doc/localsite.txt	2006-07-27 14:15:46 UTC (rev 69271)
@@ -89,10 +89,8 @@
 
 The rest of this document documents the details of the old site implementation.
 Everything from here on concerns only the old implementation and is of mainly
-hysterical interest:
+hysterical interest.
 
-
-
 By default, Zope 3 has a global site which is configured through ZCML.
 It provides the fallback for all component look-up.  Local sites are
 typically set during traversal, when the traverser encounters an

Modified: Products.Five/trunk/site/localsite.py
===================================================================
--- Products.Five/trunk/site/localsite.py	2006-07-27 13:51:25 UTC (rev 69270)
+++ Products.Five/trunk/site/localsite.py	2006-07-27 14:15:46 UTC (rev 69271)
@@ -16,12 +16,26 @@
 $Id$
 """
 from zope.interface import implements
+from zope.interface.interface import InterfaceClass
 from zope.component import getGlobalSiteManager
 from zope.component.interfaces import ComponentLookupError
 from zope.app.component.interfaces import ISite, IPossibleSite
 from Acquisition import aq_parent, aq_inner
 from Products.Five.site.interfaces import IFiveSiteManager, IFiveUtilityRegistry
 
+from operator import xor
+def one_of_three(a, b, c):
+    # Logical table for a three part test where only one can be true:
+    # 0 0 0: 0
+    # 0 0 1: 1
+    # 0 1 0: 1
+    # 0 1 1: 0
+    # 1 0 0: 1
+    # 1 0 1: 0
+    # 1 1 0: 0
+    # 1 1 1: 0
+    return xor(xor(a, b), c) and not (a and b and c)
+
 class FiveSiteManager(object):
     implements(IFiveSiteManager)
 
@@ -89,7 +103,56 @@
     def getAllUtilitiesRegisteredFor(self, interface):
         return self.utilities.getAllUtilitiesRegisteredFor(interface)
 
-    def registerUtility(self, interface, utility, name=''):
+    def registerUtility(self, *args, **kw):
+        # Can be called with new API:
+        #   component, provided=None, name=u'', info=u'', event=True
+        # where info and event are ignored, or old api:
+        #   interface, utility, name=''
+        name = kw.get('name', u'')
+        interface_kw = kw.get('interface', None)
+        provided_kw = kw.get('provided', None)
+        utility_kw = kw.get('utility', None)
+        component_kw = kw.get('component', None)
+
+        interface = None                      
+        utility = None
+        if len(args) > 0:
+            # Positional argument 1
+            if isinstance(args[0], InterfaceClass):
+                interface = args[0]
+            else:
+                utility = args[0]
+
+        if len(args) > 1:
+            if isinstance(args[1], InterfaceClass):
+                interface = args[1]
+            else:
+                utility = args[1]
+
+        if len(args) > 2:
+            if name:
+                raise TypeError("You can only provide one name")
+            else:
+                name = args[2]
+
+        if not one_of_three(interface is not None, 
+                            interface_kw is not None,
+                            provided_kw is not None):
+            raise TypeError("You can specify one and only one interface")
+        if interface is None:
+            interface = interface_kw
+        if interface is None:
+            interface = provided_kw
+
+        if not one_of_three(utility is not None, 
+                            utility_kw is not None,
+                            component_kw is not None):
+            raise TypeError("You can specify one and only one interface")
+        if utility is None:
+            utility = utility_kw
+        if utility is None:
+            utility = component_kw
+            
         return self.utilities.registerUtility(interface, utility, name)
 
 class FiveSite:
@@ -121,9 +184,8 @@
 def disableLocalSiteHook(obj):
     """Remove __before_traverse__ hook for Local Site
     """
-    warnings.warn("The disableLocalSiteHook is deprecated and will be removed "
-                  "in Zope 2.12. \nSee Five/doc/localsite.txt .",
-                  DeprecationWarning, 2)
+    # This method is of course deprecated too, but issuing a warning is
+    # silly.
     disableSite(obj)
     clearSite()
     obj.setSiteManager(None)

Modified: Products.Five/trunk/site/tests/test_utility.py
===================================================================
--- Products.Five/trunk/site/tests/test_utility.py	2006-07-27 13:51:25 UTC (rev 69270)
+++ Products.Five/trunk/site/tests/test_utility.py	2006-07-27 14:15:46 UTC (rev 69271)
@@ -118,6 +118,58 @@
         self.assertEquals(list(zapi.getAllUtilitiesRegisteredFor(
             IDummyUtility)), [dummy])
 
+    def test_registerUtilityWithZopeComponentAPI1(self):
+        # With positional arguments
+        sm = getSiteManager()
+        dummy = DummyUtility()
+
+        sm.registerUtility(dummy, IDummyUtility, 'dummy')
+
+        self.assertEquals(zapi.getUtility(IDummyUtility, name='dummy'), dummy)
+        self.assertEquals(list(zapi.getUtilitiesFor(IDummyUtility)), 
+                          [('dummy', dummy)])
+        self.assertEquals(list(zapi.getAllUtilitiesRegisteredFor(
+            IDummyUtility)), [dummy])
+
+    def test_registerUtilityWithZopeComponentAPI1(self):
+        # Without name
+        sm = getSiteManager()
+        dummy = DummyUtility()
+
+        sm.registerUtility(dummy, IDummyUtility)
+
+        self.assertEquals(zapi.getUtility(IDummyUtility), dummy)
+        self.assertEquals(list(zapi.getUtilitiesFor(IDummyUtility)), 
+                          [('', dummy)])
+        self.assertEquals(list(zapi.getAllUtilitiesRegisteredFor(
+            IDummyUtility)), [dummy])
+
+    def test_registerUtilityWithZopeComponentAPI3(self):
+        # With keyword arguments
+        sm = getSiteManager()
+        dummy = DummyUtility()
+
+        sm.registerUtility(component=dummy, provided=IDummyUtility, 
+                           name='dummy')
+        self.assertEquals(zapi.getUtility(IDummyUtility, name='dummy'), dummy)
+        self.assertEquals(list(zapi.getUtilitiesFor(IDummyUtility)), 
+                          [('dummy', dummy)])
+        self.assertEquals(list(zapi.getAllUtilitiesRegisteredFor(
+            IDummyUtility)), [dummy])
+        
+    def test_registerUtilityWithZopeComponentAPI4(self):
+        # The Full kabob:
+        sm = getSiteManager()
+        dummy = DummyUtility()
+        
+        sm.registerUtility(component=dummy, provided=IDummyUtility, 
+                           name='dummy', info=u'The Dummy', event=True)
+        self.assertEquals(zapi.getUtility(IDummyUtility, name='dummy'), dummy)
+        self.assertEquals(list(zapi.getUtilitiesFor(IDummyUtility)), 
+                          [('dummy', dummy)])
+        self.assertEquals(list(zapi.getAllUtilitiesRegisteredFor(
+            IDummyUtility)), [dummy])
+
     def test_registerTwoUtilitiesWithSameNameDifferentInterface(self):
         sm = getSiteManager()
         self.failUnless(IRegisterUtilitySimply.providedBy(sm))



More information about the Zope-Checkins mailing list