[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ Changed the tales ob/adapter: mechanism to use named adapters to IPathAdapter

Jim Fulton jim at zope.com
Tue Jun 1 19:32:44 EDT 2004


Log message for revision 25167:
Changed the tales ob/adapter: mechanism to use named adapters to IPathAdapter

Now, to register a path adapter, register a named adapter to
IPathAdapter.

Removed the tales:namespace zcml directive.  (also merged 
metadirectives.py into metaconfigure.py.)




-=-
Modified: Zope3/trunk/src/zope/app/pagetemplate/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/configure.zcml	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/configure.zcml	2004-06-01 23:32:44 UTC (rev 25167)
@@ -11,9 +11,11 @@
       factory=".talesapi.ZopeTalesAPI" 
       />
 
-  <tales:namespace 
-      prefix="zope"
-      interface=".interfaces.IZopeTalesAPI"
+  <adapter
+      for="*"
+      provides="zope.app.traversing.interfaces.IPathAdapter"
+      factory=".talesapi.ZopeTalesAPI"
+      name="zope" 
       />
 
  <content class="zope.tales.tales.Iterator">

Modified: Zope3/trunk/src/zope/app/pagetemplate/engine.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/engine.py	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/engine.py	2004-06-01 23:32:44 UTC (rev 25167)
@@ -33,8 +33,8 @@
 from zope.app import zapi
 from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.app.traversing.adapters import Traverser
+from zope.app.traversing.interfaces import IPathAdapter
 
-
 class InlineCodeError(Exception):
     pass
 
@@ -112,8 +112,62 @@
                 self.setGlobal(name, value)
         return result
 
+
+class AdapterNamespaces(object):
+    """Simulate tales function namespaces with adapter lookup.
+
+    When we are asked for a namespace, we return an object that
+    actually computes an adapter when called:
+
+    To demonstrate this, we need to register an adapter:
+
+      >>> from zope.app.tests.placelesssetup import setUp, tearDown
+      >>> setUp()
+      >>> from zope.app.tests import ztapi
+      >>> def adapter1(ob):
+      ...     return 1
+      >>> ztapi.provideAdapter(None, IPathAdapter, adapter1, 'a1')
+
+    Now, with this adapter in place, we can try out the namespaces:
+
+      >>> ob = object()
+      >>> namespaces = AdapterNamespaces()
+      >>> namespace = namespaces['a1']
+      >>> namespace(ob)
+      1
+      >>> namespace = namespaces['a2']
+      >>> namespace(ob)
+      Traceback (most recent call last):
+      ...
+      KeyError: 'a2'
+
+
+    Cleanup:
+    
+      >>> tearDown()
+    """
+
+    def __init__(self):
+        self.namespaces = {}
+
+    def __getitem__(self, name):
+        namespace = self.namespaces.get(name)
+        if namespace is None:
+            def namespace(object):
+                try:
+                    return zapi.getAdapter(object, IPathAdapter, name=name)
+                except ComponentLookupError:
+                    raise KeyError, name
+                
+            self.namespaces[name] = namespace
+        return namespace
+
 class ZopeEngine(ExpressionEngine):
 
+    def __init__(self):
+        ExpressionEngine.__init__(self)
+        self.namespaces = AdapterNamespaces()
+
     def getContext(self, __namespace=None, **namespace):
         if __namespace:
             if namespace:

Modified: Zope3/trunk/src/zope/app/pagetemplate/meta.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/meta.zcml	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/meta.zcml	2004-06-01 23:32:44 UTC (rev 25167)
@@ -5,13 +5,8 @@
 
   <meta:directives namespace="http://namespaces.zope.org/tales">
 
-    <meta:directive name="namespace"
-        schema=".metadirectives.INamespaceDirective"
-        handler=".metaconfigure.namespace"
-        />
-
     <meta:directive name="expressiontype"
-        schema=".metadirectives.IExpressionTypeDirective"
+        schema=".metaconfigure.IExpressionTypeDirective"
         handler=".metaconfigure.expressiontype"
         />
 

Modified: Zope3/trunk/src/zope/app/pagetemplate/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/metaconfigure.py	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/metaconfigure.py	2004-06-01 23:32:44 UTC (rev 25167)
@@ -18,14 +18,27 @@
 """
 from zope.app.pagetemplate.engine import Engine, _Engine
 from zope.testing.cleanup import addCleanUp
+from zope.interface import Interface
+from zope.configuration.fields import GlobalObject
+from zope.schema import TextLine
 
-def namespace(_context, prefix, interface):
-    _context.action(
-        discriminator = ("tales:namespace", prefix),
-        callable = Engine.registerFunctionNamespace,
-        args = (prefix, lambda ob: interface(ob)),
+class IExpressionTypeDirective(Interface):
+    """Register a new TALES expression type"""
+
+    name = TextLine(
+        title=u"Name",
+        description=u"""Name of the expression. This will also be used
+        as the prefix in actual TALES expressions.""",
+        required=True
         )
 
+    handler = GlobalObject(
+        title=u"Handler",
+        description=u"""Handler is class that implements
+        zope.tales.interfaces.ITALESExpression.""",
+        required=True
+        )
+
 def expressiontype(_context, name, handler):
     _context.action(
         discriminator = ("tales:expressiontype", name),

Deleted: Zope3/trunk/src/zope/app/pagetemplate/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/metadirectives.py	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/metadirectives.py	2004-06-01 23:32:44 UTC (rev 25167)
@@ -1,65 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2003 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (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.
-#
-##############################################################################
-"""ZCML configuration directives for configuring the default zope:
-namespace in TALES.
-
-$Id$
-"""
-
-from zope.interface import Interface
-from zope.configuration.fields import GlobalObject
-from zope.schema import TextLine
-
-class INamespaceDirective(Interface):
-    """
-    Define a new tales namespace
-     
-    A namespace is defined by providing a prefix and an interface. A
-    handler for the namespace will be obtained by looking up an
-    adapter for the given interface.
-    """
-
-    prefix = TextLine(
-        title=u"The prefix used in tales expressions.",
-        description=u"""
-        For example, if the prefix is "dc", then a tales expression
-        would look like: ``foo/bar/dc:title``.""",
-        required=True
-        )
-
-    interface = GlobalObject(
-        title=u"The namespace interface",
-        description=u"""
-        This is an interface that the namespace must provide.  we'll
-        get the namespace by getting an adapter for this
-        interface.""",
-        required=True
-        )
-
-class IExpressionTypeDirective(Interface):
-    """Register a new TALES expression type"""
-
-    name = TextLine(
-        title=u"Name",
-        description=u"""Name of the expression. This will also be used
-        as the prefix in actual TALES expressions.""",
-        required=True
-        )
-
-    handler = GlobalObject(
-        title=u"Handler",
-        description=u"""Handler is class that implements
-        zope.tales.interfaces.ITALESExpression.""",
-        required=True
-        )

Modified: Zope3/trunk/src/zope/app/pagetemplate/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/tests/test_directives.py	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/pagetemplate/tests/test_directives.py	2004-06-01 23:32:44 UTC (rev 25167)
@@ -36,21 +36,6 @@
    </configure>"""
 
 
-class I(Interface):
-    pass
-
-class Adapter:
-    implements(I, ITraversable)
-
-    def __init__(self, context):
-        pass
-  
-    def title(self):
-        return "42" 
-
-    def traverse(self, name, *args):
-        return getattr(self, name)
-
 class Handler:
     pass
 
@@ -60,23 +45,6 @@
         super(Test, self).setUp()
         XMLConfig('meta.zcml', zope.app.pagetemplate)()
 
-    def testTalesAPI1(self):
-        ztapi.provideAdapter(None, I, Adapter)
-
-        xmlconfig(StringIO(template % (
-            """
-            <tales:namespace
-              prefix="zope"
-              interface="zope.app.pagetemplate.tests.test_directives.I"
-              />
-            """
-            )))
-
-        e = Engine.compile('context/zope:title')
-        res = e(Engine.getContext(context=None))
-
-        self.assertEqual(res, '42')
-
     def testExpressionType(self):
         xmlconfig(StringIO(template % (
             """

Modified: Zope3/trunk/src/zope/app/traversing/namespace.py
===================================================================
--- Zope3/trunk/src/zope/app/traversing/namespace.py	2004-06-01 22:51:08 UTC (rev 25166)
+++ Zope3/trunk/src/zope/app/traversing/namespace.py	2004-06-01 23:32:44 UTC (rev 25167)
@@ -423,6 +423,10 @@
              ... except NotFoundError:
              ...     print 'no adapter'
              no adapter
+
+           Cleanup:
+    
+             >>> tearDown()
            """
         try:
             return component.getAdapter(self.context, IPathAdapter, name=name)




More information about the Zope3-Checkins mailing list