# HG changeset patch # User FUJIWARA Katsunori # Date 1451401110 -32400 # 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 -r fc7c8cac6a4b -r 60bf90eb8bf8 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)