templater: register keywords to defaults table
authorYuya Nishihara <yuya@tcha.org>
Fri, 22 Dec 2017 21:19:29 +0900
changeset 35483 817a3d20dd01
parent 35482 c240657febb7
child 35484 1853c8677160
templater: register keywords to defaults table Since the keywords are permanent, there should be no need to pass them by a temporary mapping.
mercurial/cmdutil.py
mercurial/filemerge.py
mercurial/formatter.py
tests/test-template-engine.t
--- a/mercurial/cmdutil.py	Fri Dec 22 21:22:49 2017 +0900
+++ b/mercurial/cmdutil.py	Fri Dec 22 21:19:29 2017 +0900
@@ -1844,7 +1844,9 @@
 
         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 @@
     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 @@
     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
 
--- a/mercurial/filemerge.py	Fri Dec 22 21:22:49 2017 +0900
+++ b/mercurial/filemerge.py	Fri Dec 22 21:19:29 2017 +0900
@@ -552,8 +552,7 @@
     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 @@
     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)
 
--- a/mercurial/formatter.py	Fri Dec 22 21:22:49 2017 +0900
+++ b/mercurial/formatter.py	Fri Dec 22 21:19:29 2017 +0900
@@ -363,7 +363,8 @@
         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 @@
         # 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 @@
                 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:
--- a/tests/test-template-engine.t	Fri Dec 22 21:22:49 2017 +0900
+++ b/tests/test-template-engine.t	Fri Dec 22 21:19:29 2017 +0900
@@ -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__'):