Mercurial > hg
comparison mercurial/templater.py @ 34534:b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
See the test for usage example.
wraphybridvalue() takes a key/value pair because a hybrid dict passes a key
to its makemap() function. Since makemap() of showmanifest() doesn't need
a key, it's set to None.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 09 Sep 2017 19:13:25 +0900 |
parents | a1b89c8ad32d |
children | 78590585c0db |
comparison
equal
deleted
inserted
replaced
34533:163fa0aea71e | 34534:b3073e175c17 |
---|---|
728 if not util.safehasattr(dictarg, 'get'): | 728 if not util.safehasattr(dictarg, 'get'): |
729 # i18n: "get" is a keyword | 729 # i18n: "get" is a keyword |
730 raise error.ParseError(_("get() expects a dict as first argument")) | 730 raise error.ParseError(_("get() expects a dict as first argument")) |
731 | 731 |
732 key = evalfuncarg(context, mapping, args[1]) | 732 key = evalfuncarg(context, mapping, args[1]) |
733 return dictarg.get(key) | 733 val = dictarg.get(key) |
734 if val is None: | |
735 return | |
736 return templatekw.wraphybridvalue(dictarg, key, val) | |
734 | 737 |
735 @templatefunc('if(expr, then[, else])') | 738 @templatefunc('if(expr, then[, else])') |
736 def if_(context, mapping, args): | 739 def if_(context, mapping, args): |
737 """Conditionally execute based on the result of | 740 """Conditionally execute based on the result of |
738 an expression.""" | 741 an expression.""" |
872 # i18n: "max" is a keyword | 875 # i18n: "max" is a keyword |
873 raise error.ParseError(_("max expects one arguments")) | 876 raise error.ParseError(_("max expects one arguments")) |
874 | 877 |
875 iterable = evalfuncarg(context, mapping, args[0]) | 878 iterable = evalfuncarg(context, mapping, args[0]) |
876 try: | 879 try: |
877 return max(iterable) | 880 x = max(iterable) |
878 except (TypeError, ValueError): | 881 except (TypeError, ValueError): |
879 # i18n: "max" is a keyword | 882 # i18n: "max" is a keyword |
880 raise error.ParseError(_("max first argument should be an iterable")) | 883 raise error.ParseError(_("max first argument should be an iterable")) |
884 return templatekw.wraphybridvalue(iterable, x, x) | |
881 | 885 |
882 @templatefunc('min(iterable)') | 886 @templatefunc('min(iterable)') |
883 def min_(context, mapping, args, **kwargs): | 887 def min_(context, mapping, args, **kwargs): |
884 """Return the min of an iterable""" | 888 """Return the min of an iterable""" |
885 if len(args) != 1: | 889 if len(args) != 1: |
886 # i18n: "min" is a keyword | 890 # i18n: "min" is a keyword |
887 raise error.ParseError(_("min expects one arguments")) | 891 raise error.ParseError(_("min expects one arguments")) |
888 | 892 |
889 iterable = evalfuncarg(context, mapping, args[0]) | 893 iterable = evalfuncarg(context, mapping, args[0]) |
890 try: | 894 try: |
891 return min(iterable) | 895 x = min(iterable) |
892 except (TypeError, ValueError): | 896 except (TypeError, ValueError): |
893 # i18n: "min" is a keyword | 897 # i18n: "min" is a keyword |
894 raise error.ParseError(_("min first argument should be an iterable")) | 898 raise error.ParseError(_("min first argument should be an iterable")) |
899 return templatekw.wraphybridvalue(iterable, x, x) | |
895 | 900 |
896 @templatefunc('mod(a, b)') | 901 @templatefunc('mod(a, b)') |
897 def mod(context, mapping, args): | 902 def mod(context, mapping, args): |
898 """Calculate a mod b such that a / b + a mod b == a""" | 903 """Calculate a mod b such that a / b + a mod b == a""" |
899 if not len(args) == 2: | 904 if not len(args) == 2: |