[Zope3-checkins] SVN: Zope3/trunk/ Removed `activated()` and `deactivated()` methods from Registration. We

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Dec 5 16:01:41 EST 2004


Log message for revision 28567:
  Removed `activated()` and `deactivated()` methods from Registration. We 
  handle those with events now. The change is fully backward compatible. No 
  ZODB generation is needed.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/adapter/tests.py
  U   Zope3/trunk/src/zope/app/registration/interfaces.py
  U   Zope3/trunk/src/zope/app/registration/registration.py
  U   Zope3/trunk/src/zope/app/registration/tests/registrationstack.py
  U   Zope3/trunk/src/zope/app/registration/tests/test_registrations.py
  U   Zope3/trunk/src/zope/app/registration/tests/test_registrationstack.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/site/tests/test_serviceregistration.py
  U   Zope3/trunk/src/zope/app/tests/setup.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/doc/CHANGES.txt	2004-12-05 21:01:40 UTC (rev 28567)
@@ -145,6 +145,12 @@
 
     Restructuring
 
+      - Refactored Registration component for local components
+      
+        + Deprecated the `activated()` and `deactivated()` method. It is now
+          replaced with the 'IRegistrationActivatedEvent' and
+          'IRegistrationDectivatedEvent', respectively.
+
       - Replaced NotFoundError uses with more specific and meaningful
         exceptions. Eventually, NotFoundError will be deprecated.
 

Modified: Zope3/trunk/src/zope/app/adapter/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/adapter/tests.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/adapter/tests.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -11,8 +11,116 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Local Adapter Tests
+"""Local Adapter and Service Tests
 
+$Id$
+"""
+import unittest
+import zope.interface
+from transaction import get_transaction
+from ZODB.tests.util import DB
+
+from zope.testing.doctestunit import DocTestSuite
+from zope.interface.adapter import AdapterRegistry
+from zope.component.adapter import GlobalAdapterService
+
+from zope.app import zapi
+from zope.app.adapter.adapter import LocalAdapterRegistry, LocalAdapterService
+from zope.app.registration.interfaces import RegisteredStatus
+
+class IF0(zope.interface.Interface):
+    pass
+
+class IF1(IF0):
+    pass
+
+class IF2(IF1):
+    pass
+
+class IB0(zope.interface.Interface):
+    pass
+
+class IB1(IB0):
+    pass
+
+class IR0(zope.interface.Interface):
+    pass
+
+class IR1(IR0):
+    pass
+
+class R1(object):
+    zope.interface.implements(IR1)
+
+class F0(object):
+    zope.interface.implements(IF0)
+
+class F2(object):
+    zope.interface.implements(IF2)
+
+class Registration(object):
+    name=u''
+    with=()
+    provided=zope.interface.Interface
+    required=None
+    
+    def __init__(self, **kw):
+        self.__dict__.update(kw)
+
+    def __repr__(self):
+        return "Registration(%r, %s, %r, %r, %r)" % (
+            getattr(self.required, '__name__', None),
+            tuple([i.__name__ for i in self.with]),
+            self.provided.__name__, self.name, self.factory
+            )
+    
+
+    def factories(self):
+        return self.factory,
+    factories = property(factories)
+
+# Create a picklable global registry. The pickleability of other
+# global adapter registries is beyond the scope of these tests:
+class GlobalAdapterRegistry(AdapterRegistry):
+    def __reduce__(self):
+        return 'globalAdapterRegistry'
+
+globalAdapterRegistry = GlobalAdapterRegistry()
+
+class TestStack(object):
+    registration = None
+    registrations = ()
+
+    def __init__(self, parent):
+        self.__parent__ = parent
+
+    def active(self):
+        return self.registration
+
+    def info(self):
+        for registration in self.registrations:
+            yield {'registration': registration}
+
+    def activate(self, registration):
+        self.registration = registration
+        if registration not in self.registrations:
+            self.registrations += (registration,)
+        self.__parent__.notifyActivated(self, registration)
+
+    def deactivate(self, registration):
+        self.registration = None
+        self.__parent__.notifyDeactivated(self, registration)
+
+
+class LocalAdapterRegistry(LocalAdapterRegistry):
+    """For testing, use custom stack type
+    """
+    _stackType = TestStack
+
+
+def test_local_adapter():
+    """Local Adapter Tests
+
    Local surrogates and adapter registries share declarations with
    those "above" them.
 
@@ -131,8 +239,6 @@
    [Registration('IF0', (), 'IB1', u'', 'A011')]
 
    This shows only the local registrations in L1.
-
-   $Id$
    """
 
 def test_named_adapters():
@@ -696,177 +802,73 @@
     >>> setup.placefulTearDown()
     """
 
+def test_service_registrations():
+    """
+    Local Adapter Service Registration Tests
 
 
-import unittest
-from zope.testing.doctestunit import DocTestSuite
-from zope.interface.adapter import AdapterRegistry
-from zope.component.adapter import GlobalAdapterService
-from zope.app.adapter.adapter import LocalAdapterRegistry, LocalAdapterService
-import zope.interface
-from ZODB.tests.util import DB
-from transaction import get_transaction
-from zope.app import zapi
-from zope.app.registration.interfaces import RegisteredStatus
+    Local adapter services share declarations and registrations with those
+    "above" them.
 
-class IF0(zope.interface.Interface):
-    pass
+    Suppose we have a global adapter service, which is a type of
+    adapter registry that is an zope.component.interfaces.IRegistry:
 
-class IF1(IF0):
-    pass
+    >>> G = GlobalAdapterService()
 
-class IF2(IF1):
-    pass
-
-class IB0(zope.interface.Interface):
-    pass
-
-class IB1(IB0):
-    pass
-
-class IR0(zope.interface.Interface):
-    pass
-
-class IR1(IR0):
-    pass
-
-class R1(object):
-    zope.interface.implements(IR1)
-
-class F0(object):
-    zope.interface.implements(IF0)
-
-class F2(object):
-    zope.interface.implements(IF2)
-
-class Registration(object):
-    name=u''
-    with=()
-    provided=zope.interface.Interface
-    required=None
+    we also have a local adapter registry, with G as it's base:
     
-    def __init__(self, **kw):
-        self.__dict__.update(kw)
+    >>> L1 = LocalAdapterService(G)
+    
+    and another local, with G as it's base:
 
-    def __repr__(self):
-        return "Registration(%r, %s, %r, %r, %r)" % (
-            getattr(self.required, '__name__', None),
-            tuple([i.__name__ for i in self.with]),
-            self.provided.__name__, self.name, self.factory
-            )
+    >>> L2 = LocalAdapterService(G)
     
+    and L1 as it's next service:
+    
+    >>> L2.setNext(L1)
+    
+    Now will register some adapters:
+    
+    >>> G.register([IF1], IB1, '', 'A11G')
+    >>> ra011 = Registration(required = IF0, provided=IB1, factory='A011')
+    >>> L1.createRegistrationsFor(ra011).register(ra011)
+    >>> ra112 = Registration(required = IF1, provided=IB1, factory='A112')
+    >>> L2.createRegistrationsFor(ra112).register(ra112)
+    >>> ra102 = Registration(required = IF1, provided=IB0, factory='A102')
+    >>> L2.createRegistrationsFor(ra102).register(ra102)
+    
+    We can ask for all of the registrations locally:
+    
+    >>> registrations = map(repr, L1.registrations())
+    >>> registrations.sort()
+    >>> for registration in registrations:
+    ...     print registration
+    AdapterRegistration(('IF1',), 'IB1', '', 'A11G', '')
+    Registration('IF0', (), 'IB1', u'', 'A011')
+    
+    This shows the local registrations in L1 and the global registrations.
+    
+    If we ask L2, we'll see the registrations from G, L1, and L2:
+    
+    >>> registrations = map(repr, L2.registrations())
+    >>> registrations.sort()
+    >>> for registration in registrations:
+    ...     print registration
+    AdapterRegistration(('IF1',), 'IB1', '', 'A11G', '')
+    Registration('IF0', (), 'IB1', u'', 'A011')
+    Registration('IF1', (), 'IB0', u'', 'A102')
+    Registration('IF1', (), 'IB1', u'', 'A112')
+    
+    Now we just want the local registrations for L1:
+    
+    >>> registrations = map(repr, L1.registrations(localOnly=True))
+    >>> registrations.sort()
+    >>> for registration in registrations:
+    ...     print registration
+    Registration('IF0', (), 'IB1', u'', 'A011')
 
