Patchwork [2,of,6] formatter: wrap (tmpl, mapfile) by named tuple

login
register
mail settings
Submitter Yuya Nishihara
Date June 15, 2017, 2:52 p.m.
Message ID <019d918605ade79f4982.1497538348@mimosa>
Download mbox | patch
Permalink /patch/21389/
State Accepted
Headers show

Comments

Yuya Nishihara - June 15, 2017, 2:52 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1492854518 -32400
#      Sat Apr 22 18:48:38 2017 +0900
# Node ID 019d918605ade79f498235167af488130c9f74e9
# Parent  06aa02920b896e50e6fc18709bd42522d32e68bd
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.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1580,7 +1580,8 @@  class changeset_templater(changeset_prin
 
     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 @@  class changeset_templater(changeset_prin
                 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 @@  def _lookuplogtemplate(ui, tmpl, style):
     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 @@  def _lookuplogtemplate(ui, tmpl, style):
                        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)
 
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -103,6 +103,7 @@  baz: foo, bar
 
 from __future__ import absolute_import
 
+import collections
 import contextlib
 import itertools
 import os
@@ -373,6 +374,9 @@  class templateformatter(baseformatter):
         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 @@  def lookuptemplate(ui, topic, tmpl):
 
     # 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 @@  def lookuptemplate(ui, topic, tmpl):
     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'"""