Skip to content

Commit

Permalink
Finalized all checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rhjdjong committed Mar 4, 2024
1 parent 6563c2d commit b2da1de
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 318 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: test

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

Expand Down Expand Up @@ -35,8 +34,14 @@ jobs:
- name: Install Hatch
run: pip install --upgrade hatch

- name: Run style check
run: hatch run style:check

- name: Check types
run: hatch run types:check

- name: Run static analysis
run: hatch fmt --check

- name: Run tests
run: hatch run cov
run: hatch run test:cov
17 changes: 12 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: INP001

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
Expand All @@ -10,8 +12,10 @@


project = "SlipLib"
copyright = "2024, Ruud de Jong"
copyright = "2024, Ruud de Jong" # noqa: A001
author = "Ruud de Jong"
github_username = "rhjdjong"
github_repository = "https://github.com/rhjdjong/SlipLib/"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand All @@ -25,7 +29,13 @@
"sphinx.ext.autodoc.typehints",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx_rtd_theme",
"sphinx_tabs.tabs",
"sphinx_toolbox.more_autodoc.autoprotocol",
"sphinx_toolbox.more_autodoc.typehints",
"sphinx_toolbox.more_autodoc.typevars",
"sphinx_toolbox.more_autodoc.variables",
"sphinx_autodoc_typehints",
]

Expand All @@ -36,10 +46,7 @@
# napoleon_use_rtype = False
autoclass_content = "both"
autodoc_typehints = "description"
autodoc_type_aliases = {
"IPAddress": "IPAddress",
"Tuple[SlipSocket, IPAddress]": "Tuple[SlipSocket, IPAddress]",
}
autodoc_type_aliases = {}
add_module_names = False
intersphinx_mapping = {"python": ("https://docs.python.org/3", None)}

Expand Down
14 changes: 8 additions & 6 deletions examples/echoserver/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: T201