-    def factories(self):
-        return self.factory,
-    factories = property(factories)
-
-# Create a picklable global registry. The pickleability of other
-# global adapter registries is beyond the scope of these tests:
-class GlobalAdapterRegistry(AdapterRegistry):
-    def __reduce__(self):
-        return 'globalAdapterRegistry'
-
-globalAdapterRegistry = GlobalAdapterRegistry()
-
-class TestStack(object):
-    registration = None
-    registrations = ()
-
-    def __init__(self, parent):
-        self.__parent__ = parent
-
-    def activate(self, registration):
-        self.registration = registration
-        if registration not in self.registrations:
-            self.registrations += (registration,)
-        self.__parent__.notifyActivated(self, registration)
-
-    def deactivate(self, registration):
-        self.registration = None
-        self.__parent__.notifyDeactivated(self, registration)
-
-    def active(self):
-        return self.registration
-
-    def info(self):
-        for registration in self.registrations:
-            yield {'registration': registration}
-
-class LocalAdapterRegistry(LocalAdapterRegistry):
-    """For testing, use custom stack type
     """
-    _stackType = TestStack
-
-
-def test_service_registrations():
-    """Local Adapter Service Registration Tests
-
-
-       Local adapter services share declarations and registrations with
-       those "above" them.
-
-       Suppose we have a global adapter service, which is a type of
-       adapter registry that is an zope.component.interfaces.IRegistry:
-
-       >>> G = GlobalAdapterService()
-
-       we also have a local adapter registry, with G as it's base:
-
-       >>> L1 = LocalAdapterService(G)
-
-       and another local, with G as it's base:
-
-       >>> L2 = LocalAdapterService(G)
-
-       and L1 as it's next service:
-       
-       >>> L2.setNext(L1)
-
-       Now will register some adapters:
-
-       >>> G.register([IF1], IB1, '', 'A11G')
-       >>> ra011 = Registration(required = IF0, provided=IB1, factory='A011')
-       >>> L1.createRegistrationsFor(ra011).register(ra011)
-       >>> ra112 = Registration(required = IF1, provided=IB1, factory='A112')
-       >>> L2.createRegistrationsFor(ra112).register(ra112)
-       >>> ra102 = Registration(required = IF1, provided=IB0, factory='A102')
-       >>> L2.createRegistrationsFor(ra102).register(ra102)
-
-       We can ask for all of the registrations locally:
-
-       >>> registrations = map(repr, L1.registrations())
-       >>> registrations.sort()
-       >>> for registration in registrations:
-       ...     print registration
-       AdapterRegistration(('IF1',), 'IB1', '', 'A11G', '')
-       Registration('IF0', (), 'IB1', u'', 'A011')
-
-       This shows the local registrations in L1 and the global registrations.
-
-       If we ask L2, we'll see the registrations from G, L1, and L2:
-
-       >>> registrations = map(repr, L2.registrations())
-       >>> registrations.sort()
-       >>> for registration in registrations:
-       ...     print registration
-       AdapterRegistration(('IF1',), 'IB1', '', 'A11G', '')
-       Registration('IF0', (), 'IB1', u'', 'A011')
-       Registration('IF1', (), 'IB0', u'', 'A102')
-       Registration('IF1', (), 'IB1', u'', 'A112')
-
-       Now we just want the local registrations for L1:
-
-       >>> registrations = map(repr, L1.registrations(localOnly=True))
-       >>> registrations.sort()
-       >>> for registration in registrations:
-       ...     print registration
-       Registration('IF0', (), 'IB1', u'', 'A011')
-
-       $Id$
-       """
     
-    
-
 def test_suite():
     return unittest.TestSuite((
         DocTestSuite(),

Modified: Zope3/trunk/src/zope/app/registration/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/interfaces.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/registration/interfaces.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -15,22 +15,34 @@
 
 $Id$
 """
+import zope.component.interfaces
+from zope.interface import Interface, Attribute, implements
+from zope.schema import TextLine, Field, Choice
+from zope.schema.interfaces import ITextLine
+
 from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.app.annotation.interfaces import IAnnotatable
 from zope.app.annotation.interfaces import IAttributeAnnotatable
 from zope.app.container.interfaces import IContainerNamesContainer
 from zope.app.container.interfaces import IContained, IContainer
-from zope.interface import Interface, Attribute, implements
-from zope.schema import TextLine, Field, Choice
-from zope.schema.interfaces import ITextLine
 from zope.app.container.constraints import ItemTypePrecondition
 from zope.app.container.constraints import ContainerTypesConstraint
-import zope.component.interfaces
+from zope.app.event.interfaces import IObjectEvent
 
 UnregisteredStatus = _('Unregistered')
 RegisteredStatus = _('Registered')
 ActiveStatus = _('Active')
 
+
+class IRegistrationEvent(IObjectEvent):
+    """An event that involves a registration"""
+
+class IRegistrationActivatedEvent(IRegistrationEvent):
+    """This event is fired, when a component's registration is activated."""
+
+class IRegistrationDeactivatedEvent(IRegistrationEvent):
+    """This event is fired, when a component's registration is deactivated."""
+
 class INoLocalServiceError(Interface):
     """No local service to register with.
     """
@@ -68,6 +80,9 @@
         default=UnregisteredStatus
         )
 
+    # BBB Deprecated methods, since their functionality is better implemented
+    # using event, which is done now. 12/05/2004
+    # ----------------------------------------------------------------------
     def activated():
         """Method called when a registration is made active.
         """
@@ -75,6 +90,7 @@
     def deactivated():
         """Method called when a registration is made inactive.
         """
+    # ----------------------------------------------------------------------
 
     def usageSummary():
         """Single-line usage summary.

Modified: Zope3/trunk/src/zope/app/registration/registration.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/registration.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/registration/registration.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -16,11 +16,11 @@
 $Id$
 """
 import sys
+import warnings
 from persistent import Persistent
 
 import zope.cachedescriptors.property
 import zope.event
-
 from zope.interface import implements
 from zope.exceptions import DuplicationError
 from zope.proxy import removeAllProxies, getProxiedObject
@@ -29,16 +29,27 @@
 
 from zope.app import zapi
 from zope.app.annotation.interfaces import IAttributeAnnotatable
+from zope.app.component.localservice import getLocalServices
 from zope.app.container.contained import Contained
 from zope.app.container.contained import setitem, contained, uncontained
 from zope.app.copypastemove import ObjectCopier
 from zope.app.dependable.interfaces import IDependable, DependencyError
 from zope.app.event import objectevent
-from zope.app.component.localservice import getLocalServices
 from zope.app.location import inside
 from zope.app.module.interfaces import IModuleManager
 from zope.app.registration import interfaces
 
+
+class RegistrationEvent(objectevent.ObjectEvent):
+    implements(interfaces.IRegistrationEvent)
+
+class RegistrationActivatedEvent(RegistrationEvent):
+    implements(interfaces.IRegistrationActivatedEvent)
+
+class RegistrationDeactivatedEvent(RegistrationEvent):
+    implements(interfaces.IRegistrationDeactivatedEvent)
+
+
 class RegistrationStatusProperty(object):
 
     def __get__(self, inst, klass):
@@ -105,9 +116,10 @@
        as will be described below.
 
        Registration stacks manage registrations.  They don't really care
-       what registrations are, as long as they can be activated and
-       deactivated:
+       what registrations are.
 
+         >>> from zope.app.registration import interfaces
+
          >>> class Registration(object):
          ...
          ...     def __init__(self, name):
@@ -117,14 +129,25 @@
          ...     def __repr__(self):
          ...         return self.name
          ...
-         ...     def activated(self):
-         ...         self.active = True
-         ...
-         ...     def deactivated(self):
-         ...         self.active = False
 
+       When a registration is activated or deactivated, an event is published
+       to which one can subscribe; in our case the registration components
+       themselves subscribe to these events to set their status.
+         
+         >>> def setActive(event):
+         ...     event.object.active = True
+         >>> subscribe((interfaces.IRegistrationActivatedEvent,), None,
+         ...           setActive)
+         
+         >>> def unsetActive(event):
+         ...     event.object.active = False
+         >>> subscribe((interfaces.IRegistrationDeactivatedEvent,), None,
+         ...           unsetActive)
+
+
        We create a registration stack by providing it with a parent:
 
+         >>> from zope.app.registration.registration import RegistrationStack 
          >>> stack = RegistrationStack(42)
          >>> stack.__parent__
          42
@@ -282,10 +305,27 @@
         return registration in self.data
 
     def _activate(self, registration):
-        registration.activated()
+        zope.event.notify(RegistrationActivatedEvent(registration))
+        # BBB: Depraction warningl 12/05/2004
+        if hasattr(registration, 'activated'):
+            #import pdb; pdb.set_trace()
+            warnings.warn(
+                "activated() deprected. Subscribe to "
+                "IRegistrationActivatedEvent instead.",
+                DeprecationWarning, stacklevel=3,
+                )
+            registration.activated()
 
     def _deactivate(self, registration):
-        registration.deactivated()
+        zope.event.notify(RegistrationDeactivatedEvent(registration))
+        # BBB: Depraction warningl 12/05/2004
+        if hasattr(registration, 'deactivated'):
+            warnings.warn(
+                "deactivated() deprected. Subscribe to "
+                "IRegistrationDeactivatedEvent instead.",
+                DeprecationWarning, stacklevel=3,
+                )
+            registration.deactivated()
 
     def activate(self, registration):
         data = self.data
@@ -300,7 +340,9 @@
 
             # Insert it in front, removing it from back
             data = ((registration, ) +
-                    tuple([item for item in data if item != registration])
+                    tuple([item
+                           for item in data
+                           if item != registration])
                     )
 
             # Check for trailing None
@@ -374,7 +416,7 @@
         return [r for r in result if r['registration'] is not None]
 
     #########################################################################
-    # Backward compat
+    # BBB: Backward compat
     #
     def data(self):
         # Need to convert old path-based data to object-based data
@@ -399,6 +441,11 @@
     #
     #########################################################################
 
+
+#############################################################################
+# The functionality provided by this class can is better implemented by
+# subscribing to the RegistrationActivatedEvent and
+# RegistrationDeactivatedEvent.
 class NotifyingRegistrationStack(RegistrationStack):
     """Notifying registration registry implemention
 
@@ -424,6 +471,7 @@
        in the RegistrationStack documentation.
        A registration stack provides support for a collection of
 
+         >>> from zope.app.registration import interfaces
          >>> class Registration(object):
          ...
          ...     def __init__(self, name):
@@ -432,17 +480,22 @@
          ...
          ...     def __repr__(self):
          ...         return self.name
-         ...
-         ...     def activated(self):
-         ...         self.active = True
-         ...
-         ...     def deactivated(self):
-         ...         self.active = False
 
+         >>> def setActive(event):
+         ...     event.object.active = True
+         >>> subscribe((interfaces.IRegistrationActivatedEvent,), None,
+         ...           setActive)
+         
+         >>> def unsetActive(event):
+         ...     event.object.active = False
+         >>> subscribe((interfaces.IRegistrationDeactivatedEvent,), None,
+         ...           unsetActive)
+
        We create a registration stack by providing it with a parent:
 
          >>> parent = Parent()
-         >>> stack = NotifyingRegistrationStack(parent)
+         >>> from zope.app.registration import registration
+         >>> stack = registration.NotifyingRegistrationStack(parent)
 
        We can register a registration:
 
@@ -511,15 +564,16 @@
        Because there wasn't an active registration before we made r2
        active.
        """
-
     def _activate(self, registration):
-        registration.activated()
+        super(NotifyingRegistrationStack, self)._activate(registration)
         self.__parent__.notifyActivated(self, registration)
 
     def _deactivate(self, registration):
-        registration.deactivated()
+        super(NotifyingRegistrationStack, self)._activate(registration)
         self.__parent__.notifyDeactivated(self, registration)
+#############################################################################
 
+
 def SimpleRegistrationRemoveSubscriber(registration, event):
     """Receive notification of remove event."""
 
@@ -542,6 +596,7 @@
     elif objectstatus == interfaces.RegisteredStatus:
         registration.status = interfaces.UnregisteredStatus
 
+
 class SimpleRegistration(Persistent, Contained):
     """Registration objects that just contain registration data"""
 
@@ -555,18 +610,20 @@
 
     # Methods from IRegistration
 
-    def activated(self):
-        pass
+    # BBB: Deprecated on 12/05/2004
+    # def activated(self):
+    #     pass
+    #
+    # def deactivated(self):
+    #     pass
 
-    def deactivated(self):
-        pass
-
     def usageSummary(self):
         return self.__class__.__name__
 
     def implementationSummary(self):
         return ""
 
+
 class ComponentRegistration(SimpleRegistration):
     """Component registration.
 

Modified: Zope3/trunk/src/zope/app/registration/tests/registrationstack.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/tests/registrationstack.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/registration/tests/registrationstack.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -37,10 +37,6 @@
     def __repr__(self):
         return self.id
 
-    def activated(self):
-        pass
-
-    deactivated = activated
     
 from zope.app.registration.registration import RegistrationStack
 

Modified: Zope3/trunk/src/zope/app/registration/tests/test_registrations.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/tests/test_registrations.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/registration/tests/test_registrations.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -17,9 +17,23 @@
 """
 
 from unittest import TestCase, TestSuite, main, makeSuite
-from doctest import DocTestSuite
+from zope.testing.doctestunit import DocTestSuite
 
+import zope.interface
 from zope.interface import Interface, implements
+from zope.security.proxy import Proxy
+
+from zope.app.annotation.interfaces import IAnnotations
+from zope.app.container.contained import Contained
+from zope.app.container.contained import ObjectRemovedEvent, ObjectMovedEvent
+from zope.app.container.interfaces import IObjectRemovedEvent
+from zope.app.dependable.interfaces import IDependable
+from zope.app.registration.interfaces import IRegistration
+from zope.app.site.tests.placefulsetup import PlacefulSetup
+from zope.app.tests import ztapi, placelesssetup
+from zope.app.traversing.api import traverse
+from zope.app.traversing.interfaces import IPhysicallyLocatable
+
 from zope.app.registration.interfaces import UnregisteredStatus
 from zope.app.registration.interfaces import RegisteredStatus
 from zope.app.registration.interfaces import ActiveStatus
@@ -28,15 +42,6 @@
 from zope.app.dependable.interfaces import DependencyError
 from zope.app.registration.registration import \
      SimpleRegistration, ComponentRegistration
-from zope.app.site.tests.placefulsetup import PlacefulSetup
-from zope.app.dependable.interfaces import IDependable
-from zope.app.traversing.api import traverse
-from zope.security.proxy import Proxy
-from zope.app.container.contained import Contained
-from zope.app.container.contained import ObjectRemovedEvent, ObjectMovedEvent
-from zope.app.tests import ztapi
-from zope.app.registration.interfaces import IRegistration
-from zope.app.container.interfaces import IObjectRemovedEvent
 from zope.app.registration.registration import \
     SimpleRegistrationRemoveSubscriber, \
     ComponentRegistrationRemoveSubscriber, \
@@ -44,10 +49,8 @@
     RegisterableMoveSubscriber
 from zope.app.registration.registration import Registered
 from zope.app.registration.registration import RegisterableCopier
-from zope.app.traversing.interfaces import IPhysicallyLocatable
-import zope.interface
-from zope.app.annotation.interfaces import IAnnotations
 
+
 class ITestComponent(Interface):
     pass
 
@@ -310,13 +313,15 @@
     """
 
 def test_suite():
-    import sys
     return TestSuite((
         makeSuite(TestSimpleRegistration),
         makeSuite(TestComponentRegistration),
         makeSuite(TestSimpleRegistrationEvents),
         DocTestSuite(),
-        DocTestSuite('zope.app.registration.registration'),
+        DocTestSuite('zope.app.registration.registration',
+                     setUp=placelesssetup.setUp,
+                     tearDown=placelesssetup.tearDown,
+                     globs={'subscribe': ztapi.subscribe}),
         ))
 
 if __name__=='__main__':

Modified: Zope3/trunk/src/zope/app/registration/tests/test_registrationstack.py
===================================================================
--- Zope3/trunk/src/zope/app/registration/tests/test_registrationstack.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/registration/tests/test_registrationstack.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -16,31 +16,43 @@
 $Id$
 """
 from unittest import TestCase, TestSuite, main, makeSuite
+
 from zope.app.site.tests.placefulsetup import PlacefulSetup
-from zope.app.registration.registration import RegistrationStack
+from zope.app.tests import ztapi
 from zope.app.traversing.api import traverse
 
+from zope.app.registration.registration import RegistrationStack
+from zope.app.registration import interfaces
+
 class Registration(object):
 
     active = 0
     registry = None
 
-    def activated(self):
-        if (self.registry is not None) and (self.registry.active() != self):
-            raise AssertionError("Told active but not the active registration")
-        self.active += 1
 
-    def deactivated(self):
-        if (self.registry is not None) and (self.registry.active() == self):
-            raise AssertionError(
-                "Told deactivated but still the active registration")
-        self.active -= 1
+def handleActivated(event):
+    reg = event.object
+    if (reg.registry is not None) and (reg.registry.active() != reg):
+        raise AssertionError("Told active but not the active registration")
+    reg.active += 1
 
+def handleDeactivated(event):
+    reg = event.object
+    if (reg.registry is not None) and (reg.registry.active() == reg):
+        raise AssertionError(
+            "Told deactivated but still the active registration")
+    reg.active -= 1
 
+
 class Test(PlacefulSetup, TestCase):
 
     def setUp(self):
         PlacefulSetup.setUp(self, site=True)
+        ztapi.subscribe((interfaces.IRegistrationActivatedEvent,), None,
+                        handleActivated)
+        ztapi.subscribe((interfaces.IRegistrationDeactivatedEvent,), None,
+                        handleDeactivated)
+
         root = self.rootFolder
         self.__default = traverse(root, "++etc++site/default")
         self.__registry = RegistrationStack(root)

Modified: Zope3/trunk/src/zope/app/site/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/site/configure.zcml	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/site/configure.zcml	2004-12-05 21:01:40 UTC (rev 28567)
@@ -6,6 +6,16 @@
     <allow attributes="ISite" />
   </module>
 
+  <subscriber
+     for="..registration.interfaces.IRegistrationActivatedEvent"
+     factory=".service.handleActivated"
+     />
+
+  <subscriber
+     for="..registration.interfaces.IRegistrationDeactivatedEvent"
+     factory=".service.handleDeactivated"
+     />
+
   <!-- Service Manager -->
 
   <content class=".service.SiteManager">

Modified: Zope3/trunk/src/zope/app/site/service.py
===================================================================
--- Zope3/trunk/src/zope/app/site/service.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/site/service.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -307,15 +307,18 @@
         service_manager = zapi.getServices(self)
         return service_manager.getInterfaceFor(self.name)
 
-    def activated(self):
-        service = self.getComponent()
+    def usageSummary(self):
+        return self.name + " Service"
+
+
+def handleActivated(event):
+    if isinstance(event.object, ServiceRegistration):
+        service = event.object.getComponent()
         if IBindingAware.providedBy(service):
-            service.bound(self.name)
+            service.bound(event.object.name)
 
-    def deactivated(self):
-        service = self.getComponent()
+def handleDeactivated(event):
+    if isinstance(event.object, ServiceRegistration):
+        service = event.object.getComponent()
         if IBindingAware.providedBy(service):
-            service.unbound(self.name)
-
-    def usageSummary(self):
-        return self.name + " Service"
+            service.unbound(event.object.name)

Modified: Zope3/trunk/src/zope/app/site/tests/test_serviceregistration.py
===================================================================
--- Zope3/trunk/src/zope/app/site/tests/test_serviceregistration.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/site/tests/test_serviceregistration.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -67,6 +67,11 @@
 class TestService(TestServiceBase):
     implements(ISimpleService)
 
+class Event(object):
+
+    def __init__(self, object):
+        self.object = object
+
 class Test(PlacefulSetup, TestCase):
 
     def setUp(self):
@@ -91,14 +96,18 @@
         self.__config = traverse(self.__cm, self.__registration_name)
         self.__configpath = getPath(self.__config)
 
-    def test_activated(self):
+    def test_handleActivated(self):
         old = self.__c._bound
-        self.__config.activated()
+        event = Event(self.__config)
+        from zope.app.site.service import handleActivated
+        handleActivated(event)
         self.assertEqual(self.__c._bound, old+('test_service',))
 
     def test_deactivated(self):
         old = self.__c._unbound
-        self.__config.deactivated()
+        event = Event(self.__config)
+        from zope.app.site.service import handleDeactivated
+        handleDeactivated(event)
         self.assertEqual(self.__c._unbound, old+('test_service',))
 
     def test_getInterface(self):

Modified: Zope3/trunk/src/zope/app/tests/setup.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/setup.py	2004-12-05 20:59:33 UTC (rev 28566)
+++ Zope3/trunk/src/zope/app/tests/setup.py	2004-12-05 21:01:40 UTC (rev 28567)
@@ -88,9 +88,14 @@
 #------------------------------------------------------------------------
 # 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.component.interfaces import IServiceService
 from zope.interface import Interface
 def setUpServiceService():
+    ztapi.subscribe((IRegistrationActivatedEvent,), None, handleActivated)
+    ztapi.subscribe((IRegistrationDeactivatedEvent,), None, handleDeactivated)
     ztapi.provideAdapter(Interface, IServiceService, serviceServiceAdapter)
 
 #------------------------------------------------------------------------



More information about the Zope3-Checkins mailing list