obsolete: add and use templatekw decorator
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 07 Aug 2012 14:08:20 +0200
changeset 431 bd05022c7856
parent 430 07db1d511faf
child 432 6347bc69b49a
obsolete: add and use templatekw decorator
hgext/obsolete.py
--- a/hgext/obsolete.py	Tue Aug 07 14:00:39 2012 +0200
+++ b/hgext/obsolete.py	Tue Aug 07 14:08:20 2012 +0200
@@ -95,6 +95,7 @@
         self._extcallables = []
         self._repocallables = []
         self._revsetsymbols = []
+        self._templatekws = []
         self._commandwrappers = []
         self._extcommandwrappers = []
         self._functionwrappers = []
@@ -140,6 +141,8 @@
         knownexts = {}
         for name, symbol in self._revsetsymbols:
             revset.symbols[name] = symbol
+        for name, kw in self._templatekws:
+            templatekw.keywords[name] = kw
         for ext, command, wrapper in self._extcommandwrappers:
             if ext not in knownexts:
                 e = extensions.find('rebase')
@@ -216,6 +219,24 @@
             return symbol
         return dec
 
+
+    def templatekw(self, keywordname):
+        """Decorated function is a revset keyword
+
+        The name of the keyword must be given as the decorator argument.
+        The symbol is added during `extsetup`.
+
+        example::
+
+            @eh.templatekw('babar')
+            def kwbabar(ctx):
+                return 'babar'
+        """
+        def dec(keyword):
+            self._templatekws.append((keywordname, keyword))
+            return keyword
+        return dec
+
     def wrapcommand(self, command, extension=None):
         """Decorated function is a command wrapper
 
@@ -487,6 +508,7 @@
 ### template keywords
 #####################
 
+@eh.templatekw('obsolete')
 def obsoletekw(repo, ctx, templ, **args):
     """:obsolete: String. The obsolescence level of the node, could be
     ``stable``, ``unstable``, ``suspended`` or ``extinct``.
@@ -500,10 +522,6 @@
         return 'unstable'
     return 'stable'
 
-@eh.extsetup
-def addkeyword(ui):
-    templatekw.keywords['obsolete'] = obsoletekw
-
 ### Other Extension compat
 ############################