[Zope3-checkins] SVN: Zope3/trunk/ Converted Principal Annotation Service to be a utility. Added

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Dec 21 11:39:26 EST 2004


Log message for revision 28668:
  Converted Principal Annotation Service to be a utility. Added
  database evolution code to convert all service instances to
  utilities.
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/appsetup/bootstrap.py
  U   Zope3/trunk/src/zope/app/appsetup/tests.py
  U   Zope3/trunk/src/zope/app/container/browser/contents.py
  U   Zope3/trunk/src/zope/app/container/browser/tests/test_contents.py
  U   Zope3/trunk/src/zope/app/copypastemove/tests/test_clipboard.py
  U   Zope3/trunk/src/zope/app/principalannotation/__init__.py
  A   Zope3/trunk/src/zope/app/principalannotation/bootstrap.py
  U   Zope3/trunk/src/zope/app/principalannotation/configure.zcml
  U   Zope3/trunk/src/zope/app/principalannotation/interfaces.py
  U   Zope3/trunk/src/zope/app/principalannotation/tests.py
  U   Zope3/trunk/src/zope/app/zopeappgenerations/evolve1.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/doc/CHANGES.txt	2004-12-21 16:39:25 UTC (rev 28668)
@@ -169,11 +169,16 @@
       - Removal of Services.
 
         + Converted Error Reporting Service to a utility. Added database
-          evolution code to convert all serice instances to utilities.
+          evolution code to convert all service instances to utilities.
 
         + Converted Authentication Service to be a utility. Renamed PAS to
           PAU.
 
+        + Converted Principal Annotation Service to be a utility. Added
+          database evolution code to convert all service instances to
+          utilities.
+ 
+
       - The `pluggableauth` package has been deprecated. The `pas` module
         provides a much more modular approach with many more capabilities.
 

Modified: Zope3/trunk/src/zope/app/appsetup/bootstrap.py
===================================================================
--- Zope3/trunk/src/zope/app/appsetup/bootstrap.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/appsetup/bootstrap.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -30,20 +30,12 @@
 from zope.app.traversing.api import traverse, traverseName
 from zope.app.publication.zopepublication import ZopePublication
 from zope.app.folder import rootFolder
-from zope.app.servicenames import PrincipalAnnotation
 from zope.app.servicenames import Utilities
 from zope.app.site.service import ServiceManager, ServiceRegistration
 from zope.app.container.interfaces import INameChooser
 from zope.app.utility import UtilityRegistration, LocalUtilityService
 from zope.app.utility.interfaces import ILocalUtilityService
 
-# XXX It should be possible to remove each of these from the basic
-# bootstrap, at which point we can remove the
-# zope.app.principalannotation packages from
-# zope.app.
-
-from zope.app.principalannotation import PrincipalAnnotationService
-
 def ensureObject(root_folder, object_name, object_type, object_factory):
     """Check that there's a basic object in the service
     manager. If not, add one.
@@ -223,8 +215,6 @@
         service_manager = getServiceManager(root_folder)
 
         # Sundry other services
-        ensureService(service_manager, root_folder, PrincipalAnnotation,
-                      PrincipalAnnotationService)
         ensureService(service_manager, root_folder, Utilities,
                       LocalUtilityService)
 

Modified: Zope3/trunk/src/zope/app/appsetup/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/appsetup/tests.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/appsetup/tests.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -23,7 +23,6 @@
 from zope.app.error.interfaces import IErrorReportingUtility
 from zope.app.folder import rootFolder
 from zope.app.folder.interfaces import IRootFolder
-from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
 from zope.app.publication.zopepublication import ZopePublication
 from zope.app.servicenames import Utilities
 from zope.app.site.service import ServiceManager

Modified: Zope3/trunk/src/zope/app/container/browser/contents.py
===================================================================
--- Zope3/trunk/src/zope/app/container/browser/contents.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/container/browser/contents.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -34,6 +34,7 @@
 from zope.app.copypastemove.interfaces import IObjectCopier
 from zope.app.copypastemove.interfaces import IObjectMover
 from zope.app.copypastemove import rename
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
 
 from zope.app.container.browser.adding import Adding
 from zope.app.container.interfaces import IContainer
@@ -266,8 +267,8 @@
         container_path = zapi.getPath(self.context)
 
         user = self.request.principal
-        annotationsvc = zapi.getService('PrincipalAnnotation')
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         clipboard.clearContents()
         items = []
@@ -286,8 +287,8 @@
         container_path = zapi.getPath(self.context)
 
         user = self.request.principal
-        annotationsvc = zapi.getService('PrincipalAnnotation')
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         clipboard.clearContents()
         items = []
@@ -301,8 +302,8 @@
         """
         target = self.context
         user = self.request.principal
