Patchwork [3,of,4] templater: register keywords to defaults table

login
register
mail settings
Submitter Yuya Nishihara
Date Dec. 22, 2017, 2:08 p.m.
Message ID <d8f9134603553151fd06.1513951709@mimosa>
Download mbox | patch
Permalink /patch/26398/
State Accepted
Headers show

Comments

Yuya Nishihara - Dec. 22, 2017, 2:08 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1513945169 -32400
#      Fri Dec 22 21:19:29 2017 +0900
# Node ID d8f9134603553151fd0669d7235729f09188fc53
# Parent  a9a8f3642ca47198d44e65781cb328131137d69f
templater: register keywords to defaults table

Since the keywords are permanent, there should be no need to pass them
by a temporary mapping.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1844,7 +1844,9 @@  class changeset_templater(changeset_prin
 
         changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
         tres = formatter.templateresources(ui, repo)
-        self.t = formatter.loadtemplater(ui, tmplspec, resources=tres,
+        self.t = formatter.loadtemplater(ui, tmplspec,
+                                         defaults=templatekw.keywords,
+                                         resources=tres,
                                          cache=templatekw.defaulttempl)
         self._counter = itertools.count()
         self.cache = tres['cache']  # shared with _graphnodeformatter()
@@ -1886,7 +1888,6 @@  class changeset_templater(changeset_prin
     def _show(self, ctx, copies, matchfn, hunksfilterfn, props):
         '''show a single changeset or file revision'''
         props = props.copy()
-        props.update(templatekw.keywords)
         props['ctx'] = ctx
         props['index'] = index = next(self._counter)
         props['revcache'] = {'copies': copies}
@@ -2658,12 +2659,10 @@  def _graphnodeformatter(ui, displayer):
     tres = formatter.templateresources(ui)
     if isinstance(displayer, changeset_templater):
         tres['cache'] = displayer.cache  # reuse cache of slow templates
-    templ = formatter.maketemplater(ui, spec, resources=tres)
-    props = templatekw.keywords.copy()
+    templ = formatter.maketemplater(ui, spec, defaults=templatekw.keywords,
+                                    resources=tres)
     def formatnode(repo, ctx):
-        props['ctx'] = ctx
-        props['repo'] = repo
-        props['revcache'] = {}
+        props = {'ctx': ctx, 'repo': repo, 'revcache': {}}
         return templ.render(props)
     return formatnode
 
diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -552,8 +552,7 @@  def _formatconflictmarker(ctx, template,
     if ctx.node() is None:
         ctx = ctx.p1()
 
-    props = templatekw.keywords.copy()
-    props['ctx'] = ctx
+    props = {'ctx': ctx}
     templateresult = template.render(props)
 
     label = ('%s:' % label).ljust(pad + 1)
@@ -580,7 +579,8 @@  def _formatlabels(repo, fcd, fco, fca, l
     template = ui.config('ui', 'mergemarkertemplate')
     template = templater.unquotestring(template)
     tres = formatter.templateresources(ui, repo)
-    tmpl = formatter.maketemplater(ui, template, resources=tres)
+    tmpl = formatter.maketemplater(ui, template, defaults=templatekw.keywords,
+                                   resources=tres)
 
     pad = max(len(l) for l in labels)
 
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -363,7 +363,8 @@  class templateformatter(baseformatter):
         self._out = out
         spec = lookuptemplate(ui, topic, opts.get('template', ''))
         self._tref = spec.ref
-        self._t = loadtemplater(ui, spec, resources=templateresources(ui),
+        self._t = loadtemplater(ui, spec, defaults=templatekw.keywords,
+                                resources=templateresources(ui),
                                 cache=templatekw.defaulttempl)
         self._parts = templatepartsmap(spec, self._t,
                                        ['docheader', 'docfooter', 'separator'])
@@ -386,8 +387,6 @@  class templateformatter(baseformatter):
         # function will have to declare dependent resources. e.g.
         # @templatekeyword(..., requires=('ctx',))
         props = {}
-        if 'ctx' in item:
-            props.update(templatekw.keywords)
         # explicitly-defined fields precede templatekw
         props.update(item)
         if 'ctx' in item:
@@ -467,19 +466,22 @@  def templatepartsmap(spec, t, partnames)
                 partsmap[part] = ref
     return partsmap
 
-def loadtemplater(ui, spec, resources=None, cache=None):
+def loadtemplater(ui, spec, defaults=None, resources=None, cache=None):
     """Create a templater from either a literal template or loading from
     a map file"""
     assert not (spec.tmpl and spec.mapfile)
     if spec.mapfile:
         frommapfile = templater.templater.frommapfile
-        return frommapfile(spec.mapfile, resources=resources, cache=cache)
-    return maketemplater(ui, spec.tmpl, resources=resources, cache=cache)
+        return frommapfile(spec.mapfile, defaults=defaults, resources=resources,
+                           cache=cache)
+    return maketemplater(ui, spec.tmpl, defaults=defaults, resources=resources,
+                         cache=cache)
 
-def maketemplater(ui, tmpl, resources=None, cache=None):
+def maketemplater(ui, tmpl, defaults=None, resources=None, cache=None):
     """Create a templater from a string template 'tmpl'"""
     aliases = ui.configitems('templatealias')
-    t = templater.templater(resources=resources, cache=cache, aliases=aliases)
+    t = templater.templater(defaults=defaults, resources=resources,
+                            cache=cache, aliases=aliases)
     t.cache.update((k, templater.unquotestring(v))
                    for k, v in ui.configitems('templates'))
     if tmpl:
diff --git a/tests/test-template-engine.t b/tests/test-template-engine.t
--- a/tests/test-template-engine.t
+++ b/tests/test-template-engine.t
@@ -6,11 +6,14 @@ 
   > class mytemplater(object):
   >     def __init__(self, loader, filters, defaults, resources, aliases):
   >         self.loader = loader
+  >         self._defaults = defaults
   >         self._resources = resources
   > 
   >     def process(self, t, map):
   >         tmpl = self.loader(t)
-  >         for k, v in map.iteritems():
+  >         props = self._defaults.copy()
+  >         props.update(map)
+  >         for k, v in props.iteritems():
   >             if k in ('templ', 'ctx', 'repo', 'revcache', 'cache', 'troubles'):
   >                 continue
   >             if hasattr(v, '__call__'):