Updated "extension:" TALES expression to be able to handle inner attributes with dotted notation
authorThierry Florac <thierry.florac@onf.fr>
Wed, 20 May 2015 12:40:19 +0200
changeset 33 e1aca8c25e61
parent 32 c784991b55a4
child 34 b70afbacf63b
Updated "extension:" TALES expression to be able to handle inner attributes with dotted notation
src/pyams_utils/tales.py
--- a/src/pyams_utils/tales.py	Wed May 20 12:39:38 2015 +0200
+++ b/src/pyams_utils/tales.py	Wed May 20 12:40:19 2015 +0200
@@ -41,6 +41,19 @@
 
 
 FUNCTION_EXPRESSION = re.compile('(.+)\((.+)\)')
+ARGUMENTS_EXPRESSION = re.compile('[\'\"\w\.\+@]+')
+
+
+def get_value(econtext, arg):
+    """Extract argument value from context"""
+    if arg.startswith('"') or arg.startswith("'"):
+        # may be a quoted string...
+        return arg[1:-1]
+    args = arg.split('.')
+    result = econtext.get(args.pop(0))
+    for arg in args:
+        result = getattr(result, arg)
+    return result
 
 
 def render_extension(econtext, name):
@@ -52,10 +65,10 @@
 
     func_match = FUNCTION_EXPRESSION.match(name)
     if func_match:
-        name, argument = func_match.groups()
-        arg_value = econtext.get(argument, argument)
+        name, arguments = func_match.groups()
+        arg_value = map(lambda x: get_value(econtext, x), ARGUMENTS_EXPRESSION.findall(arguments))
     else:
-        arg_value = None
+        arg_value = ()
 
     registry = request.registry
     extension = registry.queryMultiAdapter((context, request, view), ITALESExtension, name=name)
@@ -71,10 +84,13 @@
     # Insert the data gotten from the context
     addTALNamespaceData(extension, econtext)
 
-    return extension.render(arg_value)
+    return extension.render(*arg_value)
 
 
 class ExtensionExpr(ContextExprMixin, StringExpr):
-    """extension: TALES expression"""
+    """extension: TALES expression
+
+    This expression can be used a call a custom named adapter providing ITALESExtension interface.
+    """
 
     transform = Symbol(render_extension)