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

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 27, 2018, 2:58 p.m.
Message ID <e6b41f06c7bbcbc74581.1519743481@mimosa>
Download mbox | patch
Permalink /patch/28465/
State New
Headers show

Comments

Yuya Nishihara - Feb. 27, 2018, 2:58 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

Patch

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)
 
-@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):