[PATCH 5 of 8] templatekw: just pass underlying value (or key) to joinfmt() function

Yuya Nishihara yuya at tcha.org
Sun Sep 24 08:21:54 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1506234166 -32400
#      Sun Sep 24 15:22:46 2017 +0900
# Node ID 669f28277c7b70f9f58d252bf4e57f84aff2268e
# Parent  5f8c602b3140a4b7b02c9de4fdbc01da5f07dc91
templatekw: just pass underlying value (or key) to joinfmt() function

Before, iter(hybrid) was proxied to hybrid.gen, which generated formatted
strings. That's why we had to apply joinfmt() to the dicts generated by
hybrid.itermaps(). Since this weird API was fixed at a0f2d83f8083, we can
get rid of the makemap() calls from join().

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -48,10 +48,10 @@ class _hybrid(object):
         return self._defaultgen()
     def _defaultgen(self):
         """Generator to stringify this as {join(self, ' ')}"""
-        for i, d in enumerate(self.itermaps()):
+        for i, x in enumerate(self._values):
             if i > 0:
                 yield ' '
-            yield self.joinfmt(d)
+            yield self.joinfmt(x)
     def itermaps(self):
         makemap = self._makemap
         for x in self._values:
@@ -73,11 +73,11 @@ class _hybrid(object):
 def hybriddict(data, key='key', value='value', fmt='%s=%s', gen=None):
     """Wrap data to support both dict-like and string-like operations"""
     return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
-                   lambda d: fmt % (d[key], d[value]))
+                   lambda k: fmt % (k, data[k]))
 
 def hybridlist(data, name, fmt='%s', gen=None):
     """Wrap data to support both list-like and string-like operations"""
