templatekw: add compatdict() as a replacement for showdict()
authorYuya Nishihara <yuya@tcha.org>
Sun, 25 Feb 2018 16:03:19 +0900
changeset 36520 a7fbe11a5d59
parent 36519 94c4ae452293
child 36521 c3692364b344
templatekw: add compatdict() as a replacement for showdict() This is mostly a copy of showdict(), which will be deprecated later. See the docstring for why it's called a "compat" dict. showenvvars() is ported to the new API as an example.
mercurial/templatekw.py
--- a/mercurial/templatekw.py	Sun Feb 25 15:43:35 2018 +0900
+++ b/mercurial/templatekw.py	Sun Feb 25 16:03:19 2018 +0900
@@ -136,6 +136,18 @@
         return value
     return _mappable(None, key, value, makemap)
 
+def compatdict(context, mapping, name, data, key='key', value='value',
+               fmt='%s=%s', plural=None, separator=' '):
+    """Wrap data like hybriddict(), but also supports old-style list template
+
+    This exists for backward compatibility with the old-style template. Use
+    hybriddict() for new template keywords.
+    """
+    c = [{key: k, value: v} for k, v in data.iteritems()]
+    t = context.resource(mapping, 'templ')
+    f = _showlist(name, c, t, mapping, plural, separator)
+    return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
+
 def showdict(name, data, mapping, plural=None, key='key', value='value',
              fmt='%s=%s', separator=' '):
     c = [{key: k, value: v} for k, v in data.iteritems()]
@@ -437,13 +449,13 @@
     maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
     return '%d: +%d/-%d' % (len(stats), adds, removes)
 
-@templatekeyword('envvars')
-def showenvvars(ui, **args):
+@templatekeyword('envvars', requires={'ui', 'templ'})
+def showenvvars(context, mapping):
     """A dictionary of environment variables. (EXPERIMENTAL)"""
-    args = pycompat.byteskwargs(args)
+    ui = context.resource(mapping, 'ui')
     env = ui.exportableenviron()
     env = util.sortdict((k, env[k]) for k in sorted(env))
-    return showdict('envvar', env, args, plural='envvars')
+    return compatdict(context, mapping, 'envvar', env, plural='envvars')
 
 @templatekeyword('extras')
 def showextras(**args):