formatter: factor out function to create templater from literal or map file
authorYuya Nishihara <yuya@tcha.org>
Sat, 22 Apr 2017 15:06:06 +0900
changeset 32832 11e667a8fcba
parent 32831 de7dc25ecf2e
child 32833 99df35499cae
formatter: factor out function to create templater from literal or map file (tmpl, mapfile) will be packed into a named tuple later.
mercurial/cmdutil.py
mercurial/formatter.py
--- a/mercurial/cmdutil.py	Sat Apr 22 14:53:05 2017 +0900
+++ b/mercurial/cmdutil.py	Sat Apr 22 15:06:06 2017 +0900
@@ -1580,15 +1580,8 @@
 
     def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
         changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
-        assert not (tmpl and mapfile)
-        defaulttempl = templatekw.defaulttempl
-        if mapfile:
-            self.t = templater.templater.frommapfile(mapfile,
-                                                     cache=defaulttempl)
-        else:
-            self.t = formatter.maketemplater(ui, 'changeset', tmpl,
-                                             cache=defaulttempl)
-
+        self.t = formatter.loadtemplater(ui, 'changeset', (tmpl, mapfile),
+                                         cache=templatekw.defaulttempl)
         self._counter = itertools.count()
         self.cache = {}
 
--- a/mercurial/formatter.py	Sat Apr 22 14:53:05 2017 +0900
+++ b/mercurial/formatter.py	Sat Apr 22 15:06:06 2017 +0900
@@ -408,6 +408,12 @@
 
 def gettemplater(ui, topic, spec, cache=None):
     tmpl, mapfile = lookuptemplate(ui, topic, spec)
+    return loadtemplater(ui, topic, (tmpl, mapfile), cache=cache)
+
+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)