[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ Added a registration event dispatcher for component registrations so that developers can explicitly register for a registration event (e.g. activated and deactivated) rather than having to check for specific component instances.

Garrett Smith garrett at mojave-corp.com
Mon Feb 7 17:50:03 EST 2005


Log message for revision 29078:
  Added a registration event dispatcher for component registrations so that developers can explicitly register for a registration event (e.g. activated and deactivated) rather than having to check for specific component instances.
  
  This change includes conversion of existing subscribers to component registration events to use the new scheme.

Changed:
  U   Zope3/trunk/src/zope/app/i18n/configure.zcml
  U   Zope3/trunk/src/zope/app/i18n/translationdomain.py
  U   Zope3/trunk/src/zope/app/registration/configure.zcml
  U   Zope3/trunk/src/zope/app/registration/registration.py
  U   Zope3/trunk/src/zope/app/security/configure.zcml
  U   Zope3/trunk/src/zope/app/security/permission.py
  U   Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
  U   Zope3/trunk/src/zope/app/securitypolicy/role.py
  U   Zope3/trunk/src/zope/app/site/configure.zcml
  U   Zope3/trunk/src/zope/app/site/service.py
  U   Zope3/trunk/src/zope/app/tests/setup.py

-=-
Modified: Zope3/trunk/src/zope/app/i18n/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/i18n/configure.zcml	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/i18n/configure.zcml	2005-02-07 22:50:03 UTC (rev 29078)
@@ -37,12 +37,14 @@
   </localUtility>
   
   <subscriber
-     for="..registration.interfaces.IRegistrationActivatedEvent"
+     for=".interfaces.ILocalTranslationDomain
+          ..registration.interfaces.IRegistrationActivatedEvent"
      handler=".translationdomain.setDomainOnActivation"
      />
 
   <subscriber
-     for="..registration.interfaces.IRegistrationDeactivatedEvent"
+     for=".interfaces.ILocalTranslationDomain
+          ..registration.interfaces.IRegistrationDeactivatedEvent"
      handler=".translationdomain.unsetDomainOnDeactivation"
      />  
 

Modified: Zope3/trunk/src/zope/app/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -226,7 +226,7 @@
 # BBB: Backward compatibility. 12/09/2004
 DomainRegistration = UtilityRegistration
 
-def setDomainOnActivation(event):
+def setDomainOnActivation(domain, event):
     """Set the permission id upon registration activation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -241,35 +241,21 @@
     >>> domain1.domain
     '<domain not activated>'
 
-    >>> from zope.app.registration import registration 
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationActivatedEvent(
     ...     Registration(domain1, 'domain1'))
 
     Now we pass the event into this function, and the id of the domain should
     be set to 'domain1'.
 
-    >>> setDomainOnActivation(event)
+    >>> setDomainOnActivation(domain1, event)
     >>> domain1.domain
     'domain1'
-
-    If the function is called and the component is not a local domain, nothing
-    is done:
-
-    >>> class Foo:
-    ...     domain = 'no domain'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationActivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> setDomainOnActivation(event)
-    >>> foo.domain
-    'no domain'
     """
-    domain = event.object.component
-    if isinstance(domain, TranslationDomain):
-        domain.domain = event.object.name
+    domain.domain = event.object.name
 
 
-def unsetDomainOnDeactivation(event):
+def unsetDomainOnDeactivation(domain, event):
     """Unset the permission id up registration deactivation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -283,29 +269,15 @@
     >>> domain1 = TranslationDomain()
     >>> domain1.domain = 'domain1'
 
-    >>> from zope.app.registration import registration 
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationDeactivatedEvent(
     ...     Registration(domain1, 'domain1'))
 
     Now we pass the event into this function, and the id of the role should be
     set to '<domain not activated>'.
 
-    >>> unsetDomainOnDeactivation(event)
+    >>> unsetDomainOnDeactivation(domain1, event)
     >>> domain1.domain
     '<domain not activated>'
-
-    If the function is called and the component is not a local domain,
-    nothing is done:
-
-    >>> class Foo:
-    ...     domain = 'foo'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationDeactivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> unsetDomainOnDeactivation(event)
-    >>> foo.domain
-    'foo'
     """
-    domain = event.object.component
-    if isinstance(domain, TranslationDomain):
-        domain.domain = '<domain not activated>'
+    domain.domain = '<domain not activated>'

Modified: Zope3/trunk/src/zope/app/registration/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/registration/configure.zcml	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/registration/configure.zcml	2005-02-07 22:50:03 UTC (rev 29078)
@@ -26,7 +26,7 @@
       />
 
   <class class=".registration.Registered">
-    <require 
+    <require
         permission="zope.ManageContent"
         attributes="addUsage removeUsage" />
     <allow attributes="usages registrations" />
@@ -54,7 +54,7 @@
         zope.app.container.interfaces.IWriteContainer
         .interfaces.IOrderedContainer
         zope.app.container.interfaces.INameChooser
-        " 
+        "
         />
     <implements
         interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
@@ -85,4 +85,9 @@
            zope.app.container.interfaces.IObjectMovedEvent"
       />
 
+  <subscriber
+    handler=".registration.componentRegistrationEventNotify"
+    for=".interfaces.IComponentRegistration
+         .interfaces.IRegistrationEvent" />
+
 </configure>

Modified: Zope3/trunk/src/zope/app/registration/registration.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/registration.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/registration/registration.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -26,6 +26,7 @@
 from zope.proxy import removeAllProxies, getProxiedObject
 from zope.security.checker import InterfaceChecker, CheckerPublic
 from zope.security.proxy import Proxy, removeSecurityProxy
+from zope.component import subscribers
 
 from zope.app import zapi
 from zope.app.annotation.interfaces import IAttributeAnnotatable
@@ -1021,3 +1022,10 @@
         l = name.rfind('.')
         mod = self.findModule(name[:l])
         return getattr(mod, name[l+1:])
+
+
+def componentRegistrationEventNotify(componentReg, event):
+    """Subscriber to dispatch registration events for components."""
+    adapters = subscribers((componentReg.component, event), None)
+    for adapter in adapters:
+        pass # getting them does the work

Modified: Zope3/trunk/src/zope/app/security/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/security/configure.zcml	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/security/configure.zcml	2005-02-07 22:50:03 UTC (rev 29078)
@@ -10,7 +10,7 @@
                    Public resources are always accessible."
       />
 
-  <vocabulary 
+  <vocabulary
       name="Permissions"
       factory="zope.app.utility.vocabulary.UtilityVocabulary"
       interface="zope.security.interfaces.IPermission" />
@@ -23,15 +23,15 @@
   <include file="_protections.zcml" />
 
   <utility
-      provides=".interfaces.IAuthentication" 
+      provides=".interfaces.IAuthentication"
       component=".principalregistry.principalRegistry" />
 
   <localUtility class=".permission.LocalPermission">
     <factory
         id="zope.app.security.Permission"
         />
-    <allow 
-        interface=".interfaces.IPermission" 
+    <allow
+        interface=".interfaces.IPermission"
         />
     <require
         permission="zope.Security"
@@ -40,12 +40,14 @@
   </localUtility>
 
   <subscriber
-     for="..registration.interfaces.IRegistrationActivatedEvent"
+     for="zope.security.interfaces.IPermission
+          ..registration.interfaces.IRegistrationActivatedEvent"
      handler=".permission.setIdOnActivation"
      />
 
   <subscriber
-     for="..registration.interfaces.IRegistrationDeactivatedEvent"
+     for="zope.security.interfaces.IPermission
+          ..registration.interfaces.IRegistrationDeactivatedEvent"
      handler=".permission.unsetIdOnDeactivation"
      />
 
@@ -80,9 +82,9 @@
       title="[manage-content-permission] Manage Content"
       />
 
-  <permission 
-      id="zope.ManageBindings" 
-      title="[manage-service-bindings-permission] Manage Service Bindings" 
+  <permission
+      id="zope.ManageBindings"
+      title="[manage-service-bindings-permission] Manage Service Bindings"
       />
 
   <permission
@@ -91,9 +93,9 @@
       description="Manage executable code, including Python, SQL, ZPT, etc."
       />
 
-  <permission 
-      id="zope.ManageServices" 
-      title="[manage-services-permission] Manage Services" 
+  <permission
+      id="zope.ManageServices"
+      title="[manage-services-permission] Manage Services"
       />
 
   <permission
@@ -111,7 +113,7 @@
   <adapter
       factory=".basicauthadapter.BasicAuthAdapter"
       provides=".interfaces.ILoginPassword"
-      for="zope.publisher.interfaces.http.IHTTPCredentials" 
+      for="zope.publisher.interfaces.http.IHTTPCredentials"
       />
 
   <adapter

Modified: Zope3/trunk/src/zope/app/security/permission.py
===================================================================
--- Zope3/trunk/src/zope/app/security/permission.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/security/permission.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -44,7 +44,7 @@
         self.description = description
 
 
-def setIdOnActivation(event):
+def setIdOnActivation(permission, event):
     """Set the permission id upon registration activation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -58,36 +58,22 @@
     >>> perm1 = LocalPermission('Permission 1', 'A first permission')
     >>> perm1.id
     u'<permission not activated>'
-    
-    >>> from zope.app.registration import registration 
+
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationActivatedEvent(
     ...     Registration(perm1, 'perm1'))
 
     Now we pass the event into this function, and the id of the permission
     should be set to 'perm1'.
 
-    >>> setIdOnActivation(event)
+    >>> setIdOnActivation(perm1, event)
     >>> perm1.id
     'perm1'
-
-    If the function is called and the component is not a local permission,
-    nothing is done:
-
-    >>> class Foo:
-    ...     id = 'no id'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationActivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> setIdOnActivation(event)
-    >>> foo.id
-    'no id'
     """
-    perm = event.object.component
-    if isinstance(perm, LocalPermission):
-        perm.id = event.object.name
+    permission.id = event.object.name
 
 
-def unsetIdOnDeactivation(event):
+def unsetIdOnDeactivation(permission, event):
     """Unset the permission id up registration deactivation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -101,32 +87,18 @@
     >>> perm1 = LocalPermission('Permission 1', 'A first permission')
     >>> perm1.id = 'perm1'
 
-    >>> from zope.app.registration import registration 
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationDeactivatedEvent(
     ...     Registration(perm1, 'perm1'))
 
     Now we pass the event into this function, and the id of the permission
     should be set to NULL_ID.
 
-    >>> unsetIdOnDeactivation(event)
+    >>> unsetIdOnDeactivation(perm1, event)
     >>> perm1.id
     u'<permission not activated>'
-
-    If the function is called and the component is not a local permission,
-    nothing is done:
-
-    >>> class Foo:
-    ...     id = 'foo'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationDeactivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> unsetIdOnDeactivation(event)
-    >>> foo.id
-    'foo'
     """
-    perm = event.object.component
-    if isinstance(perm, LocalPermission):
-        perm.id = NULL_ID
+    permission.id = NULL_ID
 
 
 def checkPermission(context, permission_id):

Modified: Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/configure.zcml	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/securitypolicy/configure.zcml	2005-02-07 22:50:03 UTC (rev 29078)
@@ -69,12 +69,14 @@
   </localUtility>
 
   <subscriber
-     for="..registration.interfaces.IRegistrationActivatedEvent"
+     for=".interfaces.IRole
+          ..registration.interfaces.IRegistrationActivatedEvent"
      handler=".role.setIdOnActivation"
      />
 
   <subscriber
-     for="..registration.interfaces.IRegistrationDeactivatedEvent"
+     for=".interfaces.IRole
+          ..registration.interfaces.IRegistrationDeactivatedEvent"
      handler=".role.unsetIdOnDeactivation"
      />
 

Modified: Zope3/trunk/src/zope/app/securitypolicy/role.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/role.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/securitypolicy/role.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -47,7 +47,7 @@
 from zope.app.utility import UtilityRegistration
 RoleRegistration = UtilityRegistration
 
-def setIdOnActivation(event):
+def setIdOnActivation(role, event):
     """Set the permission id upon registration activation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -61,35 +61,21 @@
     >>> role1 = LocalRole('Role 1', 'A first role')
     >>> role1.id
     u'<role not activated>'
-    >>> from zope.app.registration import registration 
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationActivatedEvent(
     ...     Registration(role1, 'role1'))
 
     Now we pass the event into this function, and the id of the role should be
     set to 'role1'.
 
-    >>> setIdOnActivation(event)
+    >>> setIdOnActivation(role1, event)
     >>> role1.id
     'role1'
-
-    If the function is called and the component is not a local permission,
-    nothing is done:
-
-    >>> class Foo:
-    ...     id = 'no id'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationActivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> setIdOnActivation(event)
-    >>> foo.id
-    'no id'
     """
-    role = event.object.component
-    if isinstance(role, LocalRole):
-        role.id = event.object.name
+    role.id = event.object.name
 
 
-def unsetIdOnDeactivation(event):
+def unsetIdOnDeactivation(role, event):
     """Unset the permission id up registration deactivation.
 
     Let's see how this notifier can be used. First we need to create an event
@@ -103,35 +89,21 @@
     >>> role1 = LocalRole('Role 1', 'A first role')
     >>> role1.id = 'role1'
 
-    >>> from zope.app.registration import registration 
+    >>> from zope.app.registration import registration
     >>> event = registration.RegistrationDeactivatedEvent(
     ...     Registration(role1, 'role1'))
 
     Now we pass the event into this function, and the id of the role should be
     set to NULL_ID.
 
-    >>> unsetIdOnDeactivation(event)
+    >>> unsetIdOnDeactivation(role1, event)
     >>> role1.id
     u'<role not activated>'
-
-    If the function is called and the component is not a local role,
-    nothing is done:
-
-    >>> class Foo:
-    ...     id = 'foo'
-    >>> foo = Foo()
-    >>> event = registration.RegistrationDeactivatedEvent(
-    ...     Registration(foo, 'foo'))
-    >>> unsetIdOnDeactivation(event)
-    >>> foo.id
-    'foo'
     """
-    role = event.object.component
-    if isinstance(role, LocalRole):
-        role.id = NULL_ID
+    role.id = NULL_ID
 
-    
 
+
 def checkRole(context, role_id):
     names = [name for name, util in zapi.getUtilitiesFor(IRole, context)]
     if not role_id in names:

Modified: Zope3/trunk/src/zope/app/site/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/site/configure.zcml	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/site/configure.zcml	2005-02-07 22:50:03 UTC (rev 29078)
@@ -1,4 +1,4 @@
-<configure 
+<configure
     xmlns="http://namespaces.zope.org/zope"
     >
 
@@ -7,13 +7,15 @@
   </module>
 
   <subscriber
-     for="..registration.interfaces.IRegistrationActivatedEvent"
-     handler=".service.handleActivated"
+     for=".interfaces.IBindingAware
+          ..registration.interfaces.IRegistrationActivatedEvent"
+     handler=".service.bindOnActivated"
      />
 
   <subscriber
-     for="..registration.interfaces.IRegistrationDeactivatedEvent"
-     handler=".service.handleDeactivated"
+     for=".interfaces.IBindingAware
+          ..registration.interfaces.IRegistrationDeactivatedEvent"
+     handler=".service.unbindOnDeactivated"
      />
 
   <!-- Service Manager -->
@@ -65,14 +67,14 @@
     <require
         permission="zope.ManageServices"
         interface="zope.app.container.interfaces.IWriteContainer"
-        attributes="getRegistrationManager resolve" 
+        attributes="getRegistrationManager resolve"
         />
     <implements
         interface="zope.app.annotation.interfaces.IAttributeAnnotatable" />
 
     </content>
-  
-  <adapter 
+
+  <adapter
       for="zope.app.site.interfaces.ISiteManager"
       provides="zope.app.filerepresentation.interfaces.IDirectoryFactory"
       factory=".folder.SMFolderFactory"

Modified: Zope3/trunk/src/zope/app/site/service.py
===================================================================
--- Zope3/trunk/src/zope/app/site/service.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/site/service.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -303,14 +303,9 @@
         return self.name + " Service"
 
 
-def handleActivated(event):
-    if isinstance(event.object, ServiceRegistration):
-        service = event.object.component
-        if IBindingAware.providedBy(service):
-            service.bound(event.object.name)
+def bindOnActivated(bindingAwareService, event):
+    bindingAwareService.bound(event.object.name)
 
-def handleDeactivated(event):
-    if isinstance(event.object, ServiceRegistration):
-        service = event.object.component
-        if IBindingAware.providedBy(service):
-            service.unbound(event.object.name)
+def unbindOnDeactivated(bindingAwareService, event):
+    bindingAwareService.unbound(event.object.name)
+

Modified: Zope3/trunk/src/zope/app/tests/setup.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/setup.py	2005-02-07 21:33:49 UTC (rev 29077)
+++ Zope3/trunk/src/zope/app/tests/setup.py	2005-02-07 22:50:03 UTC (rev 29078)
@@ -80,22 +80,30 @@
 # Use registration
 from zope.app.registration.interfaces import IAttributeRegisterable
 from zope.app.registration.interfaces import IRegistered
+from zope.app.registration.interfaces import IComponentRegistration
+from zope.app.registration.interfaces import IRegistrationEvent
 from zope.app.registration.registration import Registered
+from zope.app.registration.registration import componentRegistrationEventNotify
 def setUpRegistered():
     ztapi.provideAdapter(IAttributeRegisterable, IRegistered,
                          Registered)
+    ztapi.subscribe((IComponentRegistration, IRegistrationEvent), None,
+                     componentRegistrationEventNotify)
 
 #------------------------------------------------------------------------
 # Service service lookup
 from zope.app.component.localservice import serviceServiceAdapter
 from zope.app.registration.interfaces import IRegistrationActivatedEvent
 from zope.app.registration.interfaces import IRegistrationDeactivatedEvent
-from zope.app.site.service import handleActivated, handleDeactivated
+from zope.app.site.interfaces import IBindingAware
+from zope.app.site.service import bindOnActivated, unbindOnDeactivated
 from zope.component.interfaces import IServiceService
 from zope.interface import Interface
 def setUpServiceService():
-    ztapi.subscribe((IRegistrationActivatedEvent,), None, handleActivated)
-    ztapi.subscribe((IRegistrationDeactivatedEvent,), None, handleDeactivated)
+    ztapi.subscribe((IBindingAware, IRegistrationActivatedEvent), None,
+                     bindOnActivated)
+    ztapi.subscribe((IBindingAware, IRegistrationDeactivatedEvent), None,
+                     unbindOnDeactivated)
     ztapi.provideAdapter(Interface, IServiceService, serviceServiceAdapter)
 
 #------------------------------------------------------------------------
@@ -187,7 +195,7 @@
 
     This utility is useful for tests that need to set up utilities.
     """
-    
+
     folder_name = (name or (iface.__name__ + 'Utility')) + suffix
     default = zapi.traverse(servicemanager, 'default')
     default[folder_name] = utility



More information about the Zope3-Checkins mailing list