# Copyright (c) 2020 Ruud de Jong
# This file is part of the SlipLib project which is released under the MIT license.
# See https://github.com/rhjdjong/SlipLib for details.
Expand All @@ -13,25 +15,25 @@
This is repeated until the user enters an empty message.
"""

# ruff: noqa: F201
# ruff: noqa: T201
import sys

import sliplib

if __name__ == '__main__':
if __name__ == "__main__":
if len(sys.argv) != 2: # noqa: PLR2004
print("Usage: python client.py <port>")
sys.exit(1)
port = sys.argv[1]
print(f"Connecting to server on port {port}")
sock = sliplib.SlipSocket.create_connection(('localhost', int(port)))
sock = sliplib.SlipSocket.create_connection(("localhost", int(port)))
print(f"Connected to {sock.getpeername()}")

while True:
message = input('Message>')
message = input("Message>")
if not message:
break
b_message = bytes(message, 'utf-8')
b_message = bytes(message, "utf-8")
sock.send_msg(b_message)
b_reply = sock.recv_msg()
print('Response:', b_reply)
print("Response:", b_reply)
44 changes: 27 additions & 17 deletions examples/echoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,72 @@

# ruff: noqa: T201

from __future__ import annotations

import socket
import sys
from socketserver import TCPServer
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
if sys.version_info >= (3, 12): # noqa: UP036
from collections.abc import Buffer
else:
from typing_extensions import Buffer

from _socket import dup # type: ignore
from _socket import dup

from sliplib import SlipRequestHandler


class _ChattySocket(socket.socket):
"""A socket subclass that prints the raw data that is received and sent."""

def __init__(self, sock):
def __init__(self, sock: socket.socket) -> None:
fd = dup(sock.fileno())
super().__init__(sock.family, sock.type, sock.proto, fileno=fd)
super().settimeout(sock.gettimeout())

def recv(self, chunksize):
data = super().recv(chunksize)
print('Raw data received:', data)
def recv(self, chunksize: int, *args: Any) -> bytes:
data = super().recv(chunksize, *args)
print("Raw data received:", data)
return data

def sendall(self, data):
print('Sending raw data:', data)
super().sendall(data)
def sendall(self, data: Buffer, *args: Any) -> None:
print("Sending raw data:", data)
super().sendall(data, *args)


class SlipHandler(SlipRequestHandler):
"""A SlipRequestHandler that echoes the received message with the bytes in reversed order."""

def setup(self):
def setup(self) -> None:
self.request = _ChattySocket(self.request)
print(f"Incoming connection from {self.request.getpeername()}")
super().setup()

# Dedicated handler to show the encoded bytes.
def handle(self):
def handle(self) -> None:
while True:
message = self.request.recv_msg()
print('Decoded data:', message)
print("Decoded data:", message)
if message:
self.request.send_msg(bytes(reversed(message)))
else:
print('Closing down')
print("Closing down")
break


class TCPServerIPv6(TCPServer):
"""An IPv6 TCPServer"""

address_family = socket.AF_INET6


if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1].lower() == 'ipv6':
server = TCPServerIPv6(('localhost', 0), SlipHandler) # type: TCPServer
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1].lower() == "ipv6":
server = TCPServerIPv6(("localhost", 0), SlipHandler) # type: TCPServer
else:
server = TCPServer(('localhost', 0), SlipHandler)
print('Slip server listening on localhost, port', server.server_address[1])
server = TCPServer(("localhost", 0), SlipHandler)
print("Slip server listening on localhost, port", server.server_address[1])
server.handle_request()
37 changes: 26 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ dependencies = []
path = "src/sliplib/version.py"

[tool.hatch.envs.default]
python = "3.12"
dependencies = [
"coverage[toml]>=6.5",
"pytest",
"pytest-mock",
]

[tool.hatch.envs.default.scripts]
[[tool.hatch.envs.test.matrix]]
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]

[tool.hatch.envs.test]
extra-dependencies = [
"coverage[toml]>=6.5",
]

[tool.hatch.envs.test.scripts]
test = "pytest {args:tests}"
test-cov = "coverage run -m pytest {args:tests}"
cov-report = [
Expand All @@ -53,18 +61,17 @@ cov = [
"cov-report",
]

[[tool.hatch.envs.all.matrix]]
[[tool.hatch.envs.types.matrix]]
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]

[tool.hatch.envs.types]
dependencies = [
"pytest",
"pytest-mock",
extra-dependencies = [
"mypy>=1.0.0",
"typing_extensions"
]

[tool.hatch.envs.types.scripts]
check = "mypy --strict --install-types --non-interactive {args:src/sliplib tests}"
check = "mypy --strict --install-types --non-interactive {args:src tests examples}"

[tool.hatch.envs.style]
dependencies = [
Expand All @@ -73,10 +80,10 @@ dependencies = [
]

[tool.hatch.envs.style.scripts]
isort-check = "isort --check-only {args:src tests}"
black-check = "black --check {args:src tests}"
isort-fix = "isort {args:src tests}"
black-fix = "black {args:src tests}"
isort-check = "isort --check-only {args:src tests examples docs}"
black-check = "black --check {args:src tests examples docs}"
isort-fix = "isort {args:src tests examples docs}"
black-fix = "black {args:src tests examples docs}"
check = [
"- isort-check",
"black-check",
Expand Down Expand Up @@ -116,10 +123,18 @@ python = "3.12"
dependencies = [
"sphinx",
"sphinx_rtd_theme",
"sphinx-toolbox",
]

[tool.hatch.envs.doc.scripts]
build = "cd {root}/docs && make {args:html}"

[tool.black]
line-length = 120

[tool.isort]
profile = "black"
line_length = 120

[tool.ruff]
target-version = "py312"
12 changes: 5 additions & 7 deletions src/sliplib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@
.. autoexception:: ProtocolError
"""

from .slip import *
from .sliprequesthandler import *
from .slipsocket import *
from .slipstream import *
from .slipwrapper import *
from .version import __version__
from sliplib.slip import END, ESC, ESC_END, ESC_ESC, Driver, ProtocolError, decode, encode, is_valid
from sliplib.sliprequesthandler import SlipRequestHandler
from sliplib.slipsocket import SlipSocket
from sliplib.slipstream import SlipStream
from sliplib.slipwrapper import SlipWrapper

__all__ = [
"encode",
Expand All @@ -97,5 +96,4 @@
"ESC",
"ESC_END",
"ESC_ESC",
"__version__",
]
Loading

0 comments on commit b2da1de

Please sign in to comment.