Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Servers: Common implementation for both TCP and UDP servers #322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions docs/source/_extensions/sphinx_easynetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@

v0.1.0: Initial
v0.1.1: Fix base is not replaced if the class is generic.
v0.2.0 (current): Log when an object does not have a docstring.
v0.2.0: Log when an object does not have a docstring.
v0.2.1 (current): Add base class to replace.
"""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any, get_origin
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, get_args, get_origin

if TYPE_CHECKING:
from sphinx.application import Sphinx

from easynetwork.servers._base import BaseStandaloneNetworkServerImpl
from easynetwork.servers.abc import AbstractNetworkServer
from easynetwork.servers._base import BaseAsyncNetworkServerImpl, BaseStandaloneNetworkServerImpl
from easynetwork.servers.abc import AbstractAsyncNetworkServer, AbstractNetworkServer

logger = logging.getLogger(__name__)


def _replace_base_in_place(klass: type, bases: list[type], base_to_replace: type, base_to_set_instead: type) -> None:
def _replace_base_in_place(
klass: type,
bases: list[type],
base_to_replace: type,
base_to_set_instead: Callable[[tuple[Any, ...]], Any],
) -> None:
if issubclass(klass, base_to_replace):
for index, base in enumerate(bases):
if get_origin(base) is base_to_replace:
bases[index] = base_to_set_instead
if base is base_to_replace or get_origin(base) is base_to_replace:
bases[index] = base_to_set_instead(get_args(base))


def autodoc_process_bases(app: Sphinx, name: str, obj: type, options: dict[str, Any], bases: list[type]) -> None:
_replace_base_in_place(obj, bases, BaseStandaloneNetworkServerImpl, AbstractNetworkServer)
_replace_base_in_place(obj, bases, BaseAsyncNetworkServerImpl, lambda _: AbstractAsyncNetworkServer)
_replace_base_in_place(obj, bases, BaseStandaloneNetworkServerImpl, lambda _: AbstractNetworkServer)


def _is_magic_method(name: str) -> bool:
Expand All @@ -47,7 +55,7 @@ def setup(app: Sphinx) -> dict[str, Any]:
app.connect("autodoc-process-docstring", autodoc_process_docstring)

return {
"version": "0.2.0",
"version": "0.2.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def main() -> None:
await client(host, port, "Hello world 3")

await server.shutdown()
await server_task


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async def main() -> None:
await client(host, port, "Hello world 3")

await server.shutdown()
await server_task


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async def main() -> None:
await client(host, port, "Hello world 3")

await server.shutdown()
await server_task


if __name__ == "__main__":
Expand Down
17 changes: 17 additions & 0 deletions src/easynetwork/lowlevel/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

__all__ = [
"ElapsedTime",
"Flag",
"WarnCallback",
"adjust_leftover_buffer",
"check_real_socket_state",
Expand Down Expand Up @@ -395,6 +396,22 @@ def __call__(
) -> None: ...


class Flag:
__slots__ = ("__value", "__weakref__")

def __init__(self) -> None:
self.__value: bool = False

def is_set(self) -> bool:
return self.__value

def set(self) -> None:
self.__value = True

def clear(self) -> None:
self.__value = False


class ElapsedTime:
__slots__ = ("_current_time_func", "_start_time", "_end_time")

Expand Down
Loading