comparison mercurial/templatefuncs.py @ 38265:41ae9b3cbfb9

templater: abstract min/max away I'm not certain how many get*() functions I'll add to the wrapped types, but getmin() and getmax() will allow us to optimize a revset wrapper.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 19 Mar 2018 00:16:12 +0900
parents 688fbb758ba9
children fb874fc1d9b4
comparison
equal deleted inserted replaced
38264:fbb2eddea4d2 38265:41ae9b3cbfb9
18 color, 18 color,
19 encoding, 19 encoding,
20 error, 20 error,
21 minirst, 21 minirst,
22 obsutil, 22 obsutil,
23 pycompat,
24 registrar, 23 registrar,
25 revset as revsetmod, 24 revset as revsetmod,
26 revsetlang, 25 revsetlang,
27 scmutil, 26 scmutil,
28 templatefilters, 27 templatefilters,
402 """Return the max of an iterable""" 401 """Return the max of an iterable"""
403 if len(args) != 1: 402 if len(args) != 1:
404 # i18n: "max" is a keyword 403 # i18n: "max" is a keyword
405 raise error.ParseError(_("max expects one argument")) 404 raise error.ParseError(_("max expects one argument"))
406 405
407 iterable = evalfuncarg(context, mapping, args[0]) 406 iterable = evalwrapped(context, mapping, args[0])
408 try: 407 try:
409 x = max(pycompat.maybebytestr(iterable)) 408 return iterable.getmax(context, mapping)
410 except (TypeError, ValueError): 409 except error.ParseError as err:
411 # i18n: "max" is a keyword 410 # i18n: "max" is a keyword
412 raise error.ParseError(_("max first argument should be an iterable")) 411 hint = _("max first argument should be an iterable")
413 return templateutil.wraphybridvalue(iterable, x, x) 412 raise error.ParseError(bytes(err), hint=hint)
414 413
415 @templatefunc('min(iterable)') 414 @templatefunc('min(iterable)')
416 def min_(context, mapping, args, **kwargs): 415 def min_(context, mapping, args, **kwargs):
417 """Return the min of an iterable""" 416 """Return the min of an iterable"""
418 if len(args) != 1: 417 if len(args) != 1:
419 # i18n: "min" is a keyword 418 # i18n: "min" is a keyword
420 raise error.ParseError(_("min expects one argument")) 419 raise error.ParseError(_("min expects one argument"))
421 420
422 iterable = evalfuncarg(context, mapping, args[0]) 421 iterable = evalwrapped(context, mapping, args[0])
423 try: 422 try:
424 x = min(pycompat.maybebytestr(iterable)) 423 return iterable.getmin(context, mapping)
425 except (TypeError, ValueError): 424 except error.ParseError as err:
426 # i18n: "min" is a keyword 425 # i18n: "min" is a keyword
427 raise error.ParseError(_("min first argument should be an iterable")) 426 hint = _("min first argument should be an iterable")
428 return templateutil.wraphybridvalue(iterable, x, x) 427 raise error.ParseError(bytes(err), hint=hint)
429 428
430 @templatefunc('mod(a, b)') 429 @templatefunc('mod(a, b)')
431 def mod(context, mapping, args): 430 def mod(context, mapping, args):
432 """Calculate a mod b such that a / b + a mod b == a""" 431 """Calculate a mod b such that a / b + a mod b == a"""
433 if not len(args) == 2: 432 if not len(args) == 2: