topic: rework topic templatekw declaration to be 4.5 compatible again stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 03 Sep 2018 22:47:18 +0200
branchstable
changeset 4078 da6ce6d446b9
parent 4073 d54a4b9c1d01
child 4079 9aab5345728a
child 4083 8d8f08245f97
topic: rework topic templatekw declaration to be 4.5 compatible again Mercurial prior to 4.5 does not have a requires keyword, so we need a compatibility layer.
hgext3rd/topic/__init__.py
--- a/hgext3rd/topic/__init__.py	Mon Sep 03 21:08:33 2018 +0200
+++ b/hgext3rd/topic/__init__.py	Mon Sep 03 22:47:18 2018 +0200
@@ -134,6 +134,7 @@
     registrar,
     scmutil,
     templatefilters,
+    templatekw,
     util,
 )
 
@@ -227,7 +228,9 @@
                       default=None,
             )
 
+# we need to do old style declaration for <= 4.5
 templatekeyword = registrar.templatekeyword()
+post45template = 'requires=' in templatekeyword.__doc__
 
 def _contexttopic(self, force=False):
     if not (force or self.mutable()):
@@ -350,6 +353,8 @@
 
     cmdutil.summaryhooks.add('topic', summaryhook)
 
+    if not post45template:
+        templatekw.keywords['topic'] = topickw
     # Wrap workingctx extra to return the topic name
     extensions.wrapfunction(context.workingctx, '__init__', wrapinit)
     # Wrap changelog.add to drop empty topic
@@ -521,11 +526,16 @@
             'topics', 'topic', namemap=_namemap, nodemap=_nodemap,
             listnames=lambda repo: repo.topics))
 
-@templatekeyword('topic', requires={'ctx'})
-def topickw(context, mapping):
-    """:topic: String. The topic of the changeset"""
-    ctx = context.resource(mapping, 'ctx')
-    return ctx.topic()
+if post45template:
+    @templatekeyword('topic', requires={'ctx'})
+    def topickw(context, mapping):
+        """:topic: String. The topic of the changeset"""
+        ctx = context.resource(mapping, 'ctx')
+        return ctx.topic()
+else:
+    def topickw(**args):
+        """:topic: String. The topic of the changeset"""
+        return args['ctx'].topic()
 
 def wrapinit(orig, self, repo, *args, **kwargs):
     orig(self, repo, *args, **kwargs)