[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/principalannotation/ Replace decoy IPrincipal -> IAnnotations adapter with one that is actually registered and is not bound to a single principal annotation utility.

Gary Poster gary at zope.com
Mon Apr 25 14:00:30 EDT 2005


Log message for revision 30167:
  Replace decoy IPrincipal -> IAnnotations adapter with one that is actually registered and is not bound to a single principal annotation utility.
  
  

Changed:
  U   Zope3/trunk/src/zope/app/principalannotation/__init__.py
  U   Zope3/trunk/src/zope/app/principalannotation/configure.zcml
  U   Zope3/trunk/src/zope/app/principalannotation/tests.py

-=-
Modified: Zope3/trunk/src/zope/app/principalannotation/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/__init__.py	2005-04-25 17:08:16 UTC (rev 30166)
+++ Zope3/trunk/src/zope/app/principalannotation/__init__.py	2005-04-25 18:00:30 UTC (rev 30167)
@@ -22,7 +22,8 @@
 from persistent import Persistent
 from persistent.dict import PersistentDict
 from BTrees.OOBTree import OOBTree
-from zope.interface import implements
+from zope import interface, component
+import zope.security.interfaces
 
 from zope.app.annotation.interfaces import IAnnotations
 from zope.app.container.contained import Contained
@@ -36,7 +37,7 @@
     The utility ID is 'PrincipalAnnotation'.
     """
 
-    implements(IPrincipalAnnotationUtility)
+    interface.implements(IPrincipalAnnotationUtility)
 
     def __init__(self):
         self.annotations = OOBTree()
@@ -70,7 +71,7 @@
 class Annotations(Persistent, Location):
     """Stores annotations."""
 
-    implements(IAnnotations)
+    interface.implements(IAnnotations)
 
     def __init__(self, principalId, store=None):
         self.principalId = principalId
@@ -106,21 +107,40 @@
     def get(self, key, default=None):
         return self.data.get(key, default)
 
+def annotations(principal):
+    """adapt principal to annotations via principal annotation utility.
+    
+    To illustrate, we'll register the adapter and a dummy 
+    principal annotation utility.
+    
+    >>> component.provideAdapter(annotations)
+    >>> class DummyPrincipal(object):
+    ...     interface.implements(zope.security.interfaces.IPrincipal)
+    ...     def __init__(self, id, title=None, description=None):
+    ...         self.id = id
+    ...         self.title = title
+    ...         self.description = description
+    ...
+    >>> dummy_annotation = {}
+    >>> class DummyPAU(object):
+    ...     interface.implements(interfaces.IPrincipalAnnotationUtility)
+    ...     def getAnnotations(self, principal):
+    ...         if principal.id == 'sue':
+    ...             return dummy_annotation
+    ...         raise NotImplementedError
+    ...
+    >>> pau = DummyPAU()
+    >>> component.provideUtility(pau)
+    >>> sue = DummyPrincipal('sue')
+    >>> annotation = IAnnotations(sue)
+    >>> annotation is dummy_annotation
+    True
+    """ # TODO move this out to a doctest file when we have one...
+    utility = component.getUtility(IPrincipalAnnotationUtility)
+    return utility.getAnnotations(principal)
+component.adapter(zope.security.interfaces.IPrincipal)(annotations)
+interface.implementer(IAnnotations)(annotations)
 
-class AnnotationsForPrincipal(object):
-    """Adapter from IPrincipal to `IAnnotations` for a
-    `PrincipalAnnotationUtility`.
-
-    Register an *instance* of this class as an adapter.
-    """
-
-    def __init__(self, utility):
-        self.utility = utility
-
-    def __call__(self, principal):
-        return self.utility.getAnnotationsById(principal.id)
-
-
 #############################################################################
 # BBB: 12/20/2004
 PrincipalAnnotationService = PrincipalAnnotationUtility

Modified: Zope3/trunk/src/zope/app/principalannotation/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/configure.zcml	2005-04-25 17:08:16 UTC (rev 30166)
+++ Zope3/trunk/src/zope/app/principalannotation/configure.zcml	2005-04-25 18:00:30 UTC (rev 30167)
@@ -33,4 +33,6 @@
       handler=".bootstrap.bootStrapSubscriber"
       />
 
+  <adapter factory=".annotations" />
+
 </configure>

Modified: Zope3/trunk/src/zope/app/principalannotation/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/tests.py	2005-04-25 17:08:16 UTC (rev 30166)
+++ Zope3/trunk/src/zope/app/principalannotation/tests.py	2005-04-25 18:00:30 UTC (rev 30167)
@@ -15,6 +15,7 @@
 
 $Id$
 """
+import doctest
 from unittest import TestCase, TestLoader, TextTestRunner
 
 import zope.component
@@ -22,11 +23,10 @@
 from zope.app import zapi
 from zope.app.annotation.interfaces import IAnnotations
 from zope.app.principalannotation import PrincipalAnnotationUtility
-from zope.app.principalannotation import AnnotationsForPrincipal
 from zope.app.security.interfaces import IPrincipal
 from zope.app.component.testing import PlacefulSetup
 from zope.app.testing import ztapi, setup
-from interfaces import IPrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
 
 class Principal(object):
 
@@ -41,7 +41,6 @@
     def setUp(self):
         PlacefulSetup.setUp(self)
         sm = self.buildFolders(site='/')
-
         self.util = PrincipalAnnotationUtility()
         zope.component.provideUtility(self.util, IPrincipalAnnotationUtility)
 
@@ -83,20 +82,12 @@
 
         self.assertEquals(parentAnnotation.get("foo"), None)
 
-
-    def testAdapter(self):
-        p = Principal('somebody')
-        ztapi.provideAdapter(IPrincipal, IAnnotations,
-                             AnnotationsForPrincipal(self.util))
-        annotations = IAnnotations(p)
-        annotations["test"] = "bar"
-        annotations = IAnnotations(p)
-        self.assertEquals(annotations["test"], "bar")
-
-
 def test_suite():
     loader=TestLoader()
-    return loader.loadTestsFromTestCase(PrincipalAnnotationTests)
+    tests = loader.loadTestsFromTestCase(PrincipalAnnotationTests)
+    import zope.app.principalannotation
+    tests.addTest(doctest.DocTestSuite(zope.app.principalannotation))
+    return tests
 
 if __name__=='__main__':
     TextTestRunner().run(test_suite())



More information about the Zope3-Checkins mailing list