Rule.process_request: deprecate the use of functions taking only one argument

This commit is contained in:
Eugenio Lacuesta 2019-03-22 18:34:55 -03:00
parent bbf24b7a1c
commit 56929e77d9
1 changed files with 15 additions and 10 deletions

View File

@ -6,16 +6,19 @@ See documentation in docs/topics/spiders.rst
"""
import copy
import warnings
import six
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Request, HtmlResponse
from scrapy.utils.spider import iterate_spider_output
from scrapy.utils.python import get_func_args
from scrapy.spiders import Spider
def identity(x):
return x
def _identity(request, response):
return request
class Rule(object):
@ -25,19 +28,21 @@ class Rule(object):
self.callback = callback
self.cb_kwargs = cb_kwargs or {}
self.process_links = process_links
self.process_request = process_request or identity
if follow is None:
self.follow = False if callback else True
else:
self.follow = follow
self.process_request = process_request or _identity
self.follow = follow if follow is not None else not callback
def _process_request(self, request, response):
"""
Wrapper around the request processing function to maintain backward compatibility
with functions that do not take a Response object as parameter.
Wrapper around the request processing function to maintain backward
compatibility with functions that do not take a Response object
"""
arg_count = len(get_func_args(self.process_request))
args = [request] if arg_count == 1 else [request, response]
if arg_count == 1:
args = [request]
msg = 'Rule.process_request should accept two arguments (request, response), accepting only one is deprecated'
warnings.warn(msg, category=ScrapyDeprecationWarning, stacklevel=2)
else:
args = [request, response]
return self.process_request(*args)