diff mercurial/templater.py @ 31921:2156934b7917

parser: extend buildargsdict() to support arbitrary number of **kwargs Prepares for adding dict(key1=value1, ...) template function. More tests will be added later.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 03 Apr 2017 22:07:09 +0900
parents f7b3677f66cd
children 0f41f1e3c75c
line wrap: on
line diff
--- a/mercurial/templater.py	Sat Apr 08 20:07:37 2017 +0900
+++ b/mercurial/templater.py	Mon Apr 03 22:07:09 2017 +0900
@@ -467,7 +467,19 @@
 
 def _buildfuncargs(exp, context, curmethods, funcname, argspec):
     """Compile parsed tree of function arguments into list or dict of
-    (func, data) pairs"""
+    (func, data) pairs
+
+    >>> context = engine(lambda t: (runsymbol, t))
+    >>> def fargs(expr, argspec):
+    ...     x = _parseexpr(expr)
+    ...     n = getsymbol(x[1])
+    ...     return _buildfuncargs(x[2], context, exprmethods, n, argspec)
+    >>> sorted(fargs('a(l=1, k=2)', 'k l m').keys())
+    ['k', 'l']
+    >>> args = fargs('a(opts=1, k=2)', '**opts')
+    >>> args.keys(), sorted(args['opts'].keys())
+    (['opts'], ['k', 'opts'])
+    """
     def compiledict(xs):
         return dict((k, compileexp(x, context, curmethods))
                     for k, x in xs.iteritems())
@@ -479,12 +491,14 @@
         return compilelist(getlist(exp))
 
     # function with argspec: return dict of named args
-    _poskeys, varkey, _keys = argspec = parser.splitargspec(argspec)
+    _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec)
     treeargs = parser.buildargsdict(getlist(exp), funcname, argspec,
                                     keyvaluenode='keyvalue', keynode='symbol')
     compargs = {}
     if varkey:
         compargs[varkey] = compilelist(treeargs.pop(varkey))
+    if optkey:
+        compargs[optkey] = compiledict(treeargs.pop(optkey))
     compargs.update(compiledict(treeargs))
     return compargs