[Checkins] SVN: z3c.baseregistry/trunk/ merge the branch adamg-fix-with-ztk1.0, which makes z3c.baseregistry compatible wiht ZTK 1.0

Adam Groszer agroszer at gmail.com
Thu Oct 28 09:39:39 EDT 2010


Log message for revision 118007:
  merge the branch adamg-fix-with-ztk1.0, which makes z3c.baseregistry compatible wiht ZTK 1.0

Changed:
  U   z3c.baseregistry/trunk/CHANGES.txt
  U   z3c.baseregistry/trunk/setup.py
  U   z3c.baseregistry/trunk/src/z3c/baseregistry/README.txt
  U   z3c.baseregistry/trunk/src/z3c/baseregistry/zcml.py

-=-
Modified: z3c.baseregistry/trunk/CHANGES.txt
===================================================================
--- z3c.baseregistry/trunk/CHANGES.txt	2010-10-28 13:08:03 UTC (rev 118006)
+++ z3c.baseregistry/trunk/CHANGES.txt	2010-10-28 13:39:39 UTC (rev 118007)
@@ -2,10 +2,15 @@
 CHANGES
 =======
 
-1.2.1 (unreleased)
+1.3.0 (unreleased)
 ------------------
 
-- ...
+- *Fundamental change* in the way how baseregistry hooks into ZCA.
+  Now it uses ``hooks.setSite``, which requires that ``zope.component`` hooks
+  are in place. Usually they are installed by ``zope.app.appsetup``.
+  Unless you use ``zope.app.appsetup``, install the hooks with
+  ``zope.component.hooks.setHooks()``.
+  This applies to ``zope.component`` versions >= 3.9.4.
 
 
 1.2.0 (2009-12-27)

