[PATCH 8 of 9] templatekw: add compatdict() as a replacement for showdict()

Yuya Nishihara yuya at tcha.org
Tue Feb 27 09:58:01 EST 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1519542199 -32400
#      Sun Feb 25 16:03:19 2018 +0900
# Node ID e6b41f06c7bbcbc745814ec066c7f660d63d200f
# Parent  c0c1bdb2ae8781ea4b43dfa1019d437652956a41
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.

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -136,6 +136,18 @@ def wraphybridvalue(container, key, valu
         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 @@ def showdiffstat(context, mapping):
     maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
     return '%s: +%s/-%s' % (len(stats), adds, removes)
 
- at templatekeyword('envvars')
-def showenvvars(ui, **args):
+ at 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):


More information about the Mercurial-devel mailing list