comparison mercurial/templater.py @ 31922:0f41f1e3c75c

parser: preserve order of keyword arguments This helps building dict(key1=value1, ...) in deterministic way.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 09 Apr 2017 11:58:27 +0900
parents 2156934b7917
children 932241b8c644
comparison
equal deleted inserted replaced
31921:2156934b7917 31922:0f41f1e3c75c
472 >>> context = engine(lambda t: (runsymbol, t)) 472 >>> context = engine(lambda t: (runsymbol, t))
473 >>> def fargs(expr, argspec): 473 >>> def fargs(expr, argspec):
474 ... x = _parseexpr(expr) 474 ... x = _parseexpr(expr)
475 ... n = getsymbol(x[1]) 475 ... n = getsymbol(x[1])
476 ... return _buildfuncargs(x[2], context, exprmethods, n, argspec) 476 ... return _buildfuncargs(x[2], context, exprmethods, n, argspec)
477 >>> sorted(fargs('a(l=1, k=2)', 'k l m').keys()) 477 >>> fargs('a(l=1, k=2)', 'k l m').keys()
478 ['k', 'l'] 478 ['l', 'k']
479 >>> args = fargs('a(opts=1, k=2)', '**opts') 479 >>> args = fargs('a(opts=1, k=2)', '**opts')
480 >>> args.keys(), sorted(args['opts'].keys()) 480 >>> args.keys(), args['opts'].keys()
481 (['opts'], ['k', 'opts']) 481 (['opts'], ['opts', 'k'])
482 """ 482 """
483 def compiledict(xs): 483 def compiledict(xs):
484 return dict((k, compileexp(x, context, curmethods)) 484 return util.sortdict((k, compileexp(x, context, curmethods))
485 for k, x in xs.iteritems()) 485 for k, x in xs.iteritems())
486 def compilelist(xs): 486 def compilelist(xs):
487 return [compileexp(x, context, curmethods) for x in xs] 487 return [compileexp(x, context, curmethods) for x in xs]
488 488
489 if not argspec: 489 if not argspec:
490 # filter or function with no argspec: return list of positional args 490 # filter or function with no argspec: return list of positional args
492 492
493 # function with argspec: return dict of named args 493 # function with argspec: return dict of named args
494 _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec) 494 _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec)
495 treeargs = parser.buildargsdict(getlist(exp), funcname, argspec, 495 treeargs = parser.buildargsdict(getlist(exp), funcname, argspec,
496 keyvaluenode='keyvalue', keynode='symbol') 496 keyvaluenode='keyvalue', keynode='symbol')
497 compargs = {} 497 compargs = util.sortdict()
498 if varkey: 498 if varkey:
499 compargs[varkey] = compilelist(treeargs.pop(varkey)) 499 compargs[varkey] = compilelist(treeargs.pop(varkey))
500 if optkey: 500 if optkey:
501 compargs[optkey] = compiledict(treeargs.pop(optkey)) 501 compargs[optkey] = compiledict(treeargs.pop(optkey))
502 compargs.update(compiledict(treeargs)) 502 compargs.update(compiledict(treeargs))