vendor/Five/1.2b-r20590

view browser/TrustedExpression.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 """Trusted expression
16 $Id: TrustedExpression.py 18543 2005-10-14 13:37:26Z regebro $
17 """
18 from sys import modules
20 from Products.PageTemplates.PythonExpr import PythonExpr
22 from Products.PageTemplates.Expressions import \
23 SubPathExpr, PathExpr, \
24 StringExpr, \
25 getEngine, installHandlers,\
26 SecureModuleImporter
28 from ReuseUtils import rebindFunction
30 ModuleImporter = SecureModuleImporter
32 def trustedTraverse(ob, path, ignored,):
33 if not path: return self
35 get = getattr
36 has = hasattr
37 N = None
38 M = rebindFunction # artifical marker
40 if isinstance(path, str): path = path.split('/')
41 else: path=list(path)
43 REQUEST={'TraversalRequestNameStack': path}
44 path.reverse()
45 pop=path.pop
47 if len(path) > 1 and not path[0]:
48 # Remove trailing slash
49 path.pop(0)
51 if not path[-1]:
52 # If the path starts with an empty string, go to the root first.
53 pop()
54 self=ob.getPhysicalRoot()
56 object = ob
57 while path:
58 name=pop()
59 __traceback_info__ = path, name
61 if name == '..':
62 o=getattr(object, 'aq_parent', M)
63 if o is not M:
64 object=o
65 continue
67 t=get(object, '__bobo_traverse__', M)
68 if t is not M: o=t(REQUEST, name)
69 else:
70 o = get(object, name, M)
71 if o is M:
72 try: o = object[name]
73 except (AttributeError, TypeError): # better exception
74 raise AttributeError(name)
75 object = o
77 return object
80 class SubPathExpr(SubPathExpr):
81 _eval = rebindFunction(SubPathExpr._eval.im_func,
82 restrictedTraverse=trustedTraverse,
83 )
85 class PathExpr(PathExpr):
86 __init__ = rebindFunction(PathExpr.__init__.im_func,
87 SubPathExpr=SubPathExpr,
88 )
90 class StringExpr(StringExpr):
91 __init__ = rebindFunction(StringExpr.__init__.im_func,
92 PathExpr=PathExpr,
93 )
95 installHandlers = rebindFunction(installHandlers,
96 PathExpr=PathExpr,
97 StringExpr=StringExpr,
98 PythonExpr=PythonExpr,
99 )
101 _engine=None
102 getEngine = rebindFunction(getEngine,
103 _engine=_engine,
104 installHandlers=installHandlers
105 )