Skip to content

Commit c779e19

Browse files
committed
Merge remote-tracking branch 'origin/main' into close-inactive-contexts
2 parents 8035e27 + e0f3b61 commit c779e19

File tree

19 files changed

+265
-243
lines changed

19 files changed

+265
-243
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.28
2+
current_version = 0.0.29
33
commit = True
44
tag = True
55

.github/workflows/tests.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
os: [ubuntu-latest, macos-latest]
12-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
12+
python-version: ["3.8", "3.9", "3.10", "3.11"]
1313

1414
steps:
1515
- uses: actions/checkout@v2
@@ -22,9 +22,12 @@ jobs:
2222
- name: Install tox
2323
run: pip install tox
2424

25-
- name: Run tests
25+
- name: Run asyncio tests
2626
run: tox -e py
2727

28+
- name: Run twisted tests
29+
run: tox -e py-twisted
30+
2831
- name: Upload coverage report
2932
run: |
3033
if [ "${{ runner.os }}" = "Linux" ]; then

.gitignore

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
.mypy_cache/
55
*.egg-info/
66
.tox/
7-
.coverage
8-
.coverage.*
9-
htmlcov/
10-
coverage.xml
117
build/
128
dist/
139
examples/*.png
1410
pip-wheel-metadata/
11+
12+
# coverage
13+
.coverage
14+
.coverage.*
15+
htmlcov/
16+
coverage.xml
17+
coverage-*.xml
18+
coverage-asyncio/
19+
coverage-twisted/

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,11 @@ See [Handling page events](#handling-page-events).
363363
### `playwright_page_init_callback`
364364
Type `Optional[Union[Callable, str]]`, default `None`
365365

366-
A coroutine function (`async def`) to be invoked immediately after creating
367-
a page for the request. It receives the page and the request as positional
368-
arguments. Useful for initialization code. Invoked only for newly created
369-
pages, ignored if the page for the request already exists (e.g. by passing
366+
A coroutine function (`async def`) to be invoked for newly created pages.
367+
Called after attaching page event handlers & setting up internal route
368+
handling, before making any request. It receives the Playwright page and the
369+
Scrapy request as positional arguments. Useful for initialization code.
370+
Ignored if the page for the request already exists (e.g. by passing
370371
`playwright_page`).
371372

372373
```python

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# scrapy-playwright changelog
22

33

4+
### [v0.0.29](https://github.com/scrapy-plugins/scrapy-playwright/releases/tag/v0.0.29) (2023-08-11)
5+
6+
* Set exc_info=True for warning log records (#219)
7+
* Invoke page_init_callback after setting route (#205)
8+
9+
410
### [v0.0.28](https://github.com/scrapy-plugins/scrapy-playwright/releases/tag/v0.0.28) (2023-08-05)
511

612
* Retry page.content if necessary (#218)

scrapy_playwright/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.28"
1+
__version__ = "0.0.29"

scrapy_playwright/handler.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ async def _create_page(self, request: Request, spider: Spider) -> Page:
258258
"scrapy_request_method": request.method,
259259
"exception": ex,
260260
},
261+
exc_info=True,
261262
)
262263

263264
page.on("close", self._make_close_page_callback(context_name))
@@ -330,6 +331,10 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
330331
),
331332
)
332333

334+
await _maybe_execute_page_init_callback(
335+
page=page, request=request, context_name=context_name, spider=spider
336+
)
337+
333338
try:
334339
result = await self._download_request_with_page(request, page, spider)
335340
except Exception as ex:
@@ -346,6 +351,7 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
346351
"scrapy_request_method": request.method,
347352
"exception": ex,
348353
},
354+
exc_info=True,
349355
)
350356
await page.close()
351357
self.stats.inc_value("playwright/page_count/closed")
@@ -438,6 +444,7 @@ async def _apply_page_methods(self, page: Page, request: Request, spider: Spider
438444
"scrapy_request_method": request.method,
439445
"exception": ex,
440446
},
447+
exc_info=True,
441448
)
442449
else:
443450
pm.result = await _maybe_await(method(*pm.args, **pm.kwargs))
@@ -590,6 +597,7 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
590597
"playwright_request_method": playwright_request.method,
591598
"exception": ex,
592599
},
600+
exc_info=True,
593601
)
594602
else:
595603
raise
@@ -621,6 +629,7 @@ def _attach_page_event_handlers(
621629
"scrapy_request_method": request.method,
622630
"exception": ex,
623631
},
632+
exc_info=True,
624633
)
625634

626635

@@ -643,6 +652,35 @@ async def _set_redirect_meta(request: Request, response: PlaywrightResponse) ->
643652
request.meta["redirect_reasons"] = list(reversed(redirect_reasons))
644653

645654

655+
async def _maybe_execute_page_init_callback(
656+
page: Page,
657+
request: Request,
658+
context_name: str,
659+
spider: Spider,
660+
) -> None:
661+
page_init_callback = request.meta.get("playwright_page_init_callback")
662+
if page_init_callback:
663+
try:
664+
page_init_callback = load_object(page_init_callback)
665+
await page_init_callback(page, request)
666+
except Exception as ex:
667+
logger.warning(
668+
"[Context=%s] Page init callback exception for %s exc_type=%s exc_msg=%s",
669+
context_name,
670+
repr(request),
671+
type(ex),
672+
str(ex),
673+
extra={
674+
"spider": spider,
675+
"context_name": context_name,
676+
"scrapy_request_url": request.url,
677+
"scrapy_request_method": request.method,
678+
"exception": ex,
679+
},
680+
exc_info=True,
681+
)
682+
683+
646684
def _make_request_logger(context_name: str, spider: Spider) -> Callable:
647685
async def _log_request(request: PlaywrightRequest) -> None:
648686
referrer = await request.header_value("referer")

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"Development Status :: 3 - Alpha",
2323
"License :: OSI Approved :: BSD License",
2424
"Programming Language :: Python",
25-
"Programming Language :: Python :: 3.7",
2625
"Programming Language :: Python :: 3.8",
2726
"Programming Language :: Python :: 3.9",
2827
"Programming Language :: Python :: 3.10",
@@ -33,7 +32,7 @@
3332
"Topic :: Software Development :: Libraries :: Application Frameworks",
3433
"Topic :: Software Development :: Libraries :: Python Modules",
3534
],
36-
python_requires=">=3.7",
35+
python_requires=">=3.8",
3736
install_requires=[
3837
"scrapy>=2.0,!=2.4.0",
3938
"playwright>=1.15",

tests/conftest.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/test_utils.py

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)