changeset 32838:615ec3f14aa9

formatter: wrap (tmpl, mapfile) by named tuple I'm going to add more options to the templatespec tuple. cmdutil.logtemplatespec() is just an alias now, but it will be changed to a factory function later.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 18:48:38 +0900
parents 50586a0a946f
children b425ec7fb7f6
files mercurial/cmdutil.py mercurial/formatter.py
diffstat 2 files changed, 21 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Sat Apr 22 18:42:03 2017 +0900
+++ b/mercurial/cmdutil.py	Sat Apr 22 18:48:38 2017 +0900
@@ -1580,7 +1580,8 @@
 
     def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
         changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
-        self.t = formatter.loadtemplater(ui, 'changeset', (tmpl, mapfile),
+        tmplspec = logtemplatespec(tmpl, mapfile)
+        self.t = formatter.loadtemplater(ui, 'changeset', tmplspec,
                                          cache=templatekw.defaulttempl)
         self._counter = itertools.count()
         self.cache = {}
@@ -1646,6 +1647,8 @@
                 self.footer = templater.stringify(
                     self.t(self._parts['footer'], **props))
 
+logtemplatespec = formatter.templatespec
+
 def _lookuplogtemplate(ui, tmpl, style):
     """Find the template matching the given template spec or style
 
@@ -1656,7 +1659,7 @@
     if not tmpl and not style: # template are stronger than style
         tmpl = ui.config('ui', 'logtemplate')
         if tmpl:
-            return templater.unquotestring(tmpl), None
+            return logtemplatespec(templater.unquotestring(tmpl), None)
         else:
             style = util.expandpath(ui.config('ui', 'style', ''))
 
@@ -1667,10 +1670,10 @@
                        or templater.templatepath(mapfile))
             if mapname:
                 mapfile = mapname
-        return None, mapfile
+        return logtemplatespec(None, mapfile)
 
     if not tmpl:
-        return None, None
+        return logtemplatespec(None, None)
 
     return formatter.lookuptemplate(ui, 'changeset', tmpl)
 
--- a/mercurial/formatter.py	Sat Apr 22 18:42:03 2017 +0900
+++ b/mercurial/formatter.py	Sat Apr 22 18:48:38 2017 +0900
@@ -103,6 +103,7 @@
 
 from __future__ import absolute_import
 
+import collections
 import contextlib
 import itertools
 import os
@@ -373,6 +374,9 @@
         g = self._t(self._topic, ui=self._ui, cache=self._cache, **props)
         self._out.write(templater.stringify(g))
 
+templatespec = collections.namedtuple(r'templatespec',
+                                      r'tmpl mapfile')
+
 def lookuptemplate(ui, topic, tmpl):
     """Find the template matching the given -T/--template spec 'tmpl'
 
@@ -391,19 +395,19 @@
 
     # looks like a literal template?
     if '{' in tmpl:
-        return tmpl, None
+        return templatespec(tmpl, None)
 
     # perhaps a stock style?
     if not os.path.split(tmpl)[0]:
         mapname = (templater.templatepath('map-cmdline.' + tmpl)
                    or templater.templatepath(tmpl))
         if mapname and os.path.isfile(mapname):
-            return None, mapname
+            return templatespec(None, mapname)
 
     # perhaps it's a reference to [templates]
     t = ui.config('templates', tmpl)
     if t:
-        return templater.unquotestring(t), None
+        return templatespec(templater.unquotestring(t), None)
 
     if tmpl == 'list':
         ui.write(_("available styles: %s\n") % templater.stylelist())
@@ -413,22 +417,21 @@
     if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl):
         # is it a mapfile for a style?
         if os.path.basename(tmpl).startswith("map-"):
-            return None, os.path.realpath(tmpl)
+            return templatespec(None, os.path.realpath(tmpl))
         with util.posixfile(tmpl, 'rb') as f:
             tmpl = f.read()
-        return tmpl, None
+        return templatespec(tmpl, None)
 
     # constant string?
-    return tmpl, None
+    return templatespec(tmpl, None)
 
 def loadtemplater(ui, topic, spec, cache=None):
     """Create a templater from either a literal template or loading from
     a map file"""
-    tmpl, mapfile = spec
-    assert not (tmpl and mapfile)
-    if mapfile:
-        return templater.templater.frommapfile(mapfile, cache=cache)
-    return maketemplater(ui, topic, tmpl, cache=cache)
+    assert not (spec.tmpl and spec.mapfile)
+    if spec.mapfile:
+        return templater.templater.frommapfile(spec.mapfile, cache=cache)
+    return maketemplater(ui, topic, spec.tmpl, cache=cache)
 
 def maketemplater(ui, topic, tmpl, cache=None):
     """Create a templater from a string template 'tmpl'"""