From a8b6740d9dde9bf5bddf8ca24daa6e12cb4821b7 Mon Sep 17 00:00:00 2001 From: Wei Ouyang Date: Sat, 13 Jul 2024 23:02:45 -0700 Subject: [PATCH] black format python --- python/imjoy_rpc/hypha/pyodide_websocket.py | 7 ++++-- python/imjoy_rpc/hypha/rpc.py | 6 +++-- python/imjoy_rpc/hypha/utils.py | 2 ++ python/imjoy_rpc/hypha/websocket_client.py | 28 ++++++++++----------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/python/imjoy_rpc/hypha/pyodide_websocket.py b/python/imjoy_rpc/hypha/pyodide_websocket.py index 6f2bee68..0ed31c37 100644 --- a/python/imjoy_rpc/hypha/pyodide_websocket.py +++ b/python/imjoy_rpc/hypha/pyodide_websocket.py @@ -107,6 +107,7 @@ class LocalWebSocket { } """ + class PyodideWebsocketRPCConnection: """Represent a pyodide websocket RPC connection.""" @@ -117,7 +118,7 @@ def __init__( self._websocket = None self._handle_message = None assert server_url and client_id - + server_url = server_url + f"?client_id={client_id}" if workspace is not None: server_url += f"&workspace={workspace}" @@ -139,7 +140,9 @@ async def open(self): if self._server_url.startswith("wss://local-hypha-server:"): js.console.log("Connecting to local websocket " + self._server_url) LocalWebSocket = js.eval("(" + local_websocket_patch + ")") - self._websocket = LocalWebSocket.new(self._server_url, self._client_id, self._workspace) + self._websocket = LocalWebSocket.new( + self._server_url, self._client_id, self._workspace + ) else: self._websocket = WebSocket.new(self._server_url) self._websocket.binaryType = "arraybuffer" diff --git a/python/imjoy_rpc/hypha/rpc.py b/python/imjoy_rpc/hypha/rpc.py index abd8a098..27cfb132 100644 --- a/python/imjoy_rpc/hypha/rpc.py +++ b/python/imjoy_rpc/hypha/rpc.py @@ -703,7 +703,7 @@ async def _send_chunks(self, package, target_id, session_id): ) logger.info("All chunks sent (%d)", chunk_num) await message_cache.process(message_id, bool(session_id)) - + def emit(self, main_message, extra_data=None): """Emit a message.""" assert isinstance(main_message, dict) and "type" in main_message @@ -801,7 +801,9 @@ def pfunc(resolve, reject): # However, if the args contains _rintf === true, we will not clear the session clear_after_called = True for arg in args: - if (isinstance(arg, dict) and arg.get("_rintf")) or (hasattr(arg, "_rintf") and arg._rintf == True): + if (isinstance(arg, dict) and arg.get("_rintf")) or ( + hasattr(arg, "_rintf") and arg._rintf == True + ): clear_after_called = False break extra_data["promise"] = self._encode_promise( diff --git a/python/imjoy_rpc/hypha/utils.py b/python/imjoy_rpc/hypha/utils.py index 2d3b7252..23a2fa7e 100644 --- a/python/imjoy_rpc/hypha/utils.py +++ b/python/imjoy_rpc/hypha/utils.py @@ -24,6 +24,7 @@ def generate_password(length=50): _hash_id = generate_password() + def recursive_hash(obj): """Generate a hash for nested dictionaries and lists.""" if isinstance(obj, collections.abc.Hashable) and not isinstance(obj, dotdict): @@ -35,6 +36,7 @@ def recursive_hash(obj): else: raise TypeError(f"Unsupported type: {type(obj)}") + class dotdict(dict): # pylint: disable=invalid-name """Access dictionary attributes with dot.notation.""" diff --git a/python/imjoy_rpc/hypha/websocket_client.py b/python/imjoy_rpc/hypha/websocket_client.py index 2c0b6d0b..0687dd70 100644 --- a/python/imjoy_rpc/hypha/websocket_client.py +++ b/python/imjoy_rpc/hypha/websocket_client.py @@ -252,7 +252,7 @@ async def disconnect(): """Disconnect the rpc and server connection.""" await rpc.disconnect() await connection.disconnect() - + wm.config = dotdict(wm.config) wm.config["client_id"] = client_id wm.export = export @@ -267,7 +267,7 @@ def emit_msg(message): assert "type" in message, "message must have a 'type' field" assert message["type"] != "method", "message type cannot be 'method'" return rpc.emit(message) - + def on_msg(type, handler): assert type != "method", "message type cannot be 'method'" rpc.on(type, handler) @@ -329,6 +329,7 @@ async def get_service(query, webrtc=None, webrtc_config=None): def setup_local_client(enable_execution=False, on_ready=None): fut = asyncio.Future() + async def message_handler(event): data = event.data.to_py() type = data.get("type") @@ -349,14 +350,16 @@ async def message_handler(event): print("server_url should start with https://local-hypha-server:") return - server = await connect_to_server({ - "server_url": server_url, - "workspace": workspace, - "client_id": client_id, - "token": token, - "method_timeout": method_timeout, - "name": name - }) + server = await connect_to_server( + { + "server_url": server_url, + "workspace": workspace, + "client_id": client_id, + "token": token, + "method_timeout": method_timeout, + "name": name, + } + ) js.globalThis.api = server try: @@ -365,10 +368,7 @@ async def message_handler(event): if on_ready: await on_ready(server, config) except Exception as e: - await server.update_client_info({ - "id": client_id, - "error": str(e) - }) + await server.update_client_info({"id": client_id, "error": str(e)}) fut.set_exception(e) return fut.set_result(server)