Skip to content

Commit d825d6f

Browse files
authored
_utils._get_float_setting (#287)
1 parent 41cd9b8 commit d825d6f

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

scrapy_playwright/_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import scrapy
88
from playwright.async_api import Error, Page, Request, Response
99
from scrapy.http.headers import Headers
10+
from scrapy.settings import Settings
1011
from scrapy.utils.python import to_unicode
1112
from twisted.internet.defer import Deferred
1213
from w3lib.encoding import html_body_declared_encoding, http_content_type_encoding
@@ -85,6 +86,13 @@ async def _get_page_content(
8586
raise
8687

8788

89+
def _get_float_setting(settings: Settings, key: str) -> Optional[float]:
90+
try:
91+
return float(settings[key])
92+
except Exception:
93+
return None
94+
95+
8896
async def _get_header_value(
8997
resource: Union[Request, Response],
9098
header_name: str,

scrapy_playwright/handler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
_ThreadedLoopAdapter,
3737
_deferred_from_coro,
3838
_encode_body,
39+
_get_float_setting,
3940
_get_header_value,
4041
_get_page_content,
4142
_is_safe_close_error,
@@ -73,7 +74,7 @@ class Config:
7374
max_pages_per_context: int
7475
max_contexts: Optional[int]
7576
startup_context_kwargs: dict
76-
navigation_timeout: Optional[float] = None
77+
navigation_timeout: Optional[float]
7778

7879
@classmethod
7980
def from_settings(cls, settings: Settings) -> "Config":
@@ -85,15 +86,15 @@ def from_settings(cls, settings: Settings) -> "Config":
8586
max_pages_per_context=settings.getint("PLAYWRIGHT_MAX_PAGES_PER_CONTEXT"),
8687
max_contexts=settings.getint("PLAYWRIGHT_MAX_CONTEXTS") or None,
8788
startup_context_kwargs=settings.getdict("PLAYWRIGHT_CONTEXTS"),
89+
navigation_timeout=_get_float_setting(
90+
settings, "PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT"
91+
),
8892
)
8993
cfg.cdp_kwargs.pop("endpoint_url", None)
9094
if not cfg.max_pages_per_context:
9195
cfg.max_pages_per_context = settings.getint("CONCURRENT_REQUESTS")
9296
if cfg.cdp_url and cfg.launch_options:
9397
logger.warning("PLAYWRIGHT_CDP_URL is set, ignoring PLAYWRIGHT_LAUNCH_OPTIONS")
94-
if "PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT" in settings:
95-
with suppress(TypeError, ValueError):
96-
cfg.navigation_timeout = float(settings["PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT"])
9798
return cfg
9899

99100

tests/tests_asyncio/test_utils.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import logging
2+
from decimal import Decimal
23
from unittest import IsolatedAsyncioTestCase
34
from unittest.mock import AsyncMock
45

56
import pytest
67
from playwright.async_api import Error as PlaywrightError
78
from scrapy import Spider
89
from scrapy.http.headers import Headers
9-
10+
from scrapy.settings import Settings
1011
from scrapy_playwright._utils import (
1112
_NAVIGATION_ERROR_MSG,
1213
_encode_body,
14+
_get_float_setting,
1315
_get_header_value,
1416
_get_page_content,
1517
_maybe_await,
@@ -122,20 +124,19 @@ async def test_encode_mismatch(self):
122124

123125

124126
class TestHeaderValue(IsolatedAsyncioTestCase):
125-
async def test_get_header_ok(self):
127+
async def test_get_header_value(self):
126128
async def _identity(x):
127129
return x
128130

129-
resource = AsyncMock()
130-
resource.header_value = _identity
131-
assert "asdf" == await _get_header_value(resource, "asdf")
132-
assert "qwerty" == await _get_header_value(resource, "qwerty")
131+
res1 = AsyncMock()
132+
res1.header_value = _identity
133+
assert "asdf" == await _get_header_value(res1, "asdf")
134+
assert "qwerty" == await _get_header_value(res1, "qwerty")
133135

134-
async def test_get_header_exception(self):
135-
resource = AsyncMock()
136-
resource.header_value.side_effect = Exception("nope")
137-
assert await _get_header_value(resource, "asdf") is None
138-
assert await _get_header_value(resource, "qwerty") is None
136+
res2 = AsyncMock()
137+
res2.header_value.side_effect = Exception("nope")
138+
assert await _get_header_value(res2, "asdf") is None
139+
assert await _get_header_value(res2, "qwerty") is None
139140

140141

141142
class TestMaybeAwait(IsolatedAsyncioTestCase):
@@ -149,3 +150,28 @@ async def _awaitable_identity(x):
149150
assert await _maybe_await("foo") == "foo"
150151
assert await _maybe_await("bar") == "bar"
151152
assert await _maybe_await(1234) == 1234
153+
154+
155+
class TestGetFloatSetting(IsolatedAsyncioTestCase):
156+
async def test_get_float_setting(self):
157+
settings = Settings(
158+
{
159+
"ZERO": 0,
160+
"FLOAT": 1.5,
161+
"DECIMAL": Decimal("2.5"),
162+
"INT": 3,
163+
"NUMERIC_STRING": "123",
164+
"NON_NUMERIC_STRING": "asdf",
165+
"NONE": None,
166+
"LIST": [1, 2, 3],
167+
}
168+
)
169+
assert _get_float_setting(settings, "ZERO") == 0.0
170+
assert _get_float_setting(settings, "FLOAT") == 1.5
171+
assert _get_float_setting(settings, "DECIMAL") == 2.5
172+
assert _get_float_setting(settings, "INT") == 3.0
173+
assert _get_float_setting(settings, "NUMERIC_STRING") == 123
174+
assert _get_float_setting(settings, "NON_NUMERIC_STRING") is None
175+
assert _get_float_setting(settings, "NONE") is None
176+
assert _get_float_setting(settings, "LIST") is None
177+
assert _get_float_setting(settings, "MISSING_KEY") is None

0 commit comments

Comments
 (0)