Exemplo n.º 1
0
    def apply(self, workbench, effective):
        payload = self.payload
        name = self.name
        _next_method = getattr(workbench.origin, self.name)
        if utils.isclass(workbench.origin):
            @wraps(payload)
            def wrapper(self, *args, **kw):
                __traceback_info__ = name
                @wraps(_next_method)
                def _next(*args, **kw):
                    return _next_method(self, *args, **kw)
                # All _next methods, not just for the current name,
                # are available via _next.all
                _next.all = AllNext(workbench.origin, self)
                return payload(_next, self, *args, **kw)
        else:
            @wraps(payload)
            def wrapper(self, *args, **kw):
                __traceback_info__ = name
                @wraps(_next_method)
                def _next(*args, **kw):
                    return _next_method(*args, **kw)
                # All _next methods, not just for the current name,
                # are available via _next.all
                _next.all = AllNext(workbench.origin)
                return payload(_next, self, *args, **kw)

        # set wrapper
        workbench.dct[self.name] = wrapper
        return True
Exemplo n.º 2
0
    def apply(self, workbench, effective):
        function_list = list(self.function_list)

        # If the last function is a default or overwrite instruction,
        # we pop it off and later evaluate its usage versus a function
        # retrieved from the origin. If origin is an instance, this
        # function builds the bridge to it and therefore needs to be
        # bound to it.
        instr = None
        if isinstance(function_list[-1], (default, overwrite)):
            instr = function_list.pop()
            fn = instr.payload
            if not utils.isclass(workbench.origin):
                fn = fn.__get__(workbench.origin, workbench.origin.__class__)

        # overwrite wins over origin, default loses
        if isinstance(instr, overwrite):
            _next_method = fn
        else:
            try:
                _next_method = getattr(workbench.origin, self.name)
            except AttributeError:
                if isinstance(instr, default):
                    _next_method = fn
                else:
                    raise

        # nest remaining functions
        for i, fn in enumerate(reversed(function_list)):
            _next_is_bound = (i == 0) and not utils.isclass(workbench.origin)
            _next_method = self._wrap(workbench.origin, fn,
                                      _next_method, _next_is_bound)

        # set wrapper
        workbench.dct[self.name] = _next_method
        return True