[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/traversing/ Added
++adapter++ traversal namespace.
Jim Fulton
jim at zope.com
Tue Jun 1 18:51:08 EDT 2004
Log message for revision 25166:
Added ++adapter++ traversal namespace.
It traverses by getting named adapters to
IPathAdapter.
-=-
Modified: Zope3/trunk/src/zope/app/traversing/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/traversing/configure.zcml 2004-06-01 21:54:23 UTC (rev 25165)
+++ Zope3/trunk/src/zope/app/traversing/configure.zcml 2004-06-01 22:51:08 UTC (rev 25166)
@@ -40,6 +40,18 @@
/>
<adapter
+ name="adapter"
+ provides="zope.app.traversing.interfaces.ITraversable" for="*"
+ factory="zope.app.traversing.namespace.adapter"
+ />
+<view
+ name="adapter"
+ type="zope.interface.Interface"
+ provides="zope.app.traversing.interfaces.ITraversable" for="*"
+ factory="zope.app.traversing.namespace.adapter"
+ />
+
+<adapter
name="item"
provides="zope.app.traversing.interfaces.ITraversable" for="*"
factory="zope.app.traversing.namespace.item"
Modified: Zope3/trunk/src/zope/app/traversing/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/traversing/interfaces.py 2004-06-01 21:54:23 UTC (rev 25165)
+++ Zope3/trunk/src/zope/app/traversing/interfaces.py 2004-06-01 22:51:08 UTC (rev 25166)
@@ -193,3 +193,6 @@
Raises ValueError if a badly formed path is given.
"""
+class IPathAdapter(Interface):
+ """Marker interface for adapters to be used in paths
+ """
Modified: Zope3/trunk/src/zope/app/traversing/namespace.py
===================================================================
--- Zope3/trunk/src/zope/app/traversing/namespace.py 2004-06-01 21:54:23 UTC (rev 25165)
+++ Zope3/trunk/src/zope/app/traversing/namespace.py 2004-06-01 22:51:08 UTC (rev 25166)
@@ -20,7 +20,7 @@
from zope import component
from zope.component.servicenames import Presentation
from zope.exceptions import NotFoundError
-from zope.app.traversing.interfaces import ITraversable
+from zope.app.traversing.interfaces import ITraversable, IPathAdapter
from zope.proxy import removeAllProxies
class UnexpectedParameters(NotFoundError):
@@ -388,3 +388,43 @@
request.setVirtualHostRoot(app_names)
return self.context
+
+
+class adapter(SimpleHandler):
+
+ def traverse(self, name, ignored):
+ """Adapter traversal adapter
+
+ This adapter provides traversal to named adapters registered to
+ provide IPathAdapter.
+
+ To demonstrate this, we need to register some adapters:
+
+ >>> from zope.app.tests.placelesssetup import setUp, tearDown
+ >>> setUp()
+ >>> from zope.app.tests import ztapi
+ >>> def adapter1(ob):
+ ... return 1
+ >>> def adapter2(ob):
+ ... return 2
+ >>> ztapi.provideAdapter(None, IPathAdapter, adapter1, 'a1')
+ >>> ztapi.provideAdapter(None, IPathAdapter, adapter2, 'a2')
+
+ Now, with these adapters in place, we can use the traversal adapter:
+
+ >>> ob = object()
+ >>> adapter = adapter(ob)
+ >>> adapter.traverse('a1', ())
+ 1
+ >>> adapter.traverse('a2', ())
+ 2
+ >>> try:
+ ... adapter.traverse('bob', ())
+ ... except NotFoundError:
+ ... print 'no adapter'
+ no adapter
+ """
+ try:
+ return component.getAdapter(self.context, IPathAdapter, name=name)
+ except:
+ raise NotFoundError(self.context, name)
More information about the Zope3-Checkins
mailing list