[PATCH 3 of 5 V2] registrar: add delayregistrar class to register function in extensions

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Tue Dec 29 09:08:50 CST 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1451401110 -32400
#      Tue Dec 29 23:58:30 2015 +0900
# Node ID a1e4c18851155866ab75a35aa5bbae81945d7db5
# Parent  52684b1cc678b27efdad33e0ee78c35809bdddf0
registrar: add delayregistrar class to register function in extensions

'delayregistrar' delays actual registration of function until
'setup()' invocation on it.

diff --git a/mercurial/registrar.py b/mercurial/registrar.py
--- a/mercurial/registrar.py
+++ b/mercurial/registrar.py
@@ -94,3 +94,35 @@ class funcregistrar(object):
         """Execute exra action for registered function, if needed
         """
         pass
+
+class delayregistrar(object):
+    """Decorator to delay actual registration until uisetup or so
+
+    For example, the decorator class to delay registration by
+    'keyword' funcregistrar can be defined as below::
+
+        class extkeyword(delayregistrar):
+            registrar = keyword
+    """
+    def __init__(self):
+        self._list = []
+
+    registrar = None
+
+    def __call__(self, *args, **kwargs):
+        """Return the decorator to delay actual registration until setup
+        """
+        assert self.registrar is not None
+        def decorator(func):
+            # invocation of self.registrar() here can detect argument
+            # mismatching immediately
+            self._list.append((func, self.registrar(*args, **kwargs)))
+            return func
+        return decorator
+
+    def setup(self):
+        """Execute actual registration
+        """
+        while self._list:
+            func, decorator = self._list.pop(0)
+            decorator(func)


More information about the Mercurial-devel mailing list