Skip to content

Commit

Permalink
Use build_from_crawler on Scrapy 2.12+ (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio authored Dec 27, 2024
1 parent 6145931 commit c998d09
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions scrapy_poet/_request_fingerprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@
from scrapy import Request
from scrapy.crawler import Crawler
from scrapy.settings.default_settings import REQUEST_FINGERPRINTER_CLASS
from scrapy.utils.misc import create_instance, load_object
from scrapy.utils.misc import load_object

try:
from scrapy.utils.misc import build_from_crawler
except ImportError: # Scrapy < 2.12
from typing import Any, TypeVar

from scrapy.utils.misc import create_instance

T = TypeVar("T")

def build_from_crawler(
objcls: type[T], crawler: Crawler, /, *args: Any, **kwargs: Any
) -> T:
return create_instance(objcls, None, crawler, *args, **kwargs)

from web_poet import (
HttpClient,
HttpRequest,
Expand Down Expand Up @@ -65,16 +80,14 @@ def from_crawler(cls, crawler):
return cls(crawler)

def __init__(self, crawler: Crawler) -> None:
settings = crawler.settings
self._base_request_fingerprinter = create_instance(
self._base_request_fingerprinter = build_from_crawler(
load_object(
settings.get(
crawler.settings.get(
"SCRAPY_POET_REQUEST_FINGERPRINTER_BASE_CLASS",
REQUEST_FINGERPRINTER_CLASS,
)
),
settings=crawler.settings,
crawler=crawler,
crawler,
)
self._callback_cache: Dict[Callable, Optional[bytes]] = {}
self._request_cache: "WeakKeyDictionary[Request, bytes]" = (
Expand Down

0 comments on commit c998d09

Please sign in to comment.