-        annotationsvc = zapi.getService('PrincipalAnnotation')
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         items = clipboard.getContents()
         for item in items:
@@ -332,8 +333,8 @@
         """
         target = self.context
         user = self.request.principal
-        annotationsvc = zapi.getService('PrincipalAnnotation')
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         items = clipboard.getContents()
         moved = False
@@ -367,8 +368,8 @@
 
         user = self.request.principal
 
-        annotationsvc = zapi.getService('PrincipalAnnotation')
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
 
         # touch at least one item to in clipboard confirm contents
         clipboard = IPrincipalClipboard(annotations)

Modified: Zope3/trunk/src/zope/app/container/browser/tests/test_contents.py
===================================================================
--- Zope3/trunk/src/zope/app/container/browser/tests/test_contents.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/container/browser/tests/test_contents.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -32,8 +32,8 @@
 from zope.app.copypastemove.interfaces import IPrincipalClipboard
 from zope.app.copypastemove import PrincipalClipboard
 from zope.component import getGlobalServices
-from zope.app.principalannotation import PrincipalAnnotationService
-from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
+from zope.app.principalannotation import PrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
 from zope.app.annotation.interfaces import IAnnotations
 from zope.app.container.contained import contained
 
@@ -56,11 +56,8 @@
 
         ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
                              PrincipalClipboard)
-        global_sm = getGlobalServices()
-        svc = PrincipalAnnotationService()
-        global_sm.defineService("PrincipalAnnotation", \
-            IPrincipalAnnotationService)
-        global_sm.provideService("PrincipalAnnotation", svc)
+        ztapi.provideUtility(IPrincipalAnnotationUtility,
+                             PrincipalAnnotationUtility())
 
     def testInfo(self):
         # Do we get the correct information back from ContainerContents?
@@ -177,11 +174,8 @@
 
         ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
                              PrincipalClipboard)
-        global_sm = getGlobalServices()
-        svc = PrincipalAnnotationService()
-        global_sm.defineService("PrincipalAnnotation", \
-            IPrincipalAnnotationService)
-        global_sm.provideService("PrincipalAnnotation", svc)
+        ztapi.provideUtility(IPrincipalAnnotationUtility,
+                             PrincipalAnnotationUtility())
 
     def testRename(self):
         container = traverse(self.rootFolder, 'folder1')

Modified: Zope3/trunk/src/zope/app/copypastemove/tests/test_clipboard.py
===================================================================
--- Zope3/trunk/src/zope/app/copypastemove/tests/test_clipboard.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/copypastemove/tests/test_clipboard.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -19,8 +19,8 @@
 
 from zope.app import zapi
 from zope.app.tests import ztapi
-from zope.app.principalannotation import PrincipalAnnotationService
-from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
+from zope.app.principalannotation import PrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
 from zope.app.annotation.interfaces import IAnnotations
 
 from zope.app.copypastemove.interfaces import IPrincipalClipboard
@@ -36,17 +36,14 @@
 
         ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
                              PrincipalClipboard)
-        root_sm = zapi.getGlobalServices()
-        svc = PrincipalAnnotationService()
-        root_sm.defineService("PrincipalAnnotation", \
-            IPrincipalAnnotationService)
-        root_sm.provideService("PrincipalAnnotation", svc)
+        ztapi.provideUtility(IPrincipalAnnotationUtility,
+                             PrincipalAnnotationUtility())
 
     def testAddItems(self):
         user = self._auth['one']['srichter']
 
-        annotationsvc = zapi.getService('PrincipalAnnotation', self)
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         clipboard.addItems('move', ['bla', 'bla/foo', 'bla/bar'])
         expected = ({'action':'move', 'target':'bla'},
@@ -61,8 +58,8 @@
     def testSetContents(self):
         user = self._auth['one']['srichter']
 
-        annotationsvc = zapi.getService('PrincipalAnnotation', self)
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
 
         expected = ({'action':'move', 'target':'bla'},
@@ -77,8 +74,8 @@
     def testClearContents(self):
         user = self._auth['one']['srichter']
 
-        annotationsvc = zapi.getService('PrincipalAnnotation', self)
-        annotations = annotationsvc.getAnnotations(user)
+        annotationutil = zapi.getUtility(IPrincipalAnnotationUtility)
+        annotations = annotationutil.getAnnotations(user)
         clipboard = IPrincipalClipboard(annotations)
         clipboard.clearContents()
         self.failUnless(clipboard.getContents() == ())

Modified: Zope3/trunk/src/zope/app/principalannotation/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/__init__.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/principalannotation/__init__.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -11,37 +11,34 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Implementation of `IPrincipalAnnotationService`.
+"""Implementation of `IPrincipalAnnotationUtility`.
 
 $Id$
 """
 __docformat__ = 'restructuredtext'
 
-# TODO: register service as adapter for IAnnotations on service activation
+# TODO: register utility as adapter for IAnnotations on utility activation
 # this depends on existence of LocalAdapterService, so once that's done
 # implement this.
 
-# Zope3 imports
 from persistent import Persistent
 from persistent.dict import PersistentDict
 from BTrees.OOBTree import OOBTree
-from zope.app.component.localservice import queryNextService
-from zope.app.annotation.interfaces import IAnnotations
 from zope.interface import implements
 
-# Sibling imports
-from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
-from zope.app.site.interfaces import ISimpleService
+from zope.app.annotation.interfaces import IAnnotations
 from zope.app.container.contained import Contained
 from zope.app.location import Location
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
+from zope.app.utility.utility import queryNextUtility
 
-class PrincipalAnnotationService(Persistent, Contained):
+class PrincipalAnnotationUtility(Persistent, Contained):
     """Stores `IAnnotations` for `IPrinicipals`.
 
-    The service ID is 'PrincipalAnnotation'.
+    The utility ID is 'PrincipalAnnotation'.
     """
 
-    implements(IPrincipalAnnotationService, ISimpleService)
+    implements(IPrincipalAnnotationUtility)
 
     def __init__(self):
         self.annotations = OOBTree()
@@ -89,10 +86,10 @@
         try:
             return self.data[key]
         except KeyError:
-            # We failed locally: delegate to a higher-level service.
-            service = queryNextService(self, 'PrincipalAnnotation')
-            if service is not None:
-                annotations = service.getAnnotationsById(self.principalId)
+            # We failed locally: delegate to a higher-level utility.
+            utility = queryNextUtility(self, IPrincipalAnnotationUtility)
+            if utility is not None:
+                annotations = utility.getAnnotationsById(self.principalId)
                 return annotations[key]
             raise
 
@@ -114,13 +111,19 @@
 
 class AnnotationsForPrincipal(object):
     """Adapter from IPrincipal to `IAnnotations` for a
-    `PrincipalAnnotationService`.
+    `PrincipalAnnotationUtility`.
 
     Register an *instance* of this class as an adapter.
     """
 
-    def __init__(self, service):
-        self.service = service
+    def __init__(self, utility):
+        self.utility = utility
 
     def __call__(self, principal):
-        return self.service.getAnnotationsById(principal.id)
+        return self.utility.getAnnotationsById(principal.id)
+
+
+#############################################################################
+# BBB: 12/20/2004
+PrincipalAnnotationService = PrincipalAnnotationUtility
+#############################################################################

Added: Zope3/trunk/src/zope/app/principalannotation/bootstrap.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/bootstrap.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/principalannotation/bootstrap.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -0,0 +1,36 @@
+##############################################################################
+#
+# Copyright (c) 2002, 2004 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.
+#
+##############################################################################
+"""Bootstrap code for principal annotation utility.
+
+$Id: bootstrap.py 28023 2004-10-12 18:11:29Z anguenot $
+"""
+
+from zope.app.appsetup.bootstrap import ensureUtility, getInformationFromEvent
+
+from zope.app.principalannotation import PrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
+
+def bootStrapSubscriber(event):
+    """Subscriber to the IDataBaseOpenedEvent
+
+    Create utility at that time if not yet present
+    """
+
+    db, connection, root, root_folder = getInformationFromEvent(event)
+
+    ensureUtility(root_folder, IPrincipalAnnotationUtility,
+                  'PrincipalAnnotation', PrincipalAnnotationUtility)
+
+    get_transaction().commit()
+    connection.close()

Modified: Zope3/trunk/src/zope/app/principalannotation/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/configure.zcml	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/principalannotation/configure.zcml	2004-12-21 16:39:25 UTC (rev 28668)
@@ -3,26 +3,27 @@
     xmlns:browser="http://namespaces.zope.org/browser"
     >
 
-  <serviceType
-      id="PrincipalAnnotation"
-      interface=".interfaces.IPrincipalAnnotationService" />
-
-  <localService class=".PrincipalAnnotationService">
+  <localUtility class=".PrincipalAnnotationUtility">
     <factory
-        id="zope.app.PrincipalAnnotationService"
+        id="zope.app.PrincipalAnnotationUtility"
         />
     <require
         permission="zope.View"
-        interface=".interfaces.IPrincipalAnnotationService"
+        interface=".interfaces.IPrincipalAnnotationUtility"
         />
-    </localService>
+    </localUtility>
 
-  <!-- Principal annotations (user data) service -->
+  <!-- Principal annotations (user data) utility -->
 
   <browser:addMenuItem
-     class=".PrincipalAnnotationService"
+     class=".PrincipalAnnotationUtility"
      permission="zope.ManageServices"
-     title="Principal Annotation Service"
+     title="Principal Annotation Utility"
      description="Stores Annotations for Principals" />
 
+  <subscriber
+      for="zope.app.appsetup.IDatabaseOpenedEvent"
+      factory=".bootstrap.bootStrapSubscriber"
+      />
+
 </configure>

Modified: Zope3/trunk/src/zope/app/principalannotation/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/interfaces.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/principalannotation/interfaces.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -20,7 +20,7 @@
 from zope.interface import Interface
 
 
-class IPrincipalAnnotationService(Interface):
+class IPrincipalAnnotationUtility(Interface):
     """Stores `IAnnotations` for `IPrinicipals`."""
 
     def getAnnotations(principal):
@@ -40,3 +40,8 @@
     def hasAnnotations(principal):
         """Return boolean indicating if given `IPrincipal` has
         `IAnnotations`."""
+
+#############################################################################
+# BBB: 12/20/2004
+IPrincipalAnnotationService = IPrincipalAnnotationUtility
+#############################################################################

Modified: Zope3/trunk/src/zope/app/principalannotation/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/tests.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/principalannotation/tests.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -16,16 +16,18 @@
 $Id$
 """
 from unittest import TestCase, TestLoader, TextTestRunner
