changeset 1406:ae4bdba90e7b stable

templatekw: use decorator to register keywords
author Sean Farley <sean@farley.io>
date Thu, 05 May 2016 23:18:52 -0700
parents c5b7fb8911c0
children 963946d7dfd7
files hgsubversion/__init__.py
diffstat 1 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/__init__.py
+++ b/hgsubversion/__init__.py
@@ -42,7 +42,6 @@ demandimport.ignore.extend([
     'svn.ra',
     ])
 
-from mercurial import templatekw
 from mercurial import revset
 from mercurial import subrepo
 
@@ -51,6 +50,7 @@ import util
 import svnrepo
 import wrappers
 import svnexternals
+import compathacks
 
 svnopts = [
     ('', 'stupid', None,
@@ -165,14 +165,6 @@ def extsetup(ui):
 
     help.helptable.extend(entries)
 
-    templatekeywords = {
-        'svnrev': svnrevkw,
-        'svnpath': svnpathkw,
-        'svnuuid': svnuuidkw,
-    }
-
-    templatekw.keywords.update(templatekeywords)
-
     revset.symbols.update(util.revsets)
 
     subrepo.types['hgsubversion'] = svnexternals.svnsubrepo
@@ -226,6 +218,24 @@ cmdtable = {
 # only these methods are public
 __all__ = ('cmdtable', 'reposetup', 'uisetup')
 
+# set up templatekeywords (written this way to maintain backwards compatibility
+# until we drop support for 3.7)
+try:
+    from mercurial import registrar
+    templatekeyword = registrar.templatekeyword()
+    loadkeyword = lambda registrarobj: None  # no-op
+except (ImportError, AttributeError):
+    # registrar.templatekeyword isn't available = loading by old hg
+
+    templatekeyword = compathacks._funcregistrarbase()
+    templatekeyword._docformat = ":%s: %s"
+
+    # minimum copy from templatekw.loadkeyword
+    def loadkeyword(registrarobj):
+        from mercurial import templatekw
+        for name, func in registrarobj._table.iteritems():
+            templatekw.keywords[name] = func
+
 def _templatehelper(ctx, kw):
     '''
     Helper function for displaying information about converted changesets.
@@ -244,14 +254,17 @@ def _templatehelper(ctx, kw):
     else:
         raise hgutil.Abort('unrecognized hgsubversion keyword %s' % kw)
 
+@templatekeyword('svnrev')
 def svnrevkw(**args):
     """:svnrev: String. Converted subversion revision number."""
     return _templatehelper(args['ctx'], 'svnrev')
 
+@templatekeyword('svnpath')
 def svnpathkw(**args):
     """:svnpath: String. Converted subversion revision project path."""
     return _templatehelper(args['ctx'], 'svnpath')
 
+@templatekeyword('svnuuid')
 def svnuuidkw(**args):
     """:svnuuid: String. Converted subversion revision repository identifier."""
     return _templatehelper(args['ctx'], 'svnuuid')