[PATCH 1 of 3] registrar: add templatekeyword to mark a function as template keyword (API)

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat Mar 12 20:20:45 UTC 2016


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1457813826 -32400
#      Sun Mar 13 05:17:06 2016 +0900
# Node ID dec6fedbede5d40088f18057f49ae5c4ebd634ef
# Parent  70c2f8a982766b512e9d7f41f2d93fdb92f5481f
registrar: add templatekeyword to mark a function as template keyword (API)

_templateregistrarbase is defined as a super class of templatekeyword,
for ease of adding template common features between "keyword",
"filter" and "function".

This patch also adds loadkeyword() to templatekw, because this
combination helps to figure out how they cooperate with each other.

Listing up loadkeyword() in dispatch.extraloaders causes implicit
loading template keyword functions at loading (3rd party) extension.

This change requires that "templatekeyword" attribute of (3rd party)
extension is registrar.templatekeyword or so.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -35,6 +35,7 @@ from . import (
     hg,
     hook,
     revset,
+    templatekw,
     ui as uimod,
     util,
 )
@@ -756,6 +757,7 @@ extraloaders = [
     ('cmdtable', commands, 'loadcmdtable'),
     ('filesetpredicate', fileset, 'loadpredicate'),
     ('revsetpredicate', revset, 'loadpredicate'),
+    ('templatekeyword', templatekw, 'loadkeyword'),
 ]
 
 def _dispatch(req):
diff --git a/mercurial/registrar.py b/mercurial/registrar.py
--- a/mercurial/registrar.py
+++ b/mercurial/registrar.py
@@ -161,3 +161,33 @@ class filesetpredicate(_funcregistrarbas
     def _extrasetup(self, name, func, callstatus=False, callexisting=False):
         func._callstatus = callstatus
         func._callexisting = callexisting
+
+class _templateregistrarbase(_funcregistrarbase):
+    """Base of decorator to register functions as template specific one
+    """
+    _docformat = ":%s: %s"
+
+class templatekeyword(_templateregistrarbase):
+    """Decorator to register template keyword
+
+    Usage::
+
+        templaetkeyword = registrar.templatekeyword()
+
+        @templatekeyword('mykeyword')
+        def mykeywordfunc(repo, ctx, templ, cache, revcache, **args):
+            '''Explanation of this template keyword ....
+            '''
+            pass
+
+    The first string argument is used also in online help.
+
+    'templatekeyword' instance in example above can be used to
+    decorate multiple functions.
+
+    Decorated functions are registered automatically at loading
+    extension, if an instance named as 'templatekeyword' is used for
+    decorating in extension.
+
+    Otherwise, explicit 'templatekw.loadkeyword()' is needed.
+    """
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -574,5 +574,11 @@ keywords = {
     'tags': showtags,
 }
 
+def loadkeyword(ui, extname, registrarobj):
+    """Load template keyword from specified registrarobj
+    """
+    for name, func in registrarobj._table.iteritems():
+        keywords[name] = func
+
 # tell hggettext to extract docstrings from these functions:
 i18nfunctions = keywords.values()


More information about the Mercurial-devel mailing list