コード例 #1
0
ファイル: objreg.py プロジェクト: karlch/vimiv-qt
def __get_class(func: Callable) -> Any:
    """Helper method to retrieve the class defining func if any.

    We define this simple wrapper to be able to cache this as it is called for every
    command / status module and should thus be as fast as possible.
    """
    if is_method(func):
        _logger.debug("Retrieving instance from objreg for '%s'", func.__qualname__)
        return class_that_defined_method(func)
    return None
コード例 #2
0
ファイル: status.py プロジェクト: sanjayankur31/vimiv-qt
    def _create_func(self, func: Callable[..., str]) -> Module:
        """Create function to call for a status module.

        This retrieves the instance of a class object for methods and sets it
        as first argument (the 'self' argument) of a lambda. For standard
        functions nothing is done.

        Returns:
            A function to be called without arguments.
        """
        _logger.debug("Creating function for status module '%s'",
                      func.__name__)
        if is_method(func):
            cls = class_that_defined_method(func)
            return functools.partial(func, cls.instance)
        return func
コード例 #3
0
ファイル: commands.py プロジェクト: kaldown/vimiv-qt
    def _create_func(self, func: typing.Callable) -> typing.Callable:
        """Create function to call for a command function.

        This processes hooks and retrieves the instance of a class object for
        methods and sets it as first argument (the 'self' argument) of a
        lambda. For standard functions nothing is done.

        Returns:
            A function to be called with any keyword arguments.
        """
        logging.debug("Creating function for command '%s'", func.__name__)
        if is_method(func):
            cls = class_that_defined_method(func)
            instance = objreg.get(cls)
            hook_method = typing.cast(HookMethod,
                                      self.hook)  # Takes self as argument
            return lambda **kwargs: (hook_method(instance),
                                     func(instance, **kwargs))
        hook_function = typing.cast(HookFunction,
                                    self.hook)  # Takes no arguments
        return lambda **kwargs: (hook_function(), func(**kwargs))