Modified: z3c.baseregistry/trunk/setup.py
===================================================================
--- z3c.baseregistry/trunk/setup.py	2010-10-28 13:08:03 UTC (rev 118006)
+++ z3c.baseregistry/trunk/setup.py	2010-10-28 13:39:39 UTC (rev 118007)
@@ -67,7 +67,7 @@
         ),
     install_requires = [
         'setuptools',
-        'zope.component',
+        'zope.component >= 3.9.4',
         'zope.configuration',
         'zope.i18nmessageid',
         'zope.interface',

Modified: z3c.baseregistry/trunk/src/z3c/baseregistry/README.txt
===================================================================
--- z3c.baseregistry/trunk/src/z3c/baseregistry/README.txt	2010-10-28 13:08:03 UTC (rev 118006)
+++ z3c.baseregistry/trunk/src/z3c/baseregistry/README.txt	2010-10-28 13:39:39 UTC (rev 118007)
@@ -87,6 +87,13 @@
   >>> myRegistry
   <BaseComponents myRegistry>
 
+Another *VERY IMPORTANT* requirement is that ``zope.component`` hooks are in
+place. Install the hooks now:
+
+  >>> import zope.component.hooks
+  >>> zope.component.hooks.setHooks()
+
+
 Since this registry does not implement any of the ``IComponents`` API itself,
 it is not necessary to demonstrate those features here. Please see the
 corresponding documentation in the ``zope.component`` package.
@@ -201,18 +208,61 @@
   >>> example1 = Example('example1')
   >>> example2 = Example('example2')
 
-Let' now register "example1" in the global registry and "example2" in our
-custom registry:
+Create some adapters we can register:
 
+  >>> class IToAdapt1(zope.interface.Interface):
+  ...     pass
+
+  >>> class IToAdapt2(zope.interface.Interface):
+  ...     pass
+
+  >>> class IAdapted(zope.interface.Interface):
+  ...     pass
+
+  >>> @zope.component.adapter(IToAdapt1)
+  ... @zope.interface.implementer(IAdapted)
+  ... def adapter1(context):
+  ...     return "adapted1"
+
+  >>> @zope.component.adapter(IToAdapt2)
+  ... @zope.interface.implementer(IAdapted)
+  ... def adapter2(context):
+  ...     return "adapted2"
+
+  >>> class ToAdapt1(object):
+  ...     zope.interface.implements(IToAdapt1)
+  ...     def __init__(self, name):
+  ...         self.name = name
+  ...     def __repr__(self):
+  ...         return '<%s %r>' %(self.__class__.__name__, self.name)
+  >>> toAdapt1 = ToAdapt1('toAdapt1')
+
+  >>> class ToAdapt2(object):
+  ...     zope.interface.implements(IToAdapt2)
+  ...     def __init__(self, name):
+  ...         self.name = name
+  ...     def __repr__(self):
+  ...         return '<%s %r>' %(self.__class__.__name__, self.name)
+  >>> toAdapt2 = ToAdapt2('toAdapt2')
+
+Let' now register "example1", adapter1 in the global registry
+and "example2", "adapter2" in our custom registry:
+
   >>> context = xmlconfig.string('''
   ... <configure xmlns="http://namespaces.zope.org/zope" i18n_domain="zope">
   ...
   ...   <utility component="README.example1"
   ...            name="example1" />
+  ...   <adapter
+  ...         factory="README.adapter1"
+  ...         name="adapter1"/>
   ...
   ...   <registerIn registry="README.custom">
   ...     <utility component="README.example2"
   ...              name="example2" />
+  ...     <adapter
+  ...         factory="README.adapter2"
+  ...         name="adapter2"/>
   ...   </registerIn>
   ...
   ... </configure>
@@ -229,6 +279,18 @@
   ...
   ComponentLookupError: (<InterfaceClass README.IExample>, 'example2')
 
+Let's now make sure that the adapters have been registered in the right
+registry:
+
+  >>> zope.component.getAdapter(toAdapt1, IAdapted, name="adapter1")
+  'adapted1'
+
+  >>> zope.component.getAdapter(toAdapt2, IAdapted, name="adapter2")
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: (<ToAdapt2 'toAdapt2'>, <InterfaceClass README.IAdapted>, 'adapter2')
+
+
   >>> custom = zope.component.getUtility(IComponents, name='custom')
 
   >>> custom.getUtility(IExample, name="example1")
@@ -239,6 +301,16 @@
   >>> custom.getUtility(IExample, name="example2")
   <Example 'example2'>
 
+
+  >>> custom.getAdapter(toAdapt1, IAdapted, name="adapter1")
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: (<ToAdapt1 'toAdapt1'>, <InterfaceClass README.IAdapted>, 'adapter1')
+
+  >>> custom.getAdapter(toAdapt2, IAdapted, name="adapter2")
+  'adapted2'
+
+
 Let's now register other instances of the ``Example`` class without a
 name. This should *not* cause a conflict error:
 
@@ -295,6 +367,14 @@
   ...
   ComponentLookupError: (<InterfaceClass README.IExample>, 'example2')
 
+  >>> sm.getAdapter(toAdapt1, IAdapted, name="adapter1")
+  'adapted1'
+
+  >>> sm.getAdapter(toAdapt2, IAdapted, name="adapter2")
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: (<ToAdapt2 'toAdapt2'>, <InterfaceClass README.IAdapted>, 'adapter2')
+
 But if we add the "custom" registry, then things look more interesting:
 
   >>> sm.__bases__ += (custom,)
@@ -310,6 +390,12 @@
   >>> sm.getUtility(IExample, name="example2")
   <Example 'example2'>
 
+  >>> sm.getAdapter(toAdapt1, IAdapted, name="adapter1")
+  'adapted1'
+
+  >>> sm.getAdapter(toAdapt2, IAdapted, name="adapter2")
+  'adapted2'
+
 But where is the registration for example 4? Well, the order of the bases
 matters, like the order of base classes in Python matters. The bases run from
 must specific to most generic. Thus, if we reverse the order,
@@ -466,7 +552,14 @@
   ZopeXMLConfigurationError: File "<string>", line 5...
     ConfigurationError: Nested ``registerIn`` directives are not permitted.
 
+Cleanup
+~~~~~~~
 
+Just unregister the ``zope.component`` hooks:
+
+  >>> zope.component.hooks.resetHooks()
+
+
 Global Non-Component-Registration Actions
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: z3c.baseregistry/trunk/src/z3c/baseregistry/zcml.py
===================================================================
--- z3c.baseregistry/trunk/src/z3c/baseregistry/zcml.py	2010-10-28 13:08:03 UTC (rev 118006)
+++ z3c.baseregistry/trunk/src/z3c/baseregistry/zcml.py	2010-10-28 13:39:39 UTC (rev 118007)
@@ -18,6 +18,7 @@
 __docformat__ = "reStructuredText"
 import zope.interface
 import zope.component.globalregistry
+import zope.component.hooks
 import zope.configuration.config
 import zope.configuration.fields
 from zope.configuration.exceptions import ConfigurationError
@@ -73,13 +74,25 @@
         return getattr(self.original, name)
 
 
+class FakeBaseRegistrySite(object):
+    """This a minimal fake Site, the only responsibility it has
+    is to store our registry as a SiteManager and return it later.
+    This is needed to fool siteinfo via setSite, zope.component.zcml.handler
+    will grab the registry via zope.component.getSiteManager() then."""
+
+    def __init__(self, sm):
+        self.sm = sm
+
+    def getSiteManager(self):
+        return self.sm
+
 def setActiveRegistry(context, registry):
-    context.original = zope.component.globalregistry.globalSiteManager
-    # Set the temporary, base registry
-    zope.component.globalregistry.globalSiteManager = registry
+    context.original = zope.component.hooks.getSite()
+    fakeSite = FakeBaseRegistrySite(registry)
+    zope.component.hooks.setSite(fakeSite)
 
 def resetOriginalRegistry(context):
-    zope.component.globalregistry.globalSiteManager = context.original
+    zope.component.hooks.setSite(context.original)
 
 
 class RegisterIn(zope.configuration.config.GroupingContextDecorator):



More information about the checkins mailing list