mercurial/registrar.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Sun, 14 Feb 2016 13:25:59 +0000
changeset 28189 fac3a24be50e
parent 27585 60bf90eb8bf8
child 28392 b983a2f04987
permissions -rw-r--r--
rebase: choose default destination the same way as 'hg merge' (BC) This changeset finally make 'hg rebase' choose its default destination using the same logic as 'hg merge'. The previous default was "tipmost changeset on the current branch", the new default is "the other head if there is only one". This change has multiple consequences: - Multiple tests which were not rebasing anything (rebasing from tipmost head) are now rebasing on the other "lower" branch. This is the expected new behavior. - A test is now explicitly aborting when there is too many heads on the branch. This is the expected behavior. - We gained a better detection of the "nothing to rebase" case while performing 'hg pull --rebase' so the message have been updated. Making clearer than an update was performed and why. This is beneficial side-effect. - Rebasing from an active bookmark will behave the same as 'hg merge' from a bookmark.

# registrar.py - utilities to register function for specific purpose
#
#  Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

from . import (
    util,
)

class funcregistrar(object):
    """Base of decorator to register a fuction for specific purpose

    The least derived class can be defined by overriding 'table' and
    'formatdoc', for example::

        symbols = {}
        class keyword(funcregistrar):
            table = symbols
            formatdoc = ":%s: %s"

        @keyword('bar')
        def barfunc(*args, **kwargs):
            '''Explanation of bar keyword ....
            '''
            pass

    In this case:

    - 'barfunc' is registered as 'bar' in 'symbols'
    - online help uses ":bar: Explanation of bar keyword"
    """

    def __init__(self, decl):
        """'decl' is a name or more descriptive string of a function

        Specification of 'decl' depends on registration purpose.
        """
        self.decl = decl

    table = None

    def __call__(self, func):
        """Execute actual registration for specified function
        """
        name = self.getname()

        if func.__doc__ and not util.safehasattr(func, '_origdoc'):
            doc = func.__doc__.strip()
            func._origdoc = doc
            if callable(self.formatdoc):
                func.__doc__ = self.formatdoc(doc)
            else:
                # convenient shortcut for simple format
                func.__doc__ = self.formatdoc % (self.decl, doc)

        self.table[name] = func
        self.extraaction(name, func)

        return func

    def getname(self):
        """Return the name of the registered function from self.decl

        Derived class should override this, if it allows more
        descriptive 'decl' string than just a name.
        """
        return self.decl

    def parsefuncdecl(self):
        """Parse function declaration and return the name of function in it
        """
        i = self.decl.find('(')
        if i > 0:
            return self.decl[:i]
        else:
            return self.decl

    def formatdoc(self, doc):
        """Return formatted document of the registered function for help

        'doc' is '__doc__.strip()' of the registered function.

        If this is overridden by non-callable object in derived class,
        such value is treated as "format string" and used to format
        document by 'self.formatdoc % (self.decl, doc)' for convenience.
        """
        raise NotImplementedError()

    def extraaction(self, name, func):
        """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)