mercurial/registrar.py
changeset 27585 60bf90eb8bf8
parent 27584 fc7c8cac6a4b
child 28392 b983a2f04987
--- 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)