-from zope.app.site.tests.placefulsetup import PlacefulSetup
-from zope.app.principalannotation import \
-     PrincipalAnnotationService, AnnotationsForPrincipal
-from interfaces import IPrincipalAnnotationService
-from zope.app.tests import ztapi
+from zope import component as capi
+from zope.interface import implements
+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.site.tests.placefulsetup import PlacefulSetup
 from zope.app.tests import setup
-from zope.interface import implements
-from zope.app import zapi
+from zope.app.tests import ztapi
+from zope.app.utility.utility import LocalUtilityService
+from interfaces import IPrincipalAnnotationUtility
 
 class Principal(object):
 
@@ -41,48 +43,42 @@
         PlacefulSetup.setUp(self)
         sm = self.buildFolders(site='/')
 
-        root_sm = zapi.getGlobalServices()
+        self.util = PrincipalAnnotationUtility()
+        capi.provideUtility(self.util, IPrincipalAnnotationUtility)
 
-        svc = PrincipalAnnotationService()
-
-        root_sm.defineService("PrincipalAnnotation",
-                              IPrincipalAnnotationService)
-        root_sm.provideService("PrincipalAnnotation", svc)
-
-        self.svc = setup.addService(sm, 'PrincipalAnnotation', svc)
-
     def testGetSimple(self):
         prince = Principal('somebody')
-        self.assert_(not self.svc.hasAnnotations(prince))
+        self.assert_(not self.util.hasAnnotations(prince))
 
-        princeAnnotation = self.svc.getAnnotations(prince)
+        princeAnnotation = self.util.getAnnotations(prince)
         # Just getting doesn't actualy store. We don't want to store unless
         # we make a change.
-        self.assert_(not self.svc.hasAnnotations(prince))
+        self.assert_(not self.util.hasAnnotations(prince))
 
         princeAnnotation['something'] = 'whatever'
 
         # But now we should have the annotation:
-        self.assert_(self.svc.hasAnnotations(prince))
+        self.assert_(self.util.hasAnnotations(prince))
 
     def testGetFromLayered(self):
         princeSomebody = Principal('somebody')
         sm1 = self.makeSite('folder1')
-        subService = setup.addService(sm1, 'PrincipalAnnotation',
-                                      PrincipalAnnotationService())
+        setup.addService(sm1, 'Utilities', LocalUtilityService())
+        subUtil = setup.addUtility(sm1, '', IPrincipalAnnotationUtility,
+                                   PrincipalAnnotationUtility())
 
-        parentAnnotation = self.svc.getAnnotations(princeSomebody)
+        parentAnnotation = self.util.getAnnotations(princeSomebody)
 
         # Just getting doesn't actualy store. We don't want to store unless
         # we make a change.
-        self.assert_(not subService.hasAnnotations(princeSomebody))
+        self.assert_(not subUtil.hasAnnotations(princeSomebody))
 
         parentAnnotation['hair_color'] = 'blue'
 
         # But now we should have the annotation:
-        self.assert_(self.svc.hasAnnotations(princeSomebody))
+        self.assert_(self.util.hasAnnotations(princeSomebody))
 
-        subAnnotation = subService.getAnnotations(princeSomebody)
+        subAnnotation = subUtil.getAnnotations(princeSomebody)
         self.assertEquals(subAnnotation['hair_color'], 'blue')
 
         subAnnotation['foo'] = 'bar'
@@ -93,7 +89,7 @@
     def testAdapter(self):
         p = Principal('somebody')
         ztapi.provideAdapter(IPrincipal, IAnnotations,
-                             AnnotationsForPrincipal(self.svc))
+                             AnnotationsForPrincipal(self.util))
         annotations = IAnnotations(p)
         annotations["test"] = "bar"
         annotations = IAnnotations(p)

Modified: Zope3/trunk/src/zope/app/zopeappgenerations/evolve1.py
===================================================================
--- Zope3/trunk/src/zope/app/zopeappgenerations/evolve1.py	2004-12-21 14:32:36 UTC (rev 28667)
+++ Zope3/trunk/src/zope/app/zopeappgenerations/evolve1.py	2004-12-21 16:39:25 UTC (rev 28668)
@@ -21,6 +21,9 @@
 from zope.app.error.interfaces import IErrorReportingService
 from zope.app.error.interfaces import IErrorReportingUtility
 from zope.app.generations.utility import findObjectsProviding
+from zope.app.principalannotation import PrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
+from zope.app.principalannotation.interfaces import IPrincipalAnnotationService
 from zope.app.registration.interfaces import IComponentRegistration
 from zope.app.registration.interfaces import ActiveStatus, UnregisteredStatus
 from zope.app.site.interfaces import ISite, IServiceRegistration
@@ -32,6 +35,10 @@
 def evolve(context):
     """Evolve the ZODB from a Zope X3.0 to a X3.1 compatible format.
 
+    - The Principal Annotation Service was replaced by the Principal
+      Annotation Utility. Thus all service registrations have to be changed to
+      utility registrations. 
+
     - The Error Reporting Service was replaced by the Error Reporting
       Utility. Thus, all service registrations have to be changed to utility
       registrations. 
@@ -42,6 +49,43 @@
     """
     root = getRootFolder(context)
 
+    # Fix up Principal Annotation Service --> Utility 
+    # We do this by simply removing old Principal Annotation Services and their
+    # registrations and then add a new Principal Annotation utility.
+    for site in findObjectsProviding(root, ISite):
+        for reg in findObjectsProviding(site.getSiteManager(),
+                                        IServiceRegistration):
+        
+            if reg.name == 'PrincipalAnnotation':
+                ann = reg.component
+                # Set the registration to unregistered and then delete it
+                reg.status = UnregisteredStatus
+                del zapi.getParent(reg)[zapi.name(reg)]
+                # Get the instance dictionary from the old principal
+                # annotation service and then delete the service
+                props = ann.__dict__
+                name = zapi.name(ann)
+                folder = zapi.getParent(ann)
+                del folder[name]
+
+
+                # Only add a new principal annotation utility, if there is none.
+                utils = [obj for obj in folder.values()
+                         if isinstance(obj, PrincipalAnnotationUtility)]
+                if len(utils) == 0:
+                    # Create the principal annotation utility and set its
+                    # properties
+                    utility = PrincipalAnnotationUtility()
+                    utility.__dict__.update(props)
+                    folder[name] = utility
+                    # Register the utility and set the registration active
+                    reg = UtilityRegistration('', IPrincipalAnnotationUtility,
+                                              utility)
+                    reg_manager = folder.getRegistrationManager() 
+                    key = reg_manager.addRegistration(reg)
+                    reg_manager[key].status = ActiveStatus
+
+
     # Fix up Error Reporting Service --> Utility 
     # We do this by simply removing old Error Reporting Services and their
     # registrations and then add a new error reporting utility.



More information about the Zope3-Checkins mailing list