cyengine 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cyengine-0.1.0/LICENSE +21 -0
- cyengine-0.1.0/MANIFEST.in +7 -0
- cyengine-0.1.0/PKG-INFO +46 -0
- cyengine-0.1.0/README.md +20 -0
- cyengine-0.1.0/cyengine/__init__.py +25 -0
- cyengine-0.1.0/cyengine/app.py +143 -0
- cyengine-0.1.0/cyengine/core/_middleware.pyx +0 -0
- cyengine-0.1.0/cyengine/core/_protocol.pyi +33 -0
- cyengine-0.1.0/cyengine/core/_protocol.pyx +442 -0
- cyengine-0.1.0/cyengine/core/_router.pyi +33 -0
- cyengine-0.1.0/cyengine/core/_router.pyx +237 -0
- cyengine-0.1.0/cyengine/core/_types.pyi +175 -0
- cyengine-0.1.0/cyengine/core/_types.pyx +547 -0
- cyengine-0.1.0/cyengine/errors.py +182 -0
- cyengine-0.1.0/cyengine/handler.py +86 -0
- cyengine-0.1.0/cyengine/limits.py +23 -0
- cyengine-0.1.0/cyengine/middleware.py +209 -0
- cyengine-0.1.0/cyengine/providers/_pool.pyx +0 -0
- cyengine-0.1.0/cyengine/providers/jwt.py +0 -0
- cyengine-0.1.0/cyengine/providers/kafka.py +0 -0
- cyengine-0.1.0/cyengine/providers/postgres.py +0 -0
- cyengine-0.1.0/cyengine/providers/redis.py +0 -0
- cyengine-0.1.0/cyengine/py.typed +0 -0
- cyengine-0.1.0/cyengine/routing.py +120 -0
- cyengine-0.1.0/cyengine/server.py +146 -0
- cyengine-0.1.0/cyengine.egg-info/PKG-INFO +46 -0
- cyengine-0.1.0/cyengine.egg-info/SOURCES.txt +44 -0
- cyengine-0.1.0/cyengine.egg-info/dependency_links.txt +1 -0
- cyengine-0.1.0/cyengine.egg-info/requires.txt +10 -0
- cyengine-0.1.0/cyengine.egg-info/top_level.txt +1 -0
- cyengine-0.1.0/pyproject.toml +54 -0
- cyengine-0.1.0/setup.cfg +4 -0
- cyengine-0.1.0/setup.py +36 -0
- cyengine-0.1.0/tests/test_access_log_cors.py +201 -0
- cyengine-0.1.0/tests/test_error_handlers.py +122 -0
- cyengine-0.1.0/tests/test_errors.py +116 -0
- cyengine-0.1.0/tests/test_groups.py +214 -0
- cyengine-0.1.0/tests/test_head_options.py +141 -0
- cyengine-0.1.0/tests/test_headers_cookies.py +178 -0
- cyengine-0.1.0/tests/test_http_basics.py +186 -0
- cyengine-0.1.0/tests/test_limits.py +124 -0
- cyengine-0.1.0/tests/test_pipelining.py +188 -0
- cyengine-0.1.0/tests/test_protocol_data.py +51 -0
- cyengine-0.1.0/tests/test_router.py +75 -0
- cyengine-0.1.0/tests/test_server.py +88 -0
- cyengine-0.1.0/tests/test_validation.py +158 -0
cyengine-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arnur
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cyengine-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cyengine
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Async HTTP framework for Python with a Cython core
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/Typesh1t1337/CyEngine
|
|
7
|
+
Project-URL: Repository, https://github.com/Typesh1t1337/CyEngine
|
|
8
|
+
Project-URL: Issues, https://github.com/Typesh1t1337/CyEngine/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Cython
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Operating System :: MacOS
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: httptools
|
|
20
|
+
Requires-Dist: orjson
|
|
21
|
+
Provides-Extra: uvloop
|
|
22
|
+
Requires-Dist: uvloop; sys_platform != "win32" and extra == "uvloop"
|
|
23
|
+
Provides-Extra: pydantic
|
|
24
|
+
Requires-Dist: pydantic>=2; extra == "pydantic"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# CyEngine
|
|
28
|
+
|
|
29
|
+
Async HTTP framework for Python with a Cython core.
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install cyengine
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from cyengine import App, Request, Response
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
async def hello(request: Request) -> Response:
|
|
40
|
+
return Response.text("Hello, world!")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
app = App()
|
|
44
|
+
app.handle("GET", "/", hello)
|
|
45
|
+
app.run(port=8000)
|
|
46
|
+
```
|
cyengine-0.1.0/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# CyEngine
|
|
2
|
+
|
|
3
|
+
Async HTTP framework for Python with a Cython core.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install cyengine
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from cyengine import App, Request, Response
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def hello(request: Request) -> Response:
|
|
14
|
+
return Response.text("Hello, world!")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
app = App()
|
|
18
|
+
app.handle("GET", "/", hello)
|
|
19
|
+
app.run(port=8000)
|
|
20
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from cyengine.app import App
|
|
2
|
+
from cyengine.limits import Limits
|
|
3
|
+
from cyengine.routing import Group
|
|
4
|
+
from cyengine.errors import (
|
|
5
|
+
HTTPError, BadRequest, Unauthorized, Forbidden, NotFound, Conflict, UnprocessableEntity,
|
|
6
|
+
)
|
|
7
|
+
from cyengine.core._types import Request, Response, CyEngineError, RouterNotFound, MethodNotAllowed
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"App",
|
|
11
|
+
"Limits",
|
|
12
|
+
"Group",
|
|
13
|
+
"HTTPError",
|
|
14
|
+
"BadRequest",
|
|
15
|
+
"Unauthorized",
|
|
16
|
+
"Forbidden",
|
|
17
|
+
"NotFound",
|
|
18
|
+
"Conflict",
|
|
19
|
+
"UnprocessableEntity",
|
|
20
|
+
"Request",
|
|
21
|
+
"Response",
|
|
22
|
+
"CyEngineError",
|
|
23
|
+
"RouterNotFound",
|
|
24
|
+
"MethodNotAllowed",
|
|
25
|
+
]
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from typing import Any, Self, TypeVar, overload
|
|
4
|
+
|
|
5
|
+
from cyengine.core._router import Router
|
|
6
|
+
from cyengine.core._protocol import HttpProtocol
|
|
7
|
+
from cyengine.handler import Handler, HandlerFunc, HandlerFunction, Middleware, chain
|
|
8
|
+
from cyengine.routing import Group, Routes
|
|
9
|
+
from cyengine.limits import Limits
|
|
10
|
+
from cyengine.core._types import Request, Response
|
|
11
|
+
from cyengine.errors import ErrorHandlerFunc, ErrorHandlers, HTTPError, NotFound
|
|
12
|
+
|
|
13
|
+
from cyengine import server as _server
|
|
14
|
+
|
|
15
|
+
_E = TypeVar("_E", bound=Exception)
|
|
16
|
+
_ErrorF = TypeVar("_ErrorF", bound=ErrorHandlerFunc[Any])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class App(Routes):
|
|
20
|
+
"""
|
|
21
|
+
Registration of handlers and middleware + startup. Dependencies live in main():
|
|
22
|
+
|
|
23
|
+
async def main():
|
|
24
|
+
db = await Database.connect(dsn)
|
|
25
|
+
try:
|
|
26
|
+
app = App()
|
|
27
|
+
app.use(recover)
|
|
28
|
+
app.handle("GET", "/users/<int:id>", UserHandler(db))
|
|
29
|
+
await app.serve(port=8000)
|
|
30
|
+
finally:
|
|
31
|
+
await db.close()
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, limits: Limits | None = None) -> None:
|
|
35
|
+
self._limits = limits if limits is not None else Limits()
|
|
36
|
+
self._routes: list[tuple[str, str, Handler, Group | None]] = [] # (method, full path, …)
|
|
37
|
+
self._middleware: list[Middleware] = []
|
|
38
|
+
self._errors = ErrorHandlers()
|
|
39
|
+
self._router: Router | None = None # built in serve()
|
|
40
|
+
self._h404: HandlerFunction | None = None # 404/405 handlers, also built in serve()
|
|
41
|
+
self._h405: HandlerFunction | None = None
|
|
42
|
+
self._tasks: dict[asyncio.Task[None], float] = {} # request task → deadline (0 — none)
|
|
43
|
+
self._timed_out: set[asyncio.Task[Any]] = set() # cancelled due to handler_timeout
|
|
44
|
+
self._protocols: set[HttpProtocol] = set() # open connections
|
|
45
|
+
|
|
46
|
+
# ── registration ─────────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
def _add(self, methods: list[str], path: str, handler: Handler) -> None:
|
|
49
|
+
for method in methods:
|
|
50
|
+
self._routes.append((method, path, handler, None))
|
|
51
|
+
|
|
52
|
+
def _group_context(self) -> tuple["App", str, Group | None]:
|
|
53
|
+
return self, "", None
|
|
54
|
+
|
|
55
|
+
@overload
|
|
56
|
+
def error_handler(self, exc_type: type[_E], fn: ErrorHandlerFunc[_E]) -> Self: ...
|
|
57
|
+
@overload
|
|
58
|
+
def error_handler(self, exc_type: type[Exception],
|
|
59
|
+
fn: None = None) -> Callable[[_ErrorF], _ErrorF]: ...
|
|
60
|
+
|
|
61
|
+
def error_handler(self, exc_type: type[Exception],
|
|
62
|
+
fn: ErrorHandlerFunc[Any] | None = None) -> Any:
|
|
63
|
+
"""
|
|
64
|
+
What to return to the client if a handler raises an exception of this type.
|
|
65
|
+
|
|
66
|
+
async def not_found(request, exc):
|
|
67
|
+
return Response.json({"error": str(exc)}, status=404)
|
|
68
|
+
|
|
69
|
+
app.error_handler(UserNotFound, not_found)
|
|
70
|
+
|
|
71
|
+
@app.error_handler(ValidationError) # decorator
|
|
72
|
+
async def invalid(request, exc): ...
|
|
73
|
+
|
|
74
|
+
Applies to subclasses too; the most specific type wins.
|
|
75
|
+
error_handler(Exception, fn) — catch everything else instead of a 500.
|
|
76
|
+
"""
|
|
77
|
+
if fn is not None:
|
|
78
|
+
self._errors.add(exc_type, fn)
|
|
79
|
+
return self
|
|
80
|
+
|
|
81
|
+
def decorator(func: _ErrorF) -> _ErrorF:
|
|
82
|
+
self._errors.add(exc_type, func)
|
|
83
|
+
return func
|
|
84
|
+
return decorator
|
|
85
|
+
|
|
86
|
+
# ── startup ──────────────────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
def _build_router(self) -> Router:
|
|
89
|
+
"""Middleware chains are built once, at startup."""
|
|
90
|
+
router = Router()
|
|
91
|
+
for method, path, handler, group in self._routes:
|
|
92
|
+
mw = [*self._middleware, *(group._all_middleware() if group else ())]
|
|
93
|
+
router.add(method, path, chain(self._errors.wrap(handler), mw).handle)
|
|
94
|
+
|
|
95
|
+
# OPTIONS: if a path has no handler of its own — automatic 204 + Allow response.
|
|
96
|
+
# It goes through the same chain (incl. group middleware), so CORS etc. can see it.
|
|
97
|
+
async def options(request: Request) -> Response:
|
|
98
|
+
allow = ", ".join(router.allowed(request.path))
|
|
99
|
+
return Response.empty(204, headers={"Allow": allow})
|
|
100
|
+
|
|
101
|
+
options_wrapped = self._errors.wrap(HandlerFunc(options))
|
|
102
|
+
auto_cache: dict[tuple[int, ...], HandlerFunction] = {} # one chain per set of middleware
|
|
103
|
+
by_path: dict[str, list[Any]] = {} # path → [methods, group]
|
|
104
|
+
for method, path, _, group in self._routes:
|
|
105
|
+
entry = by_path.setdefault(path, [set(), group])
|
|
106
|
+
entry[0].add(method)
|
|
107
|
+
for path, (methods, group) in by_path.items():
|
|
108
|
+
if "OPTIONS" in methods:
|
|
109
|
+
continue
|
|
110
|
+
mw = [*self._middleware, *(group._all_middleware() if group else ())]
|
|
111
|
+
key = tuple(id(m) for m in mw)
|
|
112
|
+
if key not in auto_cache:
|
|
113
|
+
auto_cache[key] = chain(options_wrapped, mw).handle
|
|
114
|
+
router.add("OPTIONS", path, auto_cache[key])
|
|
115
|
+
|
|
116
|
+
# 404/405 are handlers like any other: middleware and error handlers apply to them
|
|
117
|
+
async def not_found(request: Request) -> Response:
|
|
118
|
+
raise NotFound()
|
|
119
|
+
|
|
120
|
+
async def method_not_allowed(request: Request) -> Response:
|
|
121
|
+
allow = ", ".join(router.allowed(request.path))
|
|
122
|
+
raise HTTPError(405, headers={"Allow": allow})
|
|
123
|
+
|
|
124
|
+
for name, fn in (("_h404", not_found), ("_h405", method_not_allowed)):
|
|
125
|
+
wrapped = self._errors.wrap(HandlerFunc(fn))
|
|
126
|
+
setattr(self, name, chain(wrapped, self._middleware).handle)
|
|
127
|
+
return router
|
|
128
|
+
|
|
129
|
+
def _make_protocol(self, loop: asyncio.AbstractEventLoop) -> HttpProtocol:
|
|
130
|
+
# serve() builds the router before the first connection is accepted
|
|
131
|
+
return HttpProtocol(self._router, self, loop) # type: ignore[arg-type]
|
|
132
|
+
|
|
133
|
+
async def serve(self, host: str = "0.0.0.0", port: int = 8000,
|
|
134
|
+
grace: float = 10.0) -> None:
|
|
135
|
+
"""Blocks until SIGINT/SIGTERM, then waits for in-flight requests."""
|
|
136
|
+
await _server.serve(self, host, port, grace=grace)
|
|
137
|
+
|
|
138
|
+
def run(self, host: str = "0.0.0.0", port: int = 8000) -> None:
|
|
139
|
+
"""Sync version of serve() — for applications with nothing to close."""
|
|
140
|
+
_server.run(self, host=host, port=port)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
__all__ = ["App"]
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import logging
|
|
3
|
+
|
|
4
|
+
from cyengine.app import App
|
|
5
|
+
from cyengine.core._router import Router
|
|
6
|
+
|
|
7
|
+
log: logging.Logger
|
|
8
|
+
|
|
9
|
+
class HttpProtocol(asyncio.Protocol):
|
|
10
|
+
"""asyncio.Protocol — one instance per TCP connection."""
|
|
11
|
+
|
|
12
|
+
# router is None only until serve() builds it, before the first connection
|
|
13
|
+
def __init__(self, router: Router, app: App, loop: asyncio.AbstractEventLoop) -> None: ...
|
|
14
|
+
|
|
15
|
+
# asyncio callbacks
|
|
16
|
+
def connection_made(self, transport: asyncio.BaseTransport) -> None: ...
|
|
17
|
+
def connection_lost(self, exc: BaseException | None) -> None: ...
|
|
18
|
+
def data_received(self, data: bytes) -> None: ...
|
|
19
|
+
|
|
20
|
+
def close(self) -> None: ...
|
|
21
|
+
def shutdown(self) -> None:
|
|
22
|
+
"""Server shutdown: finish accepted requests and close the connection."""
|
|
23
|
+
|
|
24
|
+
# httptools callbacks
|
|
25
|
+
def on_message_begin(self) -> None: ...
|
|
26
|
+
def on_url(self, url: bytes) -> None: ...
|
|
27
|
+
def on_header(self, name: bytes, value: bytes) -> None: ...
|
|
28
|
+
def on_headers_complete(self) -> None: ...
|
|
29
|
+
def on_body(self, body: bytes) -> None: ...
|
|
30
|
+
def on_message_complete(self) -> None: ...
|
|
31
|
+
|
|
32
|
+
# used by the sweeper in cyengine.server
|
|
33
|
+
def _check_deadline(self, now: float) -> None: ...
|