templatekw: just pass underlying value (or key) to joinfmt() function
authorYuya Nishihara <yuya@tcha.org>
Sun, 24 Sep 2017 15:22:46 +0900
changeset 34328 dd28b1f55eb8
parent 34327 4647e0a8d3d7
child 34329 6367318327f0
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().
mercurial/templatekw.py
mercurial/templater.py
--- a/mercurial/templatekw.py	Sun Sep 24 12:43:57 2017 +0900
+++ b/mercurial/templatekw.py	Sun Sep 24 15:22:46 2017 +0900
@@ -48,10 +48,10 @@
         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 @@
 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 @@
     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 @@
     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 @@
 
     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 @@
             '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 @@
     # 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 @@
 
     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 @@
     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 @@
         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 @@
         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 @@
         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 @@
                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 @@
     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):
--- a/mercurial/templater.py	Sun Sep 24 12:43:57 2017 +0900
+++ b/mercurial/templater.py	Sun Sep 24 15:22:46 2017 +0900
@@ -768,10 +768,7 @@
     # 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 @@
             first = False
         else:
             yield joiner
-        yield x
+        yield joinfmt(x)
 
 @templatefunc('label(label, expr)')
 def label(context, mapping, args):