# HG changeset patch # User FUJIWARA Katsunori # Date 2015-12-29 14:58:30 # Node ID 60bf90eb8bf89b113b3449849cc0b2bff4bd5e75 # Parent fc7c8cac6a4bb57f2e19a304814bf02bdc7fd436 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)