changeset 45288:8cce9f77ca73

templatespec: create a factory function for each type there is Most of the arguments to the `templatespec` constructor are mutually exclusive, so each combination creates a different type of templatespec. Let's clarify that by creating factory functions. I've left the callers in `logcmdutil` unchanged for now because they are more complex and `logcmdutil.templatespec()` is slightly higher level in that it is specific to changesets. My larger goal is to add support frozen binaries (specifically PyOxidizer) by adding a specific type of `templatespec` for built-in templates. That will get its own factory function. Differential Revision: https://phab.mercurial-scm.org/D8845
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 16 Jul 2020 13:33:46 -0700
parents b7444cfc2c05
children dfb67cd1da7f
files hgext/patchbomb.py mercurial/cmdutil.py mercurial/formatter.py
diffstat 3 files changed, 27 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/patchbomb.py	Wed Jul 29 10:42:09 2020 -0700
+++ b/hgext/patchbomb.py	Thu Jul 16 13:33:46 2020 -0700
@@ -207,7 +207,7 @@
     if not tmpl:
         return b' '.join(flags)
     out = util.stringio()
-    spec = formatter.templatespec(b'', templater.unquotestring(tmpl), None)
+    spec = formatter.literal_templatespec(templater.unquotestring(tmpl))
     with formatter.templateformatter(ui, out, b'patchbombflag', {}, spec) as fm:
         fm.startitem()
         fm.context(ctx=repo[rev])
--- a/mercurial/cmdutil.py	Wed Jul 29 10:42:09 2020 -0700
+++ b/mercurial/cmdutil.py	Thu Jul 16 13:33:46 2020 -0700
@@ -3375,7 +3375,7 @@
 
 def buildcommittemplate(repo, ctx, subs, extramsg, ref):
     ui = repo.ui
-    spec = formatter.templatespec(ref, None, None)
+    spec = formatter.reference_templatespec(ref)
     t = logcmdutil.changesettemplater(ui, repo, spec)
     t.t.cache.update(
         (k, templater.unquotestring(v))
--- a/mercurial/formatter.py	Wed Jul 29 10:42:09 2020 -0700
+++ b/mercurial/formatter.py	Thu Jul 16 13:33:46 2020 -0700
@@ -542,6 +542,22 @@
     refargs = attr.ib(default=None)
 
 
+def empty_templatespec():
+    return templatespec(None, None, None)
+
+
+def reference_templatespec(ref, refargs=None):
+    return templatespec(ref, None, None, refargs)
+
+
+def literal_templatespec(tmpl):
+    return templatespec(b'', tmpl, None)
+
+
+def mapfile_templatespec(topic, mapfile):
+    return templatespec(topic, None, mapfile)
+
+
 def lookuptemplate(ui, topic, tmpl):
     """Find the template matching the given -T/--template spec 'tmpl'
 
@@ -563,21 +579,21 @@
     """
 
     if not tmpl:
-        return templatespec(None, None, None)
+        return empty_templatespec()
 
     # looks like a literal template?
     if b'{' in tmpl:
-        return templatespec(b'', tmpl, None)
+        return literal_templatespec(tmpl)
 
     # a reference to built-in (formatter) template
     if tmpl in {b'cbor', b'json', b'pickle', b'debug'}:
-        return templatespec(tmpl, None, None)
+        return reference_templatespec(tmpl)
 
     # a function-style reference to built-in template
     func, fsep, ftail = tmpl.partition(b'(')
     if func in {b'cbor', b'json'} and fsep and ftail.endswith(b')'):
         templater.parseexpr(tmpl)  # make sure syntax errors are confined
-        return templatespec(func, None, None, refargs=ftail[:-1])
+        return reference_templatespec(func, refargs=ftail[:-1])
 
     # perhaps a stock style?
     if not os.path.split(tmpl)[0]:
@@ -585,11 +601,11 @@
             b'map-cmdline.' + tmpl
         ) or templater.templatepath(tmpl)
         if mapname:
-            return templatespec(topic, None, mapname)
+            return mapfile_templatespec(topic, mapname)
 
     # perhaps it's a reference to [templates]
     if ui.config(b'templates', tmpl):
-        return templatespec(tmpl, None, None)
+        return reference_templatespec(tmpl)
 
     if tmpl == b'list':
         ui.write(_(b"available styles: %s\n") % templater.stylelist())
@@ -599,13 +615,13 @@
     if (b'/' in tmpl or b'\\' in tmpl) and os.path.isfile(tmpl):
         # is it a mapfile for a style?
         if os.path.basename(tmpl).startswith(b"map-"):
-            return templatespec(topic, None, os.path.realpath(tmpl))
+            return mapfile_templatespec(topic, os.path.realpath(tmpl))
         with util.posixfile(tmpl, b'rb') as f:
             tmpl = f.read()
-        return templatespec(b'', tmpl, None)
+        return literal_templatespec(tmpl)
 
     # constant string?
-    return templatespec(b'', tmpl, None)
+    return literal_templatespec(tmpl)
 
 
 def templatepartsmap(spec, t, partnames):