vendor/Five/1.2b-r20590

view traversable.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Machinery for making things traversable through adaptation
16 $Id: traversable.py 19051 2005-10-27 02:59:50Z whit $
17 """
18 from zExceptions import NotFound
19 from zope.exceptions import NotFoundError
20 from zope.component import getView, ComponentLookupError
21 from zope.interface import implements
22 from zope.publisher.interfaces.browser import IBrowserRequest
23 from zope.app.traversing.interfaces import ITraverser, ITraversable
24 from zope.app.traversing.adapters import DefaultTraversable
25 from zope.app.traversing.adapters import traversePathElement
27 from AccessControl import getSecurityManager
28 from Products.Five.security import newInteraction
30 _marker = object
32 class FakeRequest(dict):
33 implements(IBrowserRequest)
35 def getPresentationSkin(self):
36 return None
38 def has_key(self, key):
39 return False
41 def getURL(self):
42 return "http://codespeak.net/z3/five"
44 class Traversable:
45 """A mixin to make an object traversable using an ITraverser adapter.
46 """
47 __five_traversable__ = True
49 def __fallback_traverse__(self, REQUEST, name):
50 """Method hook for fallback traversal
52 This method is called by __bobo_traverse___ when Zope3-style
53 ITraverser traversal fails.
55 Just raise a AttributeError to indicate traversal has failed
56 and let Zope do it's job.
57 """
58 raise AttributeError, name
60 def __bobo_traverse__(self, REQUEST, name):
61 """Hook for Zope 2 traversal
63 This method is called by Zope 2's ZPublisher upon traversal.
64 It allows us to trick it into faking the Zope 3 traversal system
65 by using an ITraverser adapter.
66 """
67 if not IBrowserRequest.providedBy(REQUEST):
68 # Try to get the REQUEST by acquisition
69 REQUEST = getattr(self, 'REQUEST', None)
70 if not IBrowserRequest.providedBy(REQUEST):
71 REQUEST = FakeRequest()
72 # con Zope 3 into using Zope 2's checkPermission
73 newInteraction()
74 try:
75 return ITraverser(self).traverse(
76 path=[name], request=REQUEST).__of__(self)
77 except (ComponentLookupError, NotFoundError,
78 AttributeError, KeyError, NotFound):
79 pass
80 try:
81 return getattr(self, name)
82 except AttributeError:
83 pass
84 try:
85 return self[name]
86 except (AttributeError, KeyError):
87 pass
88 return self.__fallback_traverse__(REQUEST, name)
89 __bobo_traverse__.__five_method__ = True
92 class FiveTraversable(DefaultTraversable):
94 def traverse(self, name, furtherPath):
95 context = self._subject
96 __traceback_info__ = (context, name, furtherPath)
97 # Find the REQUEST
98 REQUEST = getattr(context, 'REQUEST', None)
99 if not IBrowserRequest.providedBy(REQUEST):
100 REQUEST = FakeRequest()
101 # Try to lookup a view
102 try:
103 return getView(context, name, REQUEST)
104 except ComponentLookupError:
105 pass