[ZPT] CVS: Releases/Zope/lib/python/Products/PageTemplates - PathIterator.py:1.2.2.1 Expressions.py:1.25.2.3 PageTemplate.py:1.19.2.1 TALES.py:1.24.2.2 ZopePageTemplate.py:1.28.2.1
Evan Simpson
evan@zope.com
Thu, 13 Dec 2001 15:26:04 -0500
Update of /cvs-repository/Releases/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv20474/Products/PageTemplates
Modified Files:
Tag: Zope-2_5-branch
Expressions.py PageTemplate.py TALES.py ZopePageTemplate.py
Added Files:
Tag: Zope-2_5-branch
PathIterator.py
Log Message:
Merge changes from trunk
=== Added File Releases/Zope/lib/python/Products/PageTemplates/PathIterator.py ===
##############################################################################
#
# Copyright (c) 2001 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
#
##############################################################################
"""Path Iterator
A TALES Iterator with the ability to use first() and last() on
subpaths of elements.
"""
__version__='$Revision: 1.2.2.1 $'[11:-2]
import TALES
from Expressions import restrictedTraverse, Undefs, getSecurityManager
from string import split
class Iterator(TALES.Iterator):
def __bobo_traverse__(self, REQUEST, name):
if name in ('first', 'last'):
path = REQUEST['TraversalRequestNameStack']
names = list(path)
names.reverse()
path[:] = [tuple(names)]
return getattr(self, name)
def same_part(self, name, ob1, ob2):
if name is None:
return ob1 == ob2
if isinstance(name, type('')):
name = split(name, '/')
name = filter(None, name)
securityManager = getSecurityManager()
try:
ob1 = restrictedTraverse(ob1, name, securityManager)
ob2 = restrictedTraverse(ob2, name, securityManager)
except Undefs:
return 0
return ob1 == ob2
=== Releases/Zope/lib/python/Products/PageTemplates/Expressions.py 1.25.2.2 => 1.25.2.3 ===
import re, sys
from TALES import Engine, CompilerError, _valid_name, NAME_RE, \
- TALESError, Undefined, Default
+ TALESError, Undefined, Default, _parse_expr
from string import strip, split, join, replace, lstrip
from Acquisition import aq_base, aq_inner, aq_parent
+
_engine = None
def getEngine():
global _engine
if _engine is None:
- _engine = Engine()
+ from PathIterator import Iterator
+ _engine = Engine(Iterator)
installHandlers(_engine)
_engine._nocatch = (TALESError, 'Redirect')
return _engine
@@ -46,7 +48,11 @@
if sys.modules.has_key('Zope'):
import AccessControl
- from AccessControl import getSecurityManager, Unauthorized
+ from AccessControl import getSecurityManager
+ try:
+ from AccessControl import Unauthorized
+ except ImportError:
+ Unauthorized = "Unauthorized"
if hasattr(AccessControl, 'full_read_guard'):
from ZRPythonExpr import PythonExpr, _SecureModuleImporter, \
call_with_ns
@@ -66,6 +72,9 @@
else:
return f(ns)
+Undefs = (Undefined, AttributeError, KeyError,
+ TypeError, IndexError, Unauthorized)
+
def render(ob, ns):
"""
Calls the object, possibly a document template, or just returns it if
@@ -86,75 +95,98 @@
raise
return ob
-class PathExpr:
- def __init__(self, name, expr, engine):
- self._s = expr
- self._name = name
- self._paths = map(self._prepPath, split(expr, '|'))
-
- def _prepPath(self, path):
- path = split(strip(path), '/')
- base = path.pop(0)
+class SubPathExpr:
+ def __init__(self, path):
+ self._path = path = split(strip(path), '/')
+ self._base = base = path.pop(0)
if not _valid_name(base):
raise CompilerError, 'Invalid variable name "%s"' % base
# Parse path
- dp = []
+ self._dp = dp = []
for i in range(len(path)):
e = path[i]
if e[:1] == '?' and _valid_name(e[1:]):
dp.append((i, e[1:]))
dp.reverse()
- return base, path, dp
- def _eval(self, econtext, securityManager,
- list=list, isinstance=isinstance, StringType=type(''),
- render=render):
+ def _eval(self, econtext,
+ list=list, isinstance=isinstance, StringType=type('')):
vars = econtext.vars
- exists = 0
- more_paths = len(self._paths)
- for base, path, dp in self._paths:
- more_paths = more_paths - 1
- # Expand dynamic path parts from right to left.
- if dp:
- path = list(path) # Copy!
- for i, varname in dp:
- val = vars[varname]
- if isinstance(val, StringType):
- path[i] = val
- else:
- # If the value isn't a string, assume it's a sequence
- # of path names.
- path[i:i+1] = list(val)
- try:
- __traceback_info__ = base
- if base == 'CONTEXTS':
- ob = econtext.contexts
+ path = self._path
+ if self._dp:
+ path = list(path) # Copy!
+ for i, varname in self._dp:
+ val = vars[varname]
+ if isinstance(val, StringType):
+ path[i] = val
else:
- ob = vars[base]
- if isinstance(ob, DeferWrapper):
- ob = ob()
- if path:
- ob = restrictedTraverse(ob, path, securityManager)
- exists = 1
+ # If the value isn't a string, assume it's a sequence
+ # of path names.
+ path[i:i+1] = list(val)
+ __traceback_info__ = base = self._base
+ if base == 'CONTEXTS':
+ ob = econtext.contexts
+ else:
+ ob = vars[base]
+ if isinstance(ob, DeferWrapper):
+ ob = ob()
+ if path:
+ ob = restrictedTraverse(ob, path, getSecurityManager())
+ return ob
+
+class PathExpr:
+ def __init__(self, name, expr, engine):
+ self._s = expr
+ self._name = name
+ paths = split(expr, '|')
+ self._subexprs = []
+ add = self._subexprs.append
+ for i in range(len(paths)):
+ path = lstrip(paths[i])
+ if _parse_expr(path):
+ # This part is the start of another expression type,
+ # so glue it back together and compile it.
+ add(engine.compile(lstrip(join(paths[i:], '|'))))
break
- except Undefined:
- if self._name != 'exists' and not more_paths:
- raise
- except (AttributeError, KeyError, TypeError, IndexError,
- Unauthorized), e:
- if self._name != 'exists' and not more_paths:
- raise Undefined(self._s, sys.exc_info())
+ add(SubPathExpr(path)._eval)
+
+ def _exists(self, econtext):
+ for expr in self._subexprs:
+ try:
+ expr(econtext)
+ except Undefs:
+ pass
+ else:
+ return 1
+ return 0
+
+ def _eval(self, econtext,
+ isinstance=isinstance, StringType=type(''), render=render):
+ for expr in self._subexprs[:-1]:
+ # Try all but the last subexpression, skipping undefined ones
+ try:
+ ob = expr(econtext)
+ except Undefs:
+ pass
+ else:
+ break
+ else:
+ # On the last subexpression allow exceptions through, but
+ # wrap ones that indicate that the subexpression was undefined
+ try:
+ ob = self._subexprs[-1](econtext)
+ except Undefs[1:]:
+ raise Undefined(self._s, sys.exc_info())
- if self._name == 'exists':
- # All we wanted to know is whether one of the paths exist.
- return exists
if self._name == 'nocall' or isinstance(ob, StringType):
return ob
# Return the rendered object
- return render(ob, vars)
+ return render(ob, econtext.vars)
def __call__(self, econtext):
- return self._eval(econtext, getSecurityManager())
+ if self._name == 'exists':
+ return self._exists(econtext)
+ return self._eval(econtext)
def __str__(self):
return '%s expression %s' % (self._name, `self._s`)
@@ -240,24 +272,28 @@
def restrictedTraverse(self, path, securityManager,
- get=getattr, has=hasattr, N=None, M=[]):
+ get=getattr, has=hasattr, N=None, M=[],
+ TupleType=type(()) ):
- i = 0
+ REQUEST = {'path': path}
+ REQUEST['TraversalRequestNameStack'] = path = path[:] # Copy!
if not path[0]:
# If the path starts with an empty string, go to the root first.
self = self.getPhysicalRoot()
if not securityManager.validateValue(self):
raise Unauthorized, name
- i = 1
-
- plen = len(path)
- REQUEST={'TraversalRequestNameStack': path}
+ path.pop(0)
+
+ path.reverse()
validate = securityManager.validate
object = self
- while i < plen:
- __traceback_info__ = (path, i)
- name = path[i]
- i = i + 1
+ while path:
+ __traceback_info__ = REQUEST
+ name = path.pop()
+
+ if isinstance(name, TupleType):
+ object = apply(object, name)
+ continue
if name[0] == '_':
# Never allowed in a URL.
=== Releases/Zope/lib/python/Products/PageTemplates/PageTemplate.py 1.19 => 1.19.2.1 ===
_v_errors = ()
_v_warnings = ()
+ id = '(unknown)'
_text = ''
_error_start = '<!-- Page Template Diagnostics'
=== Releases/Zope/lib/python/Products/PageTemplates/TALES.py 1.24.2.1 => 1.24.2.2 ===
try:
if ZTUtils.Iterator.next(self):
- self._context.setLocal(self.name, self.item())
+ self._context.setLocal(self.name, self.item)
return 1
except TALESError:
raise
=== Releases/Zope/lib/python/Products/PageTemplates/ZopePageTemplate.py 1.28 => 1.28.2.1 ===
from Shared.DC.Scripts.Script import Script, BindingsUI
from Shared.DC.Scripts.Signature import FuncCode
-from AccessControl import getSecurityManager, Unauthorized
+from AccessControl import getSecurityManager
+try:
+ from AccessControl import Unauthorized
+except ImportError:
+ Unauthorized = "Unauthorized"
from OFS.History import Historical, html_diff
from OFS.Cache import Cacheable
from OFS.Traversable import Traversable