templatekw: deprecate old-style template keyword function (API)
authorYuya Nishihara <yuya@tcha.org>
Sun, 25 Feb 2018 21:04:33 +0900
changeset 38930 382b055cc358
parent 38929 d7e6e109eaae
child 38932 205efbf656c2
templatekw: deprecate old-style template keyword function (API) .. api:: `f(**kwargs)` style template keyword function is deprecated. Switch to new `(context, mapping)` API by declaring resource requirements. The new-style API will be the default in Mercurial 4.9. See registrar.templatekeyword for details.
mercurial/registrar.py
mercurial/templateutil.py
--- a/mercurial/registrar.py	Sat Jul 28 21:19:24 2018 +0900
+++ b/mercurial/registrar.py	Sun Feb 25 21:04:33 2018 +0900
@@ -295,7 +295,7 @@
             '''
             pass
 
-        # old API
+        # old API (DEPRECATED)
         @templatekeyword('mykeyword')
         def mykeywordfunc(repo, ctx, templ, cache, revcache, **args):
             '''Explanation of this template keyword ....
--- a/mercurial/templateutil.py	Sat Jul 28 21:19:24 2018 +0900
+++ b/mercurial/templateutil.py	Sun Feb 25 21:04:33 2018 +0900
@@ -810,8 +810,9 @@
     return data
 
 def _recursivesymbolblocker(key):
-    def showrecursion(**args):
+    def showrecursion(context, mapping):
         raise error.Abort(_("recursive reference '%s' in template") % key)
+    showrecursion._requires = ()  # mark as new-style templatekw
     return showrecursion
 
 def runsymbol(context, mapping, key, default=''):
@@ -827,12 +828,16 @@
             v = default
     if callable(v) and getattr(v, '_requires', None) is None:
         # old templatekw: expand all keywords and resources
-        # (TODO: deprecate this after porting web template keywords to new API)
+        # (TODO: drop support for old-style functions. 'f._requires = ()'
+        #  can be removed.)
         props = {k: context._resources.lookup(context, mapping, k)
                  for k in context._resources.knownkeys()}
         # pass context to _showcompatlist() through templatekw._showlist()
         props['templ'] = context
         props.update(mapping)
+        ui = props.get('ui')
+        if ui:
+            ui.deprecwarn("old-style template keyword '%s'" % key, '4.8')
         return v(**pycompat.strkwargs(props))
     if callable(v):
         # new templatekw