Skip to content

Commit

Permalink
black format python
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Jul 14, 2024
1 parent 924e461 commit a8b6740
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
7 changes: 5 additions & 2 deletions python/imjoy_rpc/hypha/pyodide_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class LocalWebSocket {
}
"""


class PyodideWebsocketRPCConnection:
"""Represent a pyodide websocket RPC connection."""

Expand All @@ -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}"
Expand All @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions python/imjoy_rpc/hypha/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions python/imjoy_rpc/hypha/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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."""

Expand Down
28 changes: 14 additions & 14 deletions python/imjoy_rpc/hypha/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit a8b6740

Please sign in to comment.