|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import asyncio |
16 | | -import gzip |
17 | | -import os |
18 | | -import shutil |
19 | | -import stat |
20 | | -import sys |
21 | | -import subprocess |
22 | | - |
23 | | -from playwright.connection import Connection |
24 | | -from playwright.object_factory import create_remote_object |
| 15 | +from playwright.connection import Channel, ChannelOwner, ConnectionScope, from_channel |
25 | 16 | from playwright.browser_type import BrowserType |
26 | 17 | from typing import Dict |
27 | 18 |
|
28 | | -class Playwright: |
29 | | - def __init__(self) -> None: |
30 | | - # Use ProactorEventLoop in 3.7, which is default in 3.8 |
31 | | - if sys.platform == 'win32': |
32 | | - loop = asyncio.ProactorEventLoop() |
33 | | - asyncio.set_event_loop(loop) |
34 | | - self.loop = asyncio.get_event_loop() |
35 | | - self.loop.run_until_complete(self._sync_init()) |
36 | | - |
37 | | - async def _sync_init(self): |
38 | | - package_path = os.path.dirname(os.path.abspath(__file__)) |
39 | | - platform = sys.platform |
40 | | - if platform == 'darwin': |
41 | | - driver_name = 'driver-macos' |
42 | | - elif platform == 'linux': |
43 | | - driver_name = 'driver-linux' |
44 | | - elif platform == 'win32': |
45 | | - driver_name = 'driver-win.exe' |
46 | | - driver_executable = os.path.join(package_path, driver_name) |
47 | | - archive_name = os.path.join(package_path, 'drivers', driver_name + '.gz') |
48 | | - |
49 | | - if not os.path.exists(driver_executable) or os.path.getmtime(driver_executable) < os.path.getmtime(archive_name): |
50 | | - with gzip.open(archive_name, 'rb') as f_in, open(driver_executable, 'wb') as f_out: |
51 | | - shutil.copyfileobj(f_in, f_out) |
52 | | - |
53 | | - st = os.stat(driver_executable) |
54 | | - if st.st_mode & stat.S_IEXEC == 0: |
55 | | - os.chmod(driver_executable, st.st_mode | stat.S_IEXEC) |
56 | | - |
57 | | - subprocess.run(f"{driver_executable} install", shell=True) |
| 19 | +class Playwright(ChannelOwner): |
58 | 20 |
|
59 | | - self._proc = await asyncio.create_subprocess_exec(driver_executable, |
60 | | - stdin=asyncio.subprocess.PIPE, |
61 | | - stdout=asyncio.subprocess.PIPE, |
62 | | - stderr=asyncio.subprocess.PIPE, |
63 | | - limit=32768) |
64 | | - self._connection = Connection(self._proc.stdout, self._proc.stdin, create_remote_object, self.loop) |
65 | | - chromium, firefox, webkit = await asyncio.gather( |
66 | | - self._connection.wait_for_object_with_known_name('chromium'), |
67 | | - self._connection.wait_for_object_with_known_name('firefox'), |
68 | | - self._connection.wait_for_object_with_known_name('webkit')) |
69 | | - self.chromium: BrowserType = chromium |
70 | | - self.firefox: BrowserType = firefox |
71 | | - self.webkit: BrowserType = webkit |
72 | | - self.browser_types: Dict[str, BrowserType] = dict(chromium=self.chromium, firefox=self.firefox, webkit=self.webkit) |
| 21 | + def __init__(self, scope: ConnectionScope, guid: str, initializer: Dict) -> None: |
| 22 | + super().__init__(scope, guid, initializer) |
| 23 | + self.chromium: BrowserType = from_channel(initializer['chromium']) |
| 24 | + self.firefox: BrowserType = from_channel(initializer['firefox']) |
| 25 | + self.webkit: BrowserType = from_channel(initializer['webkit']) |
| 26 | + self.devices = initializer['deviceDescriptors'] |
| 27 | + self.browser_types: Dict[str, BrowserType] = dict(chromium=self.chromium, webkit=self.webkit, firefox=self.firefox) |
73 | 28 |
|
74 | | -playwright = Playwright() |
0 commit comments