From c69b2a1d2ebe8942de47c4b81077cf9e9639ad97 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 13 Oct 2021 14:27:40 +0530 Subject: [PATCH] chore: jsonpipe part1 --- playwright/_impl/_browser_type.py | 2 +- playwright/_impl/_connection.py | 44 +++++++++++------------- playwright/async_api/_context_manager.py | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/playwright/_impl/_browser_type.py b/playwright/_impl/_browser_type.py index bc5a22d46..97d8ede3e 100644 --- a/playwright/_impl/_browser_type.py +++ b/playwright/_impl/_browser_type.py @@ -189,7 +189,7 @@ async def connect( ) connection._is_sync = self._connection._is_sync connection._loop.create_task(connection.run()) - playwright_future = connection.get_playwright_future() + playwright_future = connection.playwright_future timeout_future = throw_on_timeout(timeout, Error("Connection timed out")) done, pending = await asyncio.wait( diff --git a/playwright/_impl/_connection.py b/playwright/_impl/_connection.py index 6d5162c1e..9a2871a03 100644 --- a/playwright/_impl/_connection.py +++ b/playwright/_impl/_connection.py @@ -151,7 +151,7 @@ def __init__( ) -> None: self._dispatcher_fiber = dispatcher_fiber self._transport = transport - self._transport.on_message = lambda msg: self._dispatch(msg) + self._transport.on_message = lambda msg: self.dispatch(msg) self._waiting_for_object: Dict[str, Callable[[ChannelOwner], None]] = {} self._last_id = 0 self._objects: Dict[str, ChannelOwner] = {} @@ -160,7 +160,7 @@ def __init__( self._is_sync = False self._child_ws_connections: List["Connection"] = [] self._loop = loop - self._playwright_future: asyncio.Future["Playwright"] = loop.create_future() + self.playwright_future: asyncio.Future["Playwright"] = loop.create_future() self._error: Optional[BaseException] = None async def run_as_sync(self) -> None: @@ -172,15 +172,12 @@ async def run(self) -> None: self._root_object = RootChannelOwner(self) async def init() -> None: - self._playwright_future.set_result(await self._root_object.initialize()) + self.playwright_future.set_result(await self._root_object.initialize()) await self._transport.connect() self._loop.create_task(init()) await self._transport.run() - def get_playwright_future(self) -> asyncio.Future: - return self._playwright_future - def stop_sync(self) -> None: self._transport.request_stop() self._dispatcher_fiber.switch() @@ -216,18 +213,18 @@ def _send_message_to_server( if api_name: metadata["apiName"] = api_name - message = dict( - id=id, - guid=guid, - method=method, - params=self._replace_channels_with_guids(params, "params"), - metadata=metadata, - ) + message = { + "id": id, + "guid": guid, + "method": method, + "params": self._replace_channels_with_guids(params), + "metadata": metadata, + } self._transport.send(message) self._callbacks[id] = callback return callback - def _dispatch(self, msg: ParsedMessagePayload) -> None: + def dispatch(self, msg: ParsedMessagePayload) -> None: id = msg.get("id") if id: callback = self._callbacks.pop(id) @@ -280,21 +277,22 @@ def _create_remote_object( self._waiting_for_object.pop(guid)(result) return result - def _replace_channels_with_guids(self, payload: Any, param_name: str) -> Any: + def _replace_channels_with_guids( + self, + payload: Any, + ) -> Any: if payload is None: return payload if isinstance(payload, Path): return str(payload) if isinstance(payload, list): - return list( - map(lambda p: self._replace_channels_with_guids(p, "index"), payload) - ) + return list(map(self._replace_channels_with_guids, payload)) if isinstance(payload, Channel): return dict(guid=payload._guid) if isinstance(payload, dict): result = {} - for key in payload: - result[key] = self._replace_channels_with_guids(payload[key], key) + for key, value in payload.items(): + result[key] = self._replace_channels_with_guids(value) return result return payload @@ -302,13 +300,13 @@ def _replace_guids_with_channels(self, payload: Any) -> Any: if payload is None: return payload if isinstance(payload, list): - return list(map(lambda p: self._replace_guids_with_channels(p), payload)) + return list(map(self._replace_guids_with_channels, payload)) if isinstance(payload, dict): if payload.get("guid") in self._objects: return self._objects[payload["guid"]]._channel result = {} - for key in payload: - result[key] = self._replace_guids_with_channels(payload[key]) + for key, value in payload.items(): + result[key] = self._replace_guids_with_channels(value) return result return payload diff --git a/playwright/async_api/_context_manager.py b/playwright/async_api/_context_manager.py index 516454782..1b40ad2f1 100644 --- a/playwright/async_api/_context_manager.py +++ b/playwright/async_api/_context_manager.py @@ -35,7 +35,7 @@ async def __aenter__(self) -> AsyncPlaywright: loop, ) loop.create_task(self._connection.run()) - playwright_future = self._connection.get_playwright_future() + playwright_future = self._connection.playwright_future done, pending = await asyncio.wait( {self._connection._transport.on_error_future, playwright_future},