Skip to content

Commit

Permalink
linting/workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
circuitsacul committed Dec 8, 2022
1 parent ab8e326 commit 4d015bd
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
pull_request:

jobs:
ci:
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]
os: [ ubuntu, windows ]

name: ${{ matrix.python-version }} ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Nox
run: pip install nox
- name: Run Nox
run: nox
25 changes: 25 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: pypi

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.8'
- name: Install Dependencies
run: pip install poetry
- name: Build & Upload
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: |
poetry config pypi-token.pypi $PYPI_TOKEN
poetry version $(git describe --tags --abbrev=0)
poetry build
poetry publish
4 changes: 2 additions & 2 deletions example/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from socketapp import Client


async def send(client: Client):
async def send(client: Client) -> None:
while True:
await asyncio.sleep(5)
await client.send(Message(data="hi"), client.clients)


async def main(client: Client):
async def main(client: Client) -> None:
asyncio.create_task(send(client))
await client.run()

Expand Down
21 changes: 21 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nox


@nox.session
def mypy(session: nox.Session) -> None:
session.install("poetry")
session.run("poetry", "install")

session.run("mypy", ".")


@nox.session
def black(session: nox.Session) -> None:
session.install("black")
session.run("black", ".", "--check")


@nox.session
def ruff(session: nox.Session) -> None:
session.install("ruff")
session.run("ruff", ".")
15 changes: 14 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ python = "^3.8"
websockets = "^10.4"
pydantic = "^1.10.2"


[tool.poetry.group.dev.dependencies]
ruff = "^0.0.168"
nox = "^2022.11.21"
Expand All @@ -21,3 +20,17 @@ mypy = "^0.991"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
warn_return_any=true
warn_unused_configs=true
strict=true

[tool.black]
skip-magic-trailing-comma=true

[tool.ruff]
extend-select = [
# isort
"I001"
]
14 changes: 3 additions & 11 deletions socketapp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

class Client:
def __init__(
self,
host: str = "localhost",
port: int = 5000,
password: str | None = None,
self, host: str = "localhost", port: int = 5000, password: str | None = None
) -> None:
self.host = host
self.port = port
Expand Down Expand Up @@ -61,17 +58,12 @@ async def send(self, event: Event, to: t.Collection[int]) -> None:
if not await event.process_client(self, self.client_id):
return

raw = json.dumps(
{
"to": list(to),
"event": event.to_dict(),
}
)
raw = json.dumps({"to": list(to), "event": event.to_dict()})
await self.ws.send(raw)

async def _handshake(self, ws: WebSocketClientProtocol) -> int:
await ws.send(json.dumps({"password": self.password}))
return json.loads(await ws.recv())["client_id"]
return int(json.loads(await ws.recv())["client_id"])

async def _process_messages(self, ws: WebSocketClientProtocol) -> None:
try:
Expand Down
2 changes: 1 addition & 1 deletion socketapp/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def to_dict(self) -> dict[str, t.Any]:
dct["cls_id"] = self.event_id
return dct

def to_json(self) -> str: # type: ignore
def to_json(self) -> str:
return json.dumps(self.to_dict())

@staticmethod
Expand Down
12 changes: 2 additions & 10 deletions socketapp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

class Server:
def __init__(
self,
host: str = "localhost",
port: int = 5000,
password: str | None = None,
self, host: str = "localhost", port: int = 5000, password: str | None = None
) -> None:
self.host = host
self.port = port
Expand Down Expand Up @@ -106,12 +103,7 @@ async def _process_msg(self, msg: str | bytes, client_id: int) -> None:
if not ret:
return

to_send = json.dumps(
{
"author_id": client_id,
"event": event.to_dict(),
}
)
to_send = json.dumps({"author_id": client_id, "event": event.to_dict()})

await asyncio.gather(
*(ws.send(to_send) for (cid, ws) in self.clients.items() if cid in to),
Expand Down

0 comments on commit 4d015bd

Please sign in to comment.