Skip to content

Commit

Permalink
style: format corrections
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed May 6, 2024
1 parent 406bf71 commit 6c64bcc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ pre-commit = "^3.2.2"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 90

[tool.ruff.lint]
select = ["E", "F", "C", "D"]
ignore = [
# Google Python Doc Style
"D203", "D204", "D213", "D215", "D400", "D401", "D404", "D406", "D407",
"D408", "D409", "D413",
]
per-file-ignores = {"**/{tests}/*" = ["F841", "D", "E501"]}

[tool.ruff.format]
docstring-code-format = true
10 changes: 6 additions & 4 deletions socketdock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ async def socket_send(request: Request, connectionid: str):
return text("FAIL", status=500)

socket = active_connections[connectionid]
if request.headers["content-type"] == 'text/plain':
if request.headers["content-type"] == "text/plain":
await socket.send(request.body.decode())
else:
await socket.send(request.body)
return text("OK")


@api.post("/socket/<connectionid>/disconnect")
async def socket_disconnect(request: Request, connectionid: str):
"""Disconnect a socket."""
Expand All @@ -70,6 +71,7 @@ async def socket_disconnect(request: Request, connectionid: str):
await socket.close()
return text("OK")


@api.websocket("/ws")
async def socket_handler(request: Request, websocket: Websocket):
"""Handle a new websocket connection."""
Expand All @@ -87,12 +89,12 @@ async def socket_handler(request: Request, websocket: Websocket):
LOGGER.info("Request headers: %s", dict(request.headers.items()))

await backend.socket_connected(
{
{
"connection_id": socket_id,
"headers": dict(request.headers.items()),
"send": f"{endpoint_var.get()}/socket/{socket_id}/send",
"disconnect": f"{endpoint_var.get()}/socket/{socket_id}/disconnect",
},
},
)

async for message in websocket:
Expand All @@ -101,7 +103,7 @@ async def socket_handler(request: Request, websocket: Websocket):
{
"connection_id": socket_id,
"send": f"{endpoint_var.get()}/socket/{socket_id}/send",
"disconnect": f"{endpoint_var.get()}/socket/{socket_id}/disconnect",
"disconnect": f"{endpoint_var.get()}/socket/{socket_id}/disconnect",
},
message,
)
Expand Down
4 changes: 1 addition & 3 deletions socketdock/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Backend(ABC):
"""Backend interface for SocketDock."""

@abstractmethod
async def socket_connected(
self, callback_uris: dict
):
async def socket_connected(self, callback_uris: dict):
"""Handle new socket connections, with calback provided."""
raise NotImplementedError()

Expand Down
11 changes: 2 additions & 9 deletions socketdock/httpbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ def __init__(self, connect_uri: str, message_uri: str, disconnect_uri: str):
self._message_uri = message_uri
self._disconnect_uri = disconnect_uri

async def socket_connected(
self, callback_uris: dict
):
async def socket_connected(self, callback_uris: dict):
"""Handle inbound socket message, with calback provided."""

http_body = {
"meta": callback_uris,
}
Expand All @@ -43,12 +40,9 @@ async def inbound_socket_message(
self, callback_uris: dict, message: Union[str, bytes]
):
"""Handle inbound socket message, with calback provided."""

http_body = {
"meta": callback_uris,
"message": message.decode("utf-8")
if isinstance(message, bytes)
else message,
"message": message.decode("utf-8") if isinstance(message, bytes) else message,
}

async with aiohttp.ClientSession() as session:
Expand All @@ -62,7 +56,6 @@ async def inbound_socket_message(

async def socket_disconnected(self, bundle: dict):
"""Handle socket disconnected."""

async with aiohttp.ClientSession() as session:
LOGGER.info("Notifying of disconnect: %s %s", self._disconnect_uri, bundle)
async with session.post(self._disconnect_uri, json=bundle) as resp:
Expand Down
4 changes: 1 addition & 3 deletions socketdock/testbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
class TestBackend(Backend):
"""Test backend for SocketDock."""

async def socket_connected(
self, callback_uris: dict
):
async def socket_connected(self, callback_uris: dict):
# This test method doesn't care, but can be useful to clean up state.
pass

Expand Down

0 comments on commit 6c64bcc

Please sign in to comment.