-    return _hybrid(gen, data, lambda x: {name: x}, lambda d: fmt % d[name])
+    return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % x)
 
 def unwraphybrid(thing):
     """Return an object which can be stringified possibly by using a legacy
@@ -315,7 +315,7 @@ def showbookmarks(**args):
     active = repo._activebookmark
     makemap = lambda v: {'bookmark': v, 'active': active, 'current': active}
     f = _showlist('bookmark', bookmarks, args)
-    return _hybrid(f, bookmarks, makemap, lambda x: x['bookmark'])
+    return _hybrid(f, bookmarks, makemap, pycompat.identity)
 
 @templatekeyword('children')
 def showchildren(**args):
@@ -384,7 +384,7 @@ def showextras(**args):
     c = [makemap(k) for k in extras]
     f = _showlist('extra', c, args, plural='extras')
     return _hybrid(f, extras, makemap,
-                   lambda x: '%s=%s' % (x['key'], util.escapestr(x['value'])))
+                   lambda k: '%s=%s' % (k, util.escapestr(extras[k])))
 
 @templatekeyword('file_adds')
 def showfileadds(**args):
@@ -510,7 +510,7 @@ def showlatesttags(pattern, **args):
 
     tags = latesttags[2]
     f = _showlist('latesttag', tags, args, separator=':')
-    return _hybrid(f, tags, makemap, lambda x: x['latesttag'])
+    return _hybrid(f, tags, makemap, pycompat.identity)
 
 @templatekeyword('latesttagdistance')
 def showlatesttagdistance(repo, ctx, templ, cache, **args):
@@ -584,7 +584,7 @@ def shownamespaces(**args):
             'colorname': colornames[ns],
         }
 
-    return _hybrid(f, namespaces, makemap, lambda x: x['namespace'])
+    return _hybrid(f, namespaces, makemap, pycompat.identity)
 
 @templatekeyword('node')
 def shownode(repo, ctx, templ, **args):
@@ -618,7 +618,7 @@ def showpeerpaths(repo, **args):
     # no hybriddict() since d['path'] can't be formatted as a string. perhaps
     # hybriddict() should call templatefilters.stringify(d[value]).
     return _hybrid(None, paths, lambda k: {'name': k, 'path': paths[k]},
-                   lambda d: '%s=%s' % (d['name'], d['path']['url']))
+                   lambda k: '%s=%s' % (k, paths[k]['url']))
 
 @templatekeyword("predecessors")
 def showpredecessors(repo, ctx, **args):
@@ -629,7 +629,7 @@ def showpredecessors(repo, ctx, **args):
 
     return _hybrid(None, predecessors,
                    lambda x: {'ctx': repo[x], 'revcache': {}},
-                   lambda d: scmutil.formatchangeid(d['ctx']))
+                   lambda x: scmutil.formatchangeid(repo[x]))
 
 @templatekeyword("successorssets")
 def showsuccessorssets(repo, ctx, **args):
@@ -647,7 +647,7 @@ def showsuccessorssets(repo, ctx, **args
     data = []
     for ss in ssets:
         h = _hybrid(None, ss, lambda x: {'ctx': repo[x], 'revcache': {}},
-                    lambda d: scmutil.formatchangeid(d['ctx']))
+                    lambda x: scmutil.formatchangeid(repo[x]))
         data.append(h)
 
     # Format the successorssets
@@ -661,7 +661,7 @@ def showsuccessorssets(repo, ctx, **args
         yield "; ".join(render(d) for d in data)
 
     return _hybrid(gen(data), data, lambda x: {'successorset': x},
-                   lambda d: d["successorset"])
+                   pycompat.identity)
 
 @templatekeyword("succsandmarkers")
 def showsuccsandmarkers(repo, ctx, **args):
@@ -687,7 +687,7 @@ def showsuccsandmarkers(repo, ctx, **arg
         successors = [hex(n) for n in successors]
         successors = _hybrid(None, successors,
                              lambda x: {'ctx': repo[x], 'revcache': {}},
-                             lambda d: scmutil.formatchangeid(d['ctx']))
+                             lambda x: scmutil.formatchangeid(repo[x]))
 
         # Format markers
         finalmarkers = []
@@ -703,7 +703,7 @@ def showsuccsandmarkers(repo, ctx, **arg
         data.append({'successors': successors, 'markers': finalmarkers})
 
     f = _showlist('succsandmarkers', data, args)
-    return _hybrid(f, data, lambda x: x, lambda d: d)
+    return _hybrid(f, data, lambda x: x, pycompat.identity)
 
 @templatekeyword('p1rev')
 def showp1rev(repo, ctx, templ, **args):
@@ -748,7 +748,7 @@ def showparents(**args):
                for p in pctxs]
     f = _showlist('parent', parents, args)
     return _hybrid(f, prevs, lambda x: {'ctx': repo[int(x)], 'revcache': {}},
-                   lambda d: scmutil.formatchangeid(d['ctx']))
+                   lambda x: scmutil.formatchangeid(repo[int(x)]))
 
 @templatekeyword('phase')
 def showphase(repo, ctx, templ, **args):
@@ -775,7 +775,7 @@ def showrevslist(name, revs, **args):
     f = _showlist(name, revs, args)
     return _hybrid(f, revs,
                    lambda x: {name: x, 'ctx': repo[int(x)], 'revcache': {}},
-                   lambda d: d[name])
+                   pycompat.identity)
 
 @templatekeyword('subrepos')
 def showsubrepos(**args):
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -768,10 +768,7 @@ def join(context, mapping, args):
     # TODO: perhaps this should be evalfuncarg(), but it can't because hgweb
     # abuses generator as a keyword that returns a list of dicts.
     joinset = evalrawexp(context, mapping, args[0])
-    if util.safehasattr(joinset, 'itermaps'):
-        jf = joinset.joinfmt
-        joinset = [jf(x) for x in joinset.itermaps()]
-
+    joinfmt = getattr(joinset, 'joinfmt', pycompat.identity)
     joiner = " "
     if len(args) > 1:
         joiner = evalstring(context, mapping, args[1])
@@ -782,7 +779,7 @@ def join(context, mapping, args):
             first = False
         else:
             yield joiner
-        yield x
+        yield joinfmt(x)
 
 @templatefunc('label(label, expr)')
 def label(context, mapping, args):


More information about the Mercurial-devel mailing list