registrar: add delayregistrar class to register function in extensions
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Tue, 29 Dec 2015 23:58:30 +0900
changeset 27585 60bf90eb8bf8
parent 27584 fc7c8cac6a4b
child 27586 42910f9fffeb
registrar: add delayregistrar class to register function in extensions 'delayregistrar' delays actual registration of function until 'setup()' invocation on it.
mercurial/registrar.py
--- a/mercurial/registrar.py	Tue Dec 29 23:58:30 2015 +0900
+++ b/mercurial/registrar.py	Tue Dec 29 23:58:30 2015 +0900
@@ -94,3 +94,35 @@
         """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)