[Checkins] SVN: zope3org/trunk/src/zorg/usage/ Tests are now complete for adapter and utility registrations

Uwe Oestermeier uwe_oestermeier at iwm-kmrc.de
Sun Apr 9 16:11:32 EDT 2006


Log message for revision 66749:
  Tests are now complete for adapter and utility registrations

Changed:
  U   zope3org/trunk/src/zorg/usage/README.txt
  U   zope3org/trunk/src/zorg/usage/__init__.py
  D   zope3org/trunk/src/zorg/usage/testadapter.py
  A   zope3org/trunk/src/zorg/usage/testcomponent.py

-=-
Modified: zope3org/trunk/src/zorg/usage/README.txt
===================================================================
--- zope3org/trunk/src/zorg/usage/README.txt	2006-04-09 20:10:02 UTC (rev 66748)
+++ zope3org/trunk/src/zorg/usage/README.txt	2006-04-09 20:11:32 UTC (rev 66749)
@@ -67,26 +67,38 @@
 Don't worry
 
 That after all is not very different from calling zope.component.provideAdapter.
-The interesting part comes now. We can declare the usage in the class itself
+The interesting part is that we can declare the usage in the class itself
 and register all usages of a module with a single call. With n adapters in
-a single module you safe the typing of n test registrations and n zcml
+a single module you can safe the typing of n test registrations and n zcml
 adapter statements.
 
 The ensureRegistrations function collects all usage descriptions in a single
 module and registers them:
 
->>> zorg.usage.ensureRegistrations("zorg.usage.testadapter")
+>>> zorg.usage.ensureRegistrations("zorg.usage.testcomponent")
 
->>> from zorg.usage.testadapter import First, Second, ICount, IIncrement
+>>> from zorg.usage.testcomponent import First, Second
+>>> from zorg.usage.testcomponent import ICounter, IIncrement
+
 >>> incr = IIncrement(First())
 >>> incr.incr()
 2
 
-
-
-#>>> from zope.app import zapi
-#>>> counter = zapi.getMultiAdapter((First(), Second()), ICount, name="test")
-#>>> counter.count()
+>>> from zope.app import zapi
+>>> counter = zapi.getMultiAdapter((First(), Second()), ICounter)
+>>> counter.count()
 1
 2
 
+>>> roman = zapi.getMultiAdapter((First(), Second()), ICounter, name="roman")
+>>> roman.count()
+I
+II
+
+Since the testcomponent module also describes the use of the latter class as a 
+utility we can get the same behavior in a different way:
+
+>>> counter = zapi.getUtility(ICounter, name="verbal")
+>>> counter.count()
+one
+two
\ No newline at end of file

Modified: zope3org/trunk/src/zorg/usage/__init__.py
===================================================================
--- zope3org/trunk/src/zorg/usage/__init__.py	2006-04-09 20:10:02 UTC (rev 66748)
+++ zope3org/trunk/src/zorg/usage/__init__.py	2006-04-09 20:11:32 UTC (rev 66749)
@@ -42,6 +42,7 @@
         
     def register(self) :
         """ Registers the adapter. """
+
         zope.component.provideAdapter(self.klass, 
                                             adapts=self.adapts,
                                             provides=self.provides,
@@ -49,9 +50,18 @@
         
 class GlobalUtilityUsage(ClassUsage) :
     """ Describes the usage of a class as a factory for a global utility. """
+    
+    def __init__(self, klass, provides=None, name='') :
+        self.klass = klass
+        self.provides = provides
+        self.name = name
 
     def register(self) :
         """ Registers the global utility. """
+        
+        zope.component.provideUtility(self.klass(), 
+                                            provides=self.provides,
+                                            name=self.name)
 
 
 def _adapter(cls):
@@ -105,9 +115,52 @@
     locals['__adapter_usage_data__'] =  adapts, provides, name
     addClassAdvisor(_adapter)
     
-def globalUtility() :
-    pass # to be written
+
+def _utility(cls):
     
+    provides, name = cls.__dict__['__utility_usage_data__']
+    del cls.__utility_usage_data__
+    
+    if provides is None :
+        provides = list(cls.__implemented__.interfaces())[0]
+    else :
+        classImplements(cls, iface)
+ 
+    if not hasattr(cls, '__zorg_usages__') :
+        cls.__zorg_usages__ = []
+        
+    usage = GlobalUtilityUsage(cls, provides=provides, name=name)
+    cls.__zorg_usages__.append(usage)
+    return cls
+
+    
+def globalUtility(provides=None, name='') :
+    """
+    Declares the usage of a class as factory for a global utility.
+    
+    >>> class IUtility(zope.interface.Interface) :
+    ...     pass
+ 
+    
+    class Adapter(object) :
+    ...     zope.interface.implements(IUtility)
+    ...     globalUtility()
+    
+    """
+
+    frame = sys._getframe(1)
+    locals = frame.f_locals
+    
+    # Try to make sure we were called from a class def. In 2.2.0 we can't
+    # check for __module__ since it doesn't seem to be added to the locals
+    # until later on.
+    if (locals is frame.f_globals) or ('__module__' not in locals):
+        raise TypeError("provides can be used only from a class definition.")
+
+    locals['__utility_usage_data__'] =  provides, name
+    addClassAdvisor(_utility)
+
+    
 def ensureRegistrations(dotted_name) :
     module = resolve(dotted_name)
     for key in dir(module) :

Deleted: zope3org/trunk/src/zorg/usage/testadapter.py
===================================================================
--- zope3org/trunk/src/zorg/usage/testadapter.py	2006-04-09 20:10:02 UTC (rev 66748)
+++ zope3org/trunk/src/zorg/usage/testadapter.py	2006-04-09 20:11:32 UTC (rev 66749)
@@ -1,98 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2005 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""
-
-$Id: testadapter.py 41271 2006-01-11 17:02:07Z oestermeier $
-"""
-__docformat__ = 'restructuredtext'
-
-import zope.interface
-import zope.component
-import zorg.usage
-
-# Define some test interfaces
-
-class INumber(zope.interface.Interface) :
-
-    def number() :
-        pass
-    
-class IFirst(INumber) :
-    pass
-    
-class ISecond(INumber) :
-    pass
-    
-class IIncrement(zope.interface.Interface) :
-    
-    def incr() :
-        pass
-        
-class ICount(zope.interface.Interface) :
-
-    def count() : 
-        pass
-
-# Define some trivial base classes
-
-class First(object) :
-
-    zope.interface.implements(IFirst)
-    
-    def number(self) :
-        return 1
-
-class Second(object) :
-
-    zope.interface.implements(ISecond)
-    
-    def number(self) :
-        return 2
-
-# Define some adapter
-
-class Increment(object) :
-    """ A simple sample adapter. """
-    
-    zope.interface.implements(IIncrement)
-    zope.component.adapts(INumber)
-    
-    zorg.usage.adapter()
-    
-    def __init__(self, context) :
-        self.context = context
-        
-    def incr(self) :
-        return self.context.number() + 1
-
-
-class Count(object) :
-    """ A multiadapter. """
-
-    zope.interface.implements(ICount)
-    zope.component.adapts(IFirst, ISecond)
-    
-    zorg.usage.adapter()
-
-    def __init__(self, first, second) :
-        self.first = first
-        self.second = second
-
-    def count(self) :
-        print self.first.number()
-        print self.second.number()
-       
-class Named(object) :
-    """ A named adapter. """
-    

Copied: zope3org/trunk/src/zorg/usage/testcomponent.py (from rev 66649, zope3org/trunk/src/zorg/usage/testadapter.py)
===================================================================
--- zope3org/trunk/src/zorg/usage/testadapter.py	2006-04-07 14:36:17 UTC (rev 66649)
+++ zope3org/trunk/src/zorg/usage/testcomponent.py	2006-04-09 20:11:32 UTC (rev 66749)
@@ -0,0 +1,131 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id: testadapter.py 41271 2006-01-11 17:02:07Z oestermeier $
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.app import zapi
+
+import zope.interface
+import zope.component
+import zorg.usage
+
+# Define some test interfaces
+
+class INumber(zope.interface.Interface) :
+
+    def number() :
+        pass
+    
+class IFirst(INumber) :
+    pass
+    
+class ISecond(INumber) :
+    pass
+    
+class IIncrement(zope.interface.Interface) :
+    
+    def incr() :
+        pass
+        
+class ICounter(zope.interface.Interface) :
+
+    def count() : 
+        pass
+
+
+# Define some trivial base classes
+
+class First(object) :
+
+    zope.interface.implements(IFirst)
+    
+    def number(self) :
+        return 1
+
+class Second(object) :
+
+    zope.interface.implements(ISecond)
+    
+    def number(self) :
+        return 2
+
+# Define some adapter
+
+class Increment(object) :
+    """ A simple sample adapter. """
+    
+    zope.interface.implements(IIncrement)
+    zope.component.adapts(INumber)
+    
+    zorg.usage.adapter()
+    
+    def __init__(self, context) :
+        self.context = context
+        
+    def incr(self) :
+        return self.context.number() + 1
+
+
+class Counter(object) :
+    """ A multiadapter. """
+
+    zope.interface.implements(ICounter)
+    zope.component.adapts(IFirst, ISecond)
+    
+    zorg.usage.adapter()
+
+    def __init__(self, first, second) :
+        self.first = first
+        self.second = second
+
+    def count(self) :
+        print self.first.number()
+        print self.second.number()
+    
+    
+class RomanCounter(Counter) :
+    """ A named adapter. """
+    
+    zope.interface.implements(ICounter)
+    zope.component.adapts(IFirst, ISecond)
+    
+    zorg.usage.adapter(name="roman")
+    
+    translate = {1 : "I", 2 : "II"}
+   
+    def count(self) :
+        print self.translate[self.first.number()]
+        print self.translate[self.second.number()]
+        
+        
+# A test utility
+
+class CounterUtility(Counter) :
+    """ A named utility. """
+    
+    zope.interface.implements(ICounter)
+    zorg.usage.globalUtility(name="verbal")
+    
+    def __init__(self) :
+        pass
+    
+    def count(self) :
+        print "one"
+        print "two"
+
+
+



More information about the Checkins mailing list