Skip to content

Commit 30c5b52

Browse files
authored
feat(api): split sync and async entry points (microsoft#382)
1 parent 062cc15 commit 30c5b52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+703
-463
lines changed

buildbots/assets/stub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from playwright import sync_playwright
1+
from playwright.sync_api import sync_playwright
22

33
with sync_playwright() as p:
44
for browser_type in [p.chromium, p.firefox, p.webkit]:

client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ def main(playwright: Playwright) -> None:
8888

8989

9090
if __name__ == "__main__":
91-
with playwright.sync_playwright() as p:
91+
with playwright.sync_api.sync_playwright() as p:
9292
main(p)

playwright/__init__.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
1415
"""
1516
Python package `playwright` is a Python library to automate Chromium,
1617
Firefox and WebKit with a single API. Playwright is built to enable cross-browser
@@ -19,50 +20,6 @@
1920
and for the async API [here](async_api.html).
2021
"""
2122

22-
import playwright._api_structures as api_structures
23-
import playwright._api_types as api_types
24-
from playwright._main import AsyncPlaywrightContextManager, SyncPlaywrightContextManager
25-
26-
DeviceDescriptor = api_types.DeviceDescriptor
27-
Error = api_types.Error
28-
FilePayload = api_types.FilePayload
29-
FloatRect = api_types.FloatRect
30-
Geolocation = api_types.Geolocation
31-
PdfMargins = api_types.PdfMargins
32-
ProxySettings = api_types.ProxySettings
33-
SourceLocation = api_types.SourceLocation
34-
TimeoutError = api_types.TimeoutError
35-
36-
Cookie = api_structures.Cookie
37-
ResourceTiming = api_structures.ResourceTiming
38-
StorageState = api_structures.StorageState
39-
40-
41-
def async_playwright() -> AsyncPlaywrightContextManager:
42-
return AsyncPlaywrightContextManager()
43-
44-
45-
def sync_playwright() -> SyncPlaywrightContextManager:
46-
return SyncPlaywrightContextManager()
47-
48-
49-
__all__ = [
50-
"async_playwright",
51-
"sync_playwright",
52-
"Cookie",
53-
"DeviceDescriptor",
54-
"Error",
55-
"FilePayload",
56-
"FloatRect",
57-
"Geolocation",
58-
"PdfMargins",
59-
"ProxySettings",
60-
"ResourceTiming",
61-
"SourceLocation",
62-
"StorageState",
63-
"TimeoutError",
64-
]
65-
6623
__pdoc__ = {
6724
"_accessibility": False,
6825
"_async_base": False,

playwright/__main__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from playwright._main import main
15+
import os
16+
import subprocess
17+
import sys
1618

17-
main()
19+
from playwright._impl._driver import compute_driver_executable
20+
21+
driver_executable = compute_driver_executable()
22+
my_env = os.environ.copy()
23+
my_env["PW_CLI_TARGET_LANG"] = "python"
24+
subprocess.run([str(driver_executable), *sys.argv[1:]], env=my_env)

playwright/_accessibility.py renamed to playwright/_impl/_accessibility.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
from typing import Dict, Optional
1616

17-
from playwright._connection import Channel
18-
from playwright._element_handle import ElementHandle
19-
from playwright._helper import locals_to_params
17+
from playwright._impl._connection import Channel
18+
from playwright._impl._element_handle import ElementHandle
19+
from playwright._impl._helper import locals_to_params
2020

2121

2222
def _ax_node_from_protocol(axNode: Dict) -> Dict:
File renamed without changes.
File renamed without changes.

playwright/_async_base.py renamed to playwright/_impl/_async_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import asyncio
1616
from typing import Any, Callable, Coroutine, Generic, Optional, TypeVar, cast
1717

18-
from playwright._impl_to_api_mapping import ImplToApiMapping, ImplWrapper
18+
from playwright._impl._impl_to_api_mapping import ImplToApiMapping, ImplWrapper
1919

2020
mapping = ImplToApiMapping()
2121

playwright/_browser.py renamed to playwright/_impl/_browser.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
from types import SimpleNamespace
1919
from typing import TYPE_CHECKING, Dict, List, Tuple, Union
2020

21-
from playwright._api_structures import StorageState
22-
from playwright._api_types import Geolocation, ProxySettings
23-
from playwright._browser_context import BrowserContext
24-
from playwright._connection import ChannelOwner, from_channel
25-
from playwright._helper import ColorScheme, is_safe_close_error, locals_to_params
26-
from playwright._network import serialize_headers
27-
from playwright._page import Page
21+
from playwright._impl._api_structures import StorageState
22+
from playwright._impl._api_types import Geolocation, ProxySettings
23+
from playwright._impl._browser_context import BrowserContext
24+
from playwright._impl._connection import ChannelOwner, from_channel
25+
from playwright._impl._helper import ColorScheme, is_safe_close_error, locals_to_params
26+
from playwright._impl._network import serialize_headers
27+
from playwright._impl._page import Page
2828

2929
if sys.version_info >= (3, 8): # pragma: no cover
3030
from typing import Literal
3131
else: # pragma: no cover
3232
from typing_extensions import Literal
3333

3434
if TYPE_CHECKING: # pragma: no cover
35-
from playwright._browser_type import BrowserType
35+
from playwright._impl._browser_type import BrowserType
3636

3737

3838
class Browser(ChannelOwner):

playwright/_browser_context.py renamed to playwright/_impl/_browser_context.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from types import SimpleNamespace
2020
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast
2121

22-
from playwright._api_structures import Cookie, StorageState
23-
from playwright._api_types import Error
24-
from playwright._connection import ChannelOwner, from_channel
25-
from playwright._event_context_manager import EventContextManagerImpl
26-
from playwright._helper import (
22+
from playwright._impl._api_structures import Cookie, StorageState
23+
from playwright._impl._api_types import Error
24+
from playwright._impl._connection import ChannelOwner, from_channel
25+
from playwright._impl._event_context_manager import EventContextManagerImpl
26+
from playwright._impl._helper import (
2727
PendingWaitEvent,
2828
RouteHandler,
2929
RouteHandlerEntry,
@@ -33,12 +33,12 @@
3333
is_safe_close_error,
3434
locals_to_params,
3535
)
36-
from playwright._network import Request, Route, serialize_headers
37-
from playwright._page import BindingCall, Page
38-
from playwright._wait_helper import WaitHelper
36+
from playwright._impl._network import Request, Route, serialize_headers
37+
from playwright._impl._page import BindingCall, Page
38+
from playwright._impl._wait_helper import WaitHelper
3939

4040
if TYPE_CHECKING: # pragma: no cover
41-
from playwright._browser import Browser
41+
from playwright._impl._browser import Browser
4242

4343

4444
class BrowserContext(ChannelOwner):

playwright/_browser_type.py renamed to playwright/_impl/_browser_type.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
from pathlib import Path
1717
from typing import Dict, List, Tuple, Union
1818

19-
from playwright._api_types import Geolocation, ProxySettings
20-
from playwright._browser import Browser, normalize_context_params
21-
from playwright._browser_context import BrowserContext
22-
from playwright._connection import ChannelOwner, from_channel
23-
from playwright._helper import ColorScheme, Env, locals_to_params, not_installed_error
19+
from playwright._impl._api_types import Geolocation, ProxySettings
20+
from playwright._impl._browser import Browser, normalize_context_params
21+
from playwright._impl._browser_context import BrowserContext
22+
from playwright._impl._connection import ChannelOwner, from_channel
23+
from playwright._impl._helper import (
24+
ColorScheme,
25+
Env,
26+
locals_to_params,
27+
not_installed_error,
28+
)
2429

2530
if sys.version_info >= (3, 8): # pragma: no cover
2631
from typing import Literal

playwright/_cdp_session.py renamed to playwright/_impl/_cdp_session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
from typing import Any, Dict
1616

17-
from playwright._connection import ChannelOwner
18-
from playwright._helper import locals_to_params
19-
from playwright._js_handle import parse_result
17+
from playwright._impl._connection import ChannelOwner
18+
from playwright._impl._helper import locals_to_params
19+
from playwright._impl._js_handle import parse_result
2020

2121

2222
class CDPSession(ChannelOwner):

playwright/_chromium_browser_context.py renamed to playwright/_impl/_chromium_browser_context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
from types import SimpleNamespace
1616
from typing import Dict, List, Set
1717

18-
from playwright._browser_context import BrowserContext
19-
from playwright._cdp_session import CDPSession
20-
from playwright._connection import ChannelOwner, from_channel
21-
from playwright._page import Page, Worker
18+
from playwright._impl._browser_context import BrowserContext
19+
from playwright._impl._cdp_session import CDPSession
20+
from playwright._impl._connection import ChannelOwner, from_channel
21+
from playwright._impl._page import Page, Worker
2222

2323

2424
class ChromiumBrowserContext(BrowserContext):

playwright/_connection.py renamed to playwright/_impl/_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from greenlet import greenlet
2222
from pyee import AsyncIOEventEmitter
2323

24-
from playwright._api_types import ApiType
25-
from playwright._helper import ParsedMessagePayload, parse_error
26-
from playwright._transport import Transport
24+
from playwright._impl._api_types import ApiType
25+
from playwright._impl._helper import ParsedMessagePayload, parse_error
26+
from playwright._impl._transport import Transport
2727

2828

2929
class Channel(AsyncIOEventEmitter):

playwright/_console_message.py renamed to playwright/_impl/_console_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
from typing import Dict, List
1616

17-
from playwright._api_types import SourceLocation
18-
from playwright._connection import ChannelOwner, from_channel
19-
from playwright._js_handle import JSHandle
17+
from playwright._impl._api_types import SourceLocation
18+
from playwright._impl._connection import ChannelOwner, from_channel
19+
from playwright._impl._js_handle import JSHandle
2020

2121

2222
class ConsoleMessage(ChannelOwner):

playwright/_dialog.py renamed to playwright/_impl/_dialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
from typing import Dict
1616

17-
from playwright._connection import ChannelOwner
18-
from playwright._helper import locals_to_params
17+
from playwright._impl._connection import ChannelOwner
18+
from playwright._impl._helper import locals_to_params
1919

2020

2121
class Dialog(ChannelOwner):

playwright/_download.py renamed to playwright/_impl/_download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from pathlib import Path
1616
from typing import Dict, Optional, Union
1717

18-
from playwright._connection import ChannelOwner
19-
from playwright._helper import patch_error_message
18+
from playwright._impl._connection import ChannelOwner
19+
from playwright._impl._helper import patch_error_message
2020

2121

2222
class Download(ChannelOwner):

playwright/_impl/_driver.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 sys
18+
from pathlib import Path
19+
20+
import playwright
21+
22+
23+
def compute_driver_executable() -> Path:
24+
package_path = Path(inspect.getfile(playwright)).parent
25+
platform = sys.platform
26+
if platform == "win32":
27+
return package_path / "driver" / "playwright-cli.exe"
28+
return package_path / "driver" / "playwright-cli"
29+
30+
31+
if sys.version_info.major == 3 and sys.version_info.minor == 7:
32+
if sys.platform == "win32":
33+
# Use ProactorEventLoop in 3.7, which is default in 3.8
34+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
35+
else:
36+
# Prevent Python 3.7 from throwing on Linux:
37+
# RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
38+
asyncio.get_event_loop()
39+
asyncio.get_child_watcher()

playwright/_element_handle.py renamed to playwright/_impl/_element_handle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
cast,
2828
)
2929

30-
from playwright._api_types import FilePayload, FloatRect, filter_out_none
31-
from playwright._connection import ChannelOwner, from_nullable_channel
32-
from playwright._file_chooser import normalize_file_payloads
33-
from playwright._helper import KeyboardModifier, MouseButton, locals_to_params
34-
from playwright._js_handle import (
30+
from playwright._impl._api_types import FilePayload, FloatRect, filter_out_none
31+
from playwright._impl._connection import ChannelOwner, from_nullable_channel
32+
from playwright._impl._file_chooser import normalize_file_payloads
33+
from playwright._impl._helper import KeyboardModifier, MouseButton, locals_to_params
34+
from playwright._impl._js_handle import (
3535
JSHandle,
3636
Serializable,
3737
parse_result,
@@ -44,7 +44,7 @@
4444
from typing_extensions import Literal
4545

4646
if TYPE_CHECKING: # pragma: no cover
47-
from playwright._frame import Frame
47+
from playwright._impl._frame import Frame
4848

4949

5050
class ElementHandle(JSHandle):

playwright/_file_chooser.py renamed to playwright/_impl/_file_chooser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from pathlib import Path
1919
from typing import TYPE_CHECKING, List, Union
2020

21-
from playwright._api_types import FilePayload
21+
from playwright._impl._api_types import FilePayload
2222

2323
if TYPE_CHECKING: # pragma: no cover
24-
from playwright._element_handle import ElementHandle
25-
from playwright._page import Page
24+
from playwright._impl._element_handle import ElementHandle
25+
from playwright._impl._page import Page
2626

2727

2828
class FileChooser:

0 commit comments

Comments
 (0)