[Zope3-checkins] SVN: Zope3/trunk/ Addressed issue 297. I did not solve it, since I could not create a new

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Dec 9 15:12:05 EST 2004


Log message for revision 28605:
  Addressed issue 297. I did not solve it, since I could not create a new 
  class, but the type of class that is permissable now is much stricter. 
  Local utilities have to implement IPersistent and ILocation (or at least 
  provide __parent__ and __name__).
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/doc/TODO.txt
  U   Zope3/trunk/src/zope/app/utility/metaconfigure.py
  U   Zope3/trunk/src/zope/app/utility/tests.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-12-09 20:10:58 UTC (rev 28604)
+++ Zope3/trunk/doc/CHANGES.txt	2004-12-09 20:12:03 UTC (rev 28605)
@@ -67,6 +67,10 @@
         `ILocalUtility` and `IAttributeAnnotatable`. I also changed all the
         local utility declarations to use this new directive.
 
+        The directive now also makes sure that the class implements the
+        `IPersistent` and `ILocation` interfaces; without those a class cannot
+        be a local utility. This addresses issue 297.  
+
       - The `Attribute` and `Method` class of the `zope.interface` have now a
         new public `interface` attribute that stores the interface they are
         defined in.  

Modified: Zope3/trunk/doc/TODO.txt
===================================================================
--- Zope3/trunk/doc/TODO.txt	2004-12-09 20:10:58 UTC (rev 28604)
+++ Zope3/trunk/doc/TODO.txt	2004-12-09 20:12:03 UTC (rev 28605)
@@ -29,8 +29,6 @@
 
 - Issue 295: Sort out defaultView 
 
-- Issue 297: Using class as localUtility 
-
 - Issue 309: <schemadisplay> should support <widget>
 
 

Modified: Zope3/trunk/src/zope/app/utility/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/utility/metaconfigure.py	2004-12-09 20:10:58 UTC (rev 28604)
+++ Zope3/trunk/src/zope/app/utility/metaconfigure.py	2004-12-09 20:12:03 UTC (rev 28605)
@@ -16,16 +16,68 @@
 $Id$
 """
 __docformat__ = "reStructuredText"
+from persistent.interfaces import IPersistent
+from zope.configuration.exceptions import ConfigurationError
 from zope.interface import classImplements
+
+from zope.app.annotation.interfaces import IAttributeAnnotatable
 from zope.app.component.contentdirective import ContentDirective
-from zope.app.annotation.interfaces import IAttributeAnnotatable
+from zope.app.location.interfaces import ILocation
 
 from interfaces import ILocalUtility
 
 
 class LocalUtilityDirective(ContentDirective):
+    r"""localUtility directive handler.
 
+    Examples:
+
+      >>> from zope.interface import implements
+      >>> class LU1(object):
+      ...     pass
+
+      >>> class LU2(LU1):
+      ...     implements(ILocation)
+
+      >>> class LU3(LU1):
+      ...     __parent__ = None
+
+      >>> class LU4(LU2):
+      ...     implements(IPersistent)
+
+      >>> dir = LocalUtilityDirective(None, LU4)
+      >>> IAttributeAnnotatable.implementedBy(LU4)
+      True
+      >>> ILocalUtility.implementedBy(LU4)
+      True
+
+      >>> LocalUtilityDirective(None, LU3)
+      Traceback (most recent call last):
+      ...
+      ConfigurationError: Class `LU3` does not implement `IPersistent`.
+
+      >>> LocalUtilityDirective(None, LU2)
+      Traceback (most recent call last):
+      ...
+      ConfigurationError: Class `LU2` does not implement `IPersistent`.
+
+      >>> LocalUtilityDirective(None, LU1)
+      Traceback (most recent call last):
+      ...
+      ConfigurationError: Class `LU1` does not implement `ILocation`.
+    """
+
     def __init__(self, _context, class_):
+        if not ILocation.implementedBy(class_) and \
+               not hasattr(class_, '__parent__'):
+            raise ConfigurationError, \
+                  'Class `%s` does not implement `ILocation`.' %class_.__name__
+
+        if not IPersistent.implementedBy(class_):
+            raise ConfigurationError, \
+                 'Class `%s` does not implement `IPersistent`.' %class_.__name__
+
         classImplements(class_, IAttributeAnnotatable)
         classImplements(class_, ILocalUtility)
+
         super(LocalUtilityDirective, self).__init__(_context, class_)

Modified: Zope3/trunk/src/zope/app/utility/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/utility/tests.py	2004-12-09 20:10:58 UTC (rev 28604)
+++ Zope3/trunk/src/zope/app/utility/tests.py	2004-12-09 20:12:03 UTC (rev 28605)
@@ -17,6 +17,7 @@
 """
 import unittest
 from StringIO import StringIO
+from persistent.interfaces import IPersistent
 
 from zope.component import getService
 from zope.component.exceptions import ComponentLookupError
@@ -27,19 +28,20 @@
 import zope.app.security
 import zope.app.utility
 from zope.app.tests import setup
-from zope.app.site.tests import placefulsetup
 from zope.app import utility, zapi
 from zope.app.annotation.interfaces import IAttributeAnnotatable
+from zope.app.dependable.interfaces import IDependable
+from zope.app.location.interfaces import ILocation
 from zope.app.traversing.api import traverse
 from zope.app.registration.interfaces import IRegistrationStack
 from zope.app.registration.interfaces import UnregisteredStatus
 from zope.app.registration.interfaces import RegisteredStatus
 from zope.app.registration.interfaces import ActiveStatus
 from zope.app.registration.interfaces import IRegistered
-from zope.app.utility.interfaces import ILocalUtility
-from zope.app.dependable.interfaces import IDependable
+from zope.app.site.tests import placefulsetup
 from zope.app.tests import setup
 from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.app.utility.interfaces import ILocalUtility
 
 
 def configfile(s):
@@ -100,7 +102,7 @@
 
 
 class UtilityStub(object):
-    pass
+    implements(ILocation, IPersistent)
 
 
 class TestUtilityService(placefulsetup.PlacefulSetup, unittest.TestCase):
@@ -257,6 +259,7 @@
     return unittest.TestSuite((
         unittest.makeSuite(TestUtilityService),
         unittest.makeSuite(TestLocalUtilityDirective),
+        DocTestSuite('zope.app.utility.metaconfigure'),
         DocTestSuite('zope.app.utility.vocabulary',
                      setUp=setup.placelessSetUp,
                      tearDown=setup.placelessTearDown)



More information about the Zope3-Checkins mailing list