| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,7 @@ | |||
| 48 | 48 | from playwright._impl._console_message import ConsoleMessage | |
| 49 | 49 | from playwright._impl._debugger import Debugger | |
| 50 | 50 | from playwright._impl._dialog import Dialog | |
| 51 | + from playwright._impl._disposable import Disposable, DisposableStub | ||
| 51 | 52 | from playwright._impl._errors import Error, TargetClosedError | |
| 52 | 53 | from playwright._impl._event_context_manager import EventContextManagerImpl | |
| 53 | 54 | from playwright._impl._fetch import APIRequestContext | |
@@ -394,16 +395,18 @@ async def set_offline(self, offline: bool) -> None: | |||
| 394 | 395 | ||
| 395 | 396 | async def add_init_script( | |
| 396 | 397 | self, script: str = None, path: Union[str, Path] = None | |
| 397 | - ) -> None: | ||
| 398 | + ) -> Disposable: | ||
| 398 | 399 | if path: | |
| 399 | 400 | script = (await async_readfile(path)).decode() | |
| 400 | 401 | if not isinstance(script, str): | |
| 401 | 402 | raise Error("Either path or script parameter must be specified") | |
| 402 | - await self._channel.send("addInitScript", None, dict(source=script)) | ||
| 403 | + return from_channel( | ||
| 404 | + await self._channel.send("addInitScript", None, dict(source=script)) | ||
| 405 | + ) | ||
| 403 | 406 | ||
| 404 | 407 | async def expose_binding( | |
| 405 | 408 | self, name: str, callback: Callable, handle: bool = None | |
| 406 | - ) -> None: | ||
| 409 | + ) -> Disposable: | ||
| 407 | 410 | for page in self._pages: | |
| 408 | 411 | if name in page._bindings: | |
| 409 | 412 | raise Error( | |
@@ -412,16 +415,18 @@ async def expose_binding( | |||
| 412 | 415 | if name in self._bindings: | |
| 413 | 416 | raise Error(f'Function "{name}" has been already registered') | |
| 414 | 417 | self._bindings[name] = callback | |
| 415 | - await self._channel.send( | ||
| 416 | - "exposeBinding", None, dict(name=name, needsHandle=handle or False) | ||
| 418 | + return from_channel( | ||
| 419 | + await self._channel.send( | ||
| 420 | + "exposeBinding", None, dict(name=name, needsHandle=handle or False) | ||
| 421 | + ) | ||
| 417 | 422 | ) | |
| 418 | 423 | ||
| 419 | - async def expose_function(self, name: str, callback: Callable) -> None: | ||
| 420 | - await self.expose_binding(name, lambda source, *args: callback(*args)) | ||
| 424 | + async def expose_function(self, name: str, callback: Callable) -> Disposable: | ||
| 425 | + return await self.expose_binding(name, lambda source, *args: callback(*args)) | ||
| 421 | 426 | ||
| 422 | 427 | async def route( | |
| 423 | 428 | self, url: URLMatch, handler: RouteHandlerCallback, times: int = None | |
| 424 | - ) -> None: | ||
| 429 | + ) -> DisposableStub: | ||
| 425 | 430 | self._routes.insert( | |
| 426 | 431 | 0, | |
| 427 | 432 | RouteHandler( | |
@@ -433,6 +438,7 @@ async def route( | |||
| 433 | 438 | ), | |
| 434 | 439 | ) | |
| 435 | 440 | await self._update_interception_patterns() | |
| 441 | + return DisposableStub(lambda: self.unroute(url, handler), self) | ||
| 436 | 442 | ||
| 437 | 443 | async def unroute( | |
| 438 | 444 | self, url: URLMatch, handler: Optional[RouteHandlerCallback] = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,93 @@ | |||
| 1 | + # Copyright (c) Microsoft Corporation. | ||
| 2 | + # | ||
| 3 | + # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + # you may not use this file except in compliance with the License. | ||
| 5 | + # You may obtain a copy of the License at | ||
| 6 | + # | ||
| 7 | + # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + # | ||
| 9 | + # Unless required by applicable law or agreed to in writing, software | ||
| 10 | + # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + # See the License for the specific language governing permissions and | ||
| 13 | + # limitations under the License. | ||
| 14 | + | ||
| 15 | + import asyncio | ||
| 16 | + import inspect | ||
| 17 | + import traceback | ||
| 18 | + from typing import Awaitable, Callable, Dict | ||
| 19 | + | ||
| 20 | + import greenlet | ||
| 21 | + | ||
| 22 | + from playwright._impl._connection import ChannelOwner | ||
| 23 | + from playwright._impl._errors import Error, is_target_closed_error | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + class Disposable(ChannelOwner): | ||
| 27 | + def __init__( | ||
| 28 | + self, parent: ChannelOwner, type: str, guid: str, initializer: Dict | ||
| 29 | + ) -> None: | ||
| 30 | + super().__init__(parent, type, guid, initializer) | ||
| 31 | + | ||
| 32 | + async def dispose(self) -> None: | ||
| 33 | + try: | ||
| 34 | + await self._channel.send( | ||
| 35 | + "dispose", | ||
| 36 | + None, | ||
| 37 | + ) | ||
| 38 | + except Exception as e: | ||
| 39 | + if not is_target_closed_error(e): | ||
| 40 | + raise e | ||
| 41 | + | ||
| 42 | + async def close(self) -> None: | ||
| 43 | + await self.dispose() | ||
| 44 | + | ||
| 45 | + def __repr__(self) -> str: | ||
| 46 | + return "<Disposable>" | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + class DisposableStub: | ||
| 50 | + def __init__( | ||
| 51 | + self, | ||
| 52 | + dispose_fn: Callable[[], Awaitable[None]], | ||
| 53 | + parent: ChannelOwner, | ||
| 54 | + ) -> None: | ||
| 55 | + self._dispose_fn = dispose_fn | ||
| 56 | + self._loop = parent._loop | ||
| 57 | + self._dispatcher_fiber = parent._dispatcher_fiber | ||
| 58 | + | ||
| 59 | + async def dispose(self) -> None: | ||
| 60 | + await self._dispose_fn() | ||
| 61 | + | ||
| 62 | + async def __aenter__(self) -> "DisposableStub": | ||
| 63 | + return self | ||
| 64 | + | ||
| 65 | + async def __aexit__(self, *args: object) -> None: | ||
| 66 | + await self.dispose() | ||
| 67 | + | ||
| 68 | + def __enter__(self) -> "DisposableStub": | ||
| 69 | + return self | ||
| 70 | + | ||
| 71 | + def __exit__(self, *args: object) -> None: | ||
| 72 | + self._sync(self.dispose()) | ||
| 73 | + | ||
| 74 | + def _sync(self, coro: object) -> object: | ||
| 75 | + __tracebackhide__ = True | ||
| 76 | + if self._loop.is_closed(): | ||
| 77 | + coro.close() # type: ignore | ||
| 78 | + raise Error("Event loop is closed! Is Playwright already stopped?") | ||
| 79 | + g_self = greenlet.getcurrent() | ||
| 80 | + task = self._loop.create_task(coro) # type: ignore | ||
| 81 | + setattr(task, "__pw_stack__", inspect.stack(0)) | ||
| 82 | + setattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10)) | ||
| 83 | + task.add_done_callback(lambda _: g_self.switch()) | ||
| 84 | + while not task.done(): | ||
| 85 | + self._dispatcher_fiber.switch() # type: ignore | ||
| 86 | + asyncio._set_running_loop(self._loop) | ||
| 87 | + return task.result() | ||
| 88 | + | ||
| 89 | + async def close(self) -> None: | ||
| 90 | + await self.dispose() | ||
| 91 | + | ||
| 92 | + def __repr__(self) -> str: | ||
| 93 | + return "<Disposable>" | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | from playwright._impl._connection import ChannelOwner | |
| 23 | 23 | from playwright._impl._debugger import Debugger | |
| 24 | 24 | from playwright._impl._dialog import Dialog | |
| 25 | + from playwright._impl._disposable import Disposable | ||
| 25 | 26 | from playwright._impl._element_handle import ElementHandle | |
| 26 | 27 | from playwright._impl._fetch import APIRequestContext | |
| 27 | 28 | from playwright._impl._frame import Frame | |
@@ -69,6 +70,8 @@ def create_remote_object( | |||
| 69 | 70 | return Debugger(parent, type, guid, initializer) | |
| 70 | 71 | if type == "Dialog": | |
| 71 | 72 | return Dialog(parent, type, guid, initializer) | |
| 73 | + if type == "Disposable": | ||
| 74 | + return Disposable(parent, type, guid, initializer) | ||
| 72 | 75 | if type == "ElementHandle": | |
| 73 | 76 | return ElementHandle(parent, type, guid, initializer) | |
| 74 | 77 | if type == "Frame": | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,7 @@ | |||
| 49 | 49 | from_nullable_channel, | |
| 50 | 50 | ) | |
| 51 | 51 | from playwright._impl._console_message import ConsoleMessage | |
| 52 | + from playwright._impl._disposable import Disposable, DisposableStub | ||
| 52 | 53 | from playwright._impl._download import Download | |
| 53 | 54 | from playwright._impl._element_handle import ElementHandle, determine_screenshot_type | |
| 54 | 55 | from playwright._impl._errors import Error, TargetClosedError, is_target_closed_error | |
@@ -501,23 +502,25 @@ async def add_style_tag( | |||
| 501 | 502 | ) -> ElementHandle: | |
| 502 | 503 | return await self._main_frame.add_style_tag(**locals_to_params(locals())) | |
| 503 | 504 | ||
| 504 | - async def expose_function(self, name: str, callback: Callable) -> None: | ||
| 505 | - await self.expose_binding(name, lambda source, *args: callback(*args)) | ||
| 505 | + async def expose_function(self, name: str, callback: Callable) -> Disposable: | ||
| 506 | + return await self.expose_binding(name, lambda source, *args: callback(*args)) | ||
| 506 | 507 | ||
| 507 | 508 | async def expose_binding( | |
| 508 | 509 | self, name: str, callback: Callable, handle: bool = None | |
| 509 | - ) -> None: | ||
| 510 | + ) -> Disposable: | ||
| 510 | 511 | if name in self._bindings: | |
| 511 | 512 | raise Error(f'Function "{name}" has been already registered') | |
| 512 | 513 | if name in self._browser_context._bindings: | |
| 513 | 514 | raise Error( | |
| 514 | 515 | f'Function "{name}" has been already registered in the browser context' | |
| 515 | 516 | ) | |
| 516 | 517 | self._bindings[name] = callback | |
| 517 | - await self._channel.send( | ||
| 518 | - "exposeBinding", | ||
| 519 | - None, | ||
| 520 | - dict(name=name, needsHandle=handle or False), | ||
| 518 | + return from_channel( | ||
| 519 | + await self._channel.send( | ||
| 520 | + "exposeBinding", | ||
| 521 | + None, | ||
| 522 | + dict(name=name, needsHandle=handle or False), | ||
| 523 | + ) | ||
| 521 | 524 | ) | |
| 522 | 525 | ||
| 523 | 526 | async def set_extra_http_headers(self, headers: Dict[str, str]) -> None: | |
@@ -661,18 +664,20 @@ async def bring_to_front(self) -> None: | |||
| 661 | 664 | ||
| 662 | 665 | async def add_init_script( | |
| 663 | 666 | self, script: str = None, path: Union[str, Path] = None | |
| 664 | - ) -> None: | ||
| 667 | + ) -> Disposable: | ||
| 665 | 668 | if path: | |
| 666 | 669 | script = add_source_url_to_script( | |
| 667 | 670 | (await async_readfile(path)).decode(), path | |
| 668 | 671 | ) | |
| 669 | 672 | if not isinstance(script, str): | |
| 670 | 673 | raise Error("Either path or script parameter must be specified") | |
| 671 | - await self._channel.send("addInitScript", None, dict(source=script)) | ||
| 674 | + return from_channel( | ||
| 675 | + await self._channel.send("addInitScript", None, dict(source=script)) | ||
| 676 | + ) | ||
| 672 | 677 | ||
| 673 | 678 | async def route( | |
| 674 | 679 | self, url: URLMatch, handler: RouteHandlerCallback, times: int = None | |
| 675 | - ) -> None: | ||
| 680 | + ) -> DisposableStub: | ||
| 676 | 681 | self._routes.insert( | |
| 677 | 682 | 0, | |
| 678 | 683 | RouteHandler( | |
@@ -684,6 +689,7 @@ async def route( | |||
| 684 | 689 | ), | |
| 685 | 690 | ) | |
| 686 | 691 | await self._update_interception_patterns() | |
| 692 | + return DisposableStub(lambda: self.unroute(url, handler), self) | ||
| 687 | 693 | ||
| 688 | 694 | async def unroute( | |
| 689 | 695 | self, url: URLMatch, handler: Optional[RouteHandlerCallback] = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | from playwright._impl._api_structures import ScreencastFrame | |
| 20 | 20 | from playwright._impl._artifact import Artifact | |
| 21 | 21 | from playwright._impl._connection import from_nullable_channel | |
| 22 | + from playwright._impl._disposable import DisposableStub | ||
| 22 | 23 | from playwright._impl._errors import Error | |
| 23 | 24 | from playwright._impl._helper import locals_to_params | |
| 24 | 25 | ||
@@ -63,7 +64,7 @@ async def start( | |||
| 63 | 64 | onFrame: ScreencastFrameCallback = None, | |
| 64 | 65 | path: Union[str, Path] = None, | |
| 65 | 66 | quality: int = None, | |
| 66 | - ) -> None: | ||
| 67 | + ) -> DisposableStub: | ||
| 67 | 68 | if self._started: | |
| 68 | 69 | raise Error("Screencast is already started") | |
| 69 | 70 | self._started = True | |
@@ -81,6 +82,7 @@ async def start( | |||
| 81 | 82 | if artifact_channel: | |
| 82 | 83 | self._artifact = from_nullable_channel(artifact_channel) | |
| 83 | 84 | self._save_path = path | |
| 85 | + return DisposableStub(lambda: self.stop(), self._page) | ||
| 84 | 86 | ||
| 85 | 87 | async def stop(self) -> None: | |
| 86 | 88 | self._started = False | |
@@ -96,18 +98,26 @@ async def show_actions( | |||
| 96 | 98 | duration: float = None, | |
| 97 | 99 | position: ScreencastPosition = None, | |
| 98 | 100 | fontSize: int = None, | |
| 99 | - ) -> None: | ||
| 101 | + ) -> DisposableStub: | ||
| 100 | 102 | await self._page._channel.send( | |
| 101 | 103 | "screencastShowActions", None, locals_to_params(locals()) | |
| 102 | 104 | ) | |
| 105 | + return DisposableStub(lambda: self.hide_actions(), self._page) | ||
| 103 | 106 | ||
| 104 | 107 | async def hide_actions(self) -> None: | |
| 105 | 108 | await self._page._channel.send("screencastHideActions", None) | |
| 106 | 109 | ||
| 107 | - async def show_overlay(self, html: str, duration: float = None) -> None: | ||
| 108 | - await self._page._channel.send( | ||
| 110 | + async def show_overlay(self, html: str, duration: float = None) -> DisposableStub: | ||
| 111 | + result = await self._page._channel.send_return_as_dict( | ||
| 109 | 112 | "screencastShowOverlay", None, locals_to_params(locals()) | |
| 110 | 113 | ) | |
| 114 | + overlay_id = (result or {}).get("id") | ||
| 115 | + return DisposableStub( | ||
| 116 | + lambda: self._page._channel.send( | ||
| 117 | + "screencastRemoveOverlay", None, {"id": overlay_id} | ||
| 118 | + ), | ||
| 119 | + self._page, | ||
| 120 | + ) | ||
| 111 | 121 | ||
| 112 | 122 | async def show_chapter( | |
| 113 | 123 | self, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,7 @@ | |||
| 18 | 18 | from playwright._impl._api_structures import TracingGroupLocation | |
| 19 | 19 | from playwright._impl._artifact import Artifact | |
| 20 | 20 | from playwright._impl._connection import ChannelOwner, from_nullable_channel | |
| 21 | + from playwright._impl._disposable import DisposableStub | ||
| 21 | 22 | from playwright._impl._helper import locals_to_params | |
| 22 | 23 | ||
| 23 | 24 | ||
@@ -148,8 +149,11 @@ def _reset_stack_counter(self) -> None: | |||
| 148 | 149 | self._is_tracing = False | |
| 149 | 150 | self._connection.set_is_tracing(False) | |
| 150 | 151 | ||
| 151 | - async def group(self, name: str, location: TracingGroupLocation = None) -> None: | ||
| 152 | + async def group( | ||
| 153 | + self, name: str, location: TracingGroupLocation = None | ||
| 154 | + ) -> DisposableStub: | ||
| 152 | 155 | await self._channel.send("tracingGroup", None, locals_to_params(locals())) | |
| 156 | + return DisposableStub(lambda: self.group_end(), self) | ||
| 153 | 157 | ||
| 154 | 158 | async def group_end(self) -> None: | |
| 155 | 159 | await self._channel.send( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments