Updated TALES expression engine to recognize named arguments
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Jul 2018 17:11:35 +0200
changeset 218 9bf1c695938e
parent 217 46402f73be89
child 219 758f286d76af
Updated TALES expression engine to recognize named arguments
src/pyams_utils/tales.py
--- a/src/pyams_utils/tales.py	Mon Jul 09 13:44:09 2018 +0200
+++ b/src/pyams_utils/tales.py	Fri Jul 13 17:11:35 2018 +0200
@@ -51,11 +51,19 @@
     """
 
     def get_value(econtext, arg):
-        """Extract argument value from context"""
+        """Extract argument value from context
+
+        Extension expression language is quite simple. Values can be given as
+        positioned strings, integers or named arguments of the same types.
+        """
         arg = arg.strip()
         if arg.startswith('"') or arg.startswith("'"):
             # may be a quoted string...
             return arg[1:-1]
+        if '=' in arg:
+            key, value = arg.split('=', 1)
+            value = get_value(econtext, value)
+            return {key: value}
         try:
             arg = int(arg)  # check integer value
         except ValueError:
@@ -72,12 +80,15 @@
     request = econtext.get('request')
     view = econtext.get('view')
 
+    args, kwargs = [], {}
     func_match = FUNCTION_EXPRESSION.match(name)
     if func_match:
         name, arguments = func_match.groups()
-        arg_value = map(lambda x: get_value(econtext, x), ARGUMENTS_EXPRESSION.findall(arguments))
-    else:
-        arg_value = ()
+        for arg in map(lambda x: get_value(econtext, x), ARGUMENTS_EXPRESSION.findall(arguments)):
+            if isinstance(arg, dict):
+                kwargs.update(arg)
+            else:
+                args.append(arg)
 
     registry = request.registry
     extension = registry.queryMultiAdapter((context, request, view), ITALESExtension, name=name)
@@ -93,7 +104,7 @@
     # Insert the data gotten from the context
     addTALNamespaceData(extension, econtext)
 
-    return extension.render(*arg_value)
+    return extension.render(*args, **kwargs)
 
 
 class ExtensionExpr(ContextExprMixin, StringExpr):