[PATCH 1 of 5] formatter: move most of template option helper to formatter

Matt Mackall mpm at selenic.com
Wed Jun 10 23:09:45 UTC 2015


# HG changeset patch
# User Matt Mackall <mpm at selenic.com>
# Date 1433964553 18000
#      Wed Jun 10 14:29:13 2015 -0500
# Node ID e8e260b75f472714c2b9526f1148176e3dc1e1bb
# Parent  19717d3c8f941fdd92725ef1723f39edd981e543
formatter: move most of template option helper to formatter

We want to share this function between formatter and cmdutils. It
doesn't belong in templater because it imports knowledge of ui layers
that shouldn't be there. We'd prefer cmdutil to layer on the formatter
rather than vice-versa. Since the formatter is the handler for -T
options for all non-log commands, let's move the helper there. We
leave the bits specific to the old --style option behind.

diff -r 19717d3c8f94 -r e8e260b75f47 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Sun Jun 07 15:49:57 2015 -0700
+++ b/mercurial/cmdutil.py	Wed Jun 10 14:29:13 2015 -0500
@@ -14,6 +14,7 @@
 import changelog
 import bookmarks
 import encoding
+import formatter
 import crecord as crecordmod
 import lock as lockmod
 
@@ -1493,40 +1494,7 @@
     if not tmpl:
         return None, None
 
-    # looks like a literal template?
-    if '{' in tmpl:
-        return 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
-
-    # perhaps it's a reference to [templates]
-    t = ui.config('templates', tmpl)
-    if t:
-        try:
-            tmpl = templater.unquotestring(t)
-        except SyntaxError:
-            tmpl = t
-        return tmpl, None
-
-    if tmpl == 'list':
-        ui.write(_("available styles: %s\n") % templater.stylelist())
-        raise util.Abort(_("specify a template"))
-
-    # perhaps it's a path to a map or a template
-    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)
-        tmpl = open(tmpl).read()
-        return tmpl, None
-
-    # constant string?
-    return tmpl, None
+    return formatter.lookuptemplate(ui, 'changeset', tmpl)
 
 def show_changeset(ui, repo, opts, buffered=False):
     """show one changeset using template or regular display.
diff -r 19717d3c8f94 -r e8e260b75f47 mercurial/formatter.py
--- a/mercurial/formatter.py	Sun Jun 07 15:49:57 2015 -0700
+++ b/mercurial/formatter.py	Wed Jun 10 14:29:13 2015 -0500
@@ -9,6 +9,8 @@
 from node import hex, short
 from i18n import _
 import encoding, util
+import templater
+import os
 
 class baseformatter(object):
     def __init__(self, ui, topic, opts):
@@ -133,6 +135,42 @@
         baseformatter.end(self)
         self._ui.write("\n]\n")
 
+def lookuptemplate(ui, topic, tmpl):
+    # looks like a literal template?
+    if '{' in tmpl:
+        return 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
+
+    # perhaps it's a reference to [templates]
+    t = ui.config('templates', tmpl)
+    if t:
+        try:
+            tmpl = templater.unquotestring(t)
+        except SyntaxError:
+            tmpl = t
+        return tmpl, None
+
+    if tmpl == 'list':
+        ui.write(_("available styles: %s\n") % templater.stylelist())
+        raise util.Abort(_("specify a template"))
+
+    # perhaps it's a path to a map or a template
+    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)
+        tmpl = open(tmpl).read()
+        return tmpl, None
+
+    # constant string?
+    return tmpl, None
+
 def formatter(ui, topic, opts):
     template = opts.get("template", "")
     if template == "json":


More information about the Mercurial-devel mailing list