Commit b3ccc1ca authored by Arnaud Fontaine's avatar Arnaud Fontaine

Get properly the TALES Expression engine and CompilerError exception

for isValidTALESExpression. This fixes testDynamicClassGeneration
errors and failure for Zope 2.8.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@44027 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0680d221
...@@ -1207,9 +1207,15 @@ def initializeProduct( context, ...@@ -1207,9 +1207,15 @@ def initializeProduct( context,
icon = icon) icon = icon)
##################################################### #####################################################
# Constructor initialization # TALES Expression
##################################################### #####################################################
# This gets the Engine and CompilerError classes for TALES Expression
# wherever it is defined (which is different depending on the Zope
# version)
ExpressionEngine = getEngine()
CompilerError = ExpressionEngine.getCompilerError()
def createExpressionContext(object, portal=None): def createExpressionContext(object, portal=None):
""" """
Return a context used for evaluating a TALES expression. Return a context used for evaluating a TALES expression.
...@@ -1276,14 +1282,10 @@ def createExpressionContext(object, portal=None): ...@@ -1276,14 +1282,10 @@ def createExpressionContext(object, portal=None):
# the proper name these days # the proper name these days
'context': object, 'context': object,
} }
ec = getEngine().getContext(data) ec = ExpressionEngine.getContext(data)
tv[cache_key] = ec tv[cache_key] = ec
return ec return ec
# This gets the CompilerError class wherever it is defined (which is
# different depending on the Zope version)
CompilerError = getEngine().getCompilerError()
def evaluateExpressionFromString(expression_context, expression_string): def evaluateExpressionFromString(expression_context, expression_string):
""" """
Evaluate a TALES Expression from the given string with the given Evaluate a TALES Expression from the given string with the given
...@@ -1310,6 +1312,22 @@ def evaluateExpressionFromString(expression_context, expression_string): ...@@ -1310,6 +1312,22 @@ def evaluateExpressionFromString(expression_context, expression_string):
raise ValueError("Error in TALES expression: '%s': %s" % (expression_string, raise ValueError("Error in TALES expression: '%s': %s" % (expression_string,
str(e))) str(e)))
def isValidTALESExpression(value):
"""return if given value is valid TALES Expression.
This validator only validates Syntax of TALES Expression,
it does not tell that Expression is callable on given context
- value: string we try to compile
return tuple: (boolean result, error_message or None)
"""
try:
ExpressionEngine.compile(value)
except CompilerError, message:
return False, message
else:
return True, None
##################################################### #####################################################
# More Useful methods which require Base # More Useful methods which require Base
##################################################### #####################################################
...@@ -1724,22 +1742,3 @@ def reencodeUrlEscapes(url): ...@@ -1724,22 +1742,3 @@ def reencodeUrlEscapes(url):
url += [_reencodeUrlEscapes_map[c] for c in part] url += [_reencodeUrlEscapes_map[c] for c in part]
except StopIteration: except StopIteration:
return ''.join(url) return ''.join(url)
from zope.tales.engine import Engine
from zope.tales.tales import CompilerError
def isValidTALESExpression(value):
"""return if given value is valid TALES Expression.
This validator only validates Syntax of TALES Expression,
it does not tell that Expression is callable on given context
- value: string we try to compile
return tuple: (boolean result, error_message or None)
"""
try:
Engine.compile(value)
except CompilerError, message:
return False, message
else:
return True, None
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment