Example #1
0
def from_method(self, method, results):
    """
    Copied from `scrapy.contracts.ContractManager`.
    """
    contracts = self.extract_contracts(method)
    if contracts:
        # calculate request args
        args, kwargs = get_spec(Request.__init__)
        kwargs['callback'] = method
        for contract in contracts:
            kwargs = contract.adjust_request_args(kwargs)

        # create and prepare request
        args.remove('self')
        if set(args).issubset(set(kwargs)):
            # Willet: All that we do is modify these two lines to access the
            # `request_cls` from the spider if it exists.
            req_cls = getattr(method.im_self, 'request_cls', Request)
            request = req_cls(**kwargs)

            # execute pre and post hooks in order
            for contract in reversed(contracts):
                request = contract.add_pre_hook(request, results)
            for contract in contracts:
                request = contract.add_post_hook(request, results)

            return request
Example #2
0
    def from_method(self, method, results):
        contracts = self.extract_contracts(method)
        if contracts:
            request_cls = Request
            for contract in contracts:
                if contract.request_cls is not None:
                    request_cls = contract.request_cls

            # calculate request args
            args, kwargs = get_spec(request_cls.__init__)

            # Don't filter requests to allow
            # testing different callbacks on the same URL.
            kwargs['dont_filter'] = True
            kwargs['callback'] = method

            for contract in contracts:
                kwargs = contract.adjust_request_args(kwargs)

            args.remove('self')

            # check if all positional arguments are defined in kwargs
            if set(args).issubset(set(kwargs)):
                request = request_cls(**kwargs)

                # execute pre and post hooks in order
                for contract in reversed(contracts):
                    request = contract.add_pre_hook(request, results)
                for contract in contracts:
                    request = contract.add_post_hook(request, results)

                self._clean_req(request, method, results)
                return request
Example #3
0
    def from_method(self, method, results):
        contracts = self.extract_contracts(method)
        if contracts:
            request_cls = Request
            for contract in contracts:
                if contract.request_cls is not None:
                    request_cls = contract.request_cls

            # calculate request args
            args, kwargs = get_spec(request_cls.__init__)

            # Don't filter requests to allow
            # testing different callbacks on the same URL.
            kwargs['dont_filter'] = True
            kwargs['callback'] = method

            for contract in contracts:
                kwargs = contract.adjust_request_args(kwargs)

            args.remove('self')

            # check if all positional arguments are defined in kwargs
            if set(args).issubset(set(kwargs)):
                request = request_cls(**kwargs)

                # execute pre and post hooks in order
                for contract in reversed(contracts):
                    request = contract.add_pre_hook(request, results)
                for contract in contracts:
                    request = contract.add_post_hook(request, results)

                self._clean_req(request, method, results)
                return request
Example #4
0
    def from_method(self, method, results):
        contracts = self.extract_contracts(method)
        if contracts:
            # calculate request args
            args, kwargs = get_spec(Request.__init__)
            kwargs['callback'] = method
            for contract in contracts:
                kwargs = contract.adjust_request_args(kwargs)

            # create and prepare request
            args.remove('self')
            if set(args).issubset(set(kwargs)):
                request = Request(**kwargs)

                # execute pre and post hooks in order
                for contract in reversed(contracts):
                    request = contract.add_pre_hook(request, results)
                for contract in contracts:
                    request = contract.add_post_hook(request, results)

                return request
Example #5
0
    def from_method(self, method, fail=False):
        contracts = self.extract_contracts(method)
        if contracts:
            # calculate request args
            args, kwargs = get_spec(Request.__init__)
            kwargs['callback'] = method
            for contract in contracts:
                kwargs = contract.adjust_request_args(kwargs)

            # create and prepare request
            args.remove('self')
            if set(args).issubset(set(kwargs)):
                request = Request(**kwargs)

                # execute pre and post hooks in order
                for contract in reversed(contracts):
                    request = contract.add_pre_hook(request, fail)
                for contract in contracts:
                    request = contract.add_post_hook(request, fail)

                return request