changeset 27585:60bf90eb8bf8

registrar: add delayregistrar class to register function in extensions 'delayregistrar' delays actual registration of function until 'setup()' invocation on it.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Tue, 29 Dec 2015 23:58:30 +0900
parents fc7c8cac6a4b
children 42910f9fffeb
files mercurial/registrar.py
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)