[Erp5-report] r43879 arnaud.fontaine - /erp5/trunk/products/ERP5Type/Utils.py

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Mar 2 11:51:06 CET 2011


Author: arnaud.fontaine
Date: Wed Mar  2 11:51:06 2011
New Revision: 43879

URL: http://svn.erp5.org?rev=43879&view=rev
Log:
Add evaluateExpressionFromString allowing to evaluate a TALES
Expression string

Modified:
    erp5/trunk/products/ERP5Type/Utils.py

Modified: erp5/trunk/products/ERP5Type/Utils.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Utils.py?rev=43879&r1=43878&r2=43879&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Utils.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/Utils.py [utf8] Wed Mar  2 11:51:06 2011
@@ -2948,6 +2948,33 @@ def createTranslationLanguageAccessors(p
       property_holder.declareProtected(accessor_dict['permission'],
                                        accessor_name)
 
+from zope.tales.tales import CompilerError
+
+def evaluateExpressionFromString(expression_context, expression_string):
+  """
+  Evaluate a TALES Expression from the given string with the given
+  Expression context (as returned by createExpressionContext for
+  instance).
+
+  Any exception normally raised when parsing and evaluating a TALES
+  Expression is re-raised as a ValueError.
+
+  @param expression_context: Expression context
+  @type expression_context: Products.PageTemplates.Expressions.ZopeContext
+  @param expression_string: TALES Expression string to evaluate
+  @type expression_string: str
+  """
+  if expression_string is None:
+    return None
+
+  try:
+    return Expression(expression_string)(expression_context)
+  # An AttributeError is raised when instanciating an Expression
+  # class, and CompilerError and ValueError are raised in case of
+  # error when evaluation the expression
+  except (AttributeError, CompilerError, ValueError), e:
+    raise ValueError("Error in TALES expression: '%s': %s" % (expression_string,
+                                                              str(e)))
 
 #####################################################
 # More Useful methods which require Base



More information about the Erp5-report mailing list