Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use build_from_crawler on Scrapy 2.12+ #214

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading