nonebot2/nonebot/drivers/quart.py

294 lines
8.0 KiB
Python
Raw Normal View History

2021-02-06 10:46:17 +08:00
"""
Quart 驱动适配
================
后端使用方法请参考: `Quart 文档`_
.. _Quart 文档:
https://pgjones.gitlab.io/quart/index.html
"""
2021-12-20 15:46:23 +08:00
from functools import partial
from typing import List, TypeVar, Callable, Optional, Coroutine
2021-02-06 10:34:52 +08:00
import uvicorn
2021-06-10 21:52:20 +08:00
from pydantic import BaseSettings
from nonebot.config import Env
2021-02-06 10:34:52 +08:00
from nonebot.log import logger
from nonebot.typing import overrides
from nonebot.utils import escape_tag
2021-12-22 16:53:55 +08:00
from nonebot.drivers.httpx import HttpxMixin
from nonebot.config import Config as NoneBotConfig
2021-12-20 15:46:23 +08:00
from nonebot.drivers import Request as BaseRequest
from nonebot.drivers import WebSocket as BaseWebSocket
2021-12-22 16:53:55 +08:00
from nonebot.drivers.websockets import WebSocketsMixin
from nonebot.drivers import (
ReverseDriver,
HTTPServerSetup,
WebSocketServerSetup,
combine_driver,
)
try:
from quart import request as _request
import werkzeug.exceptions as exceptions
from quart import websocket as _websocket
2021-06-10 21:52:20 +08:00
from quart import Quart, Request, Response
from quart import Websocket as QuartWebSocket
except ImportError:
raise ValueError("Please install Quart by using `pip install nonebot2[quart]`")
_AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine])
2021-06-10 21:52:20 +08:00
class Config(BaseSettings):
"""
Quart 驱动框架设置
"""
quart_reload: Optional[bool] = None
2021-06-10 21:52:20 +08:00
"""
:类型:
``Optional[bool]``
2021-06-10 21:52:20 +08:00
:说明:
开启/关闭冷重载默认会在配置了 app debug 模式启用
"""
quart_reload_dirs: Optional[List[str]] = None
"""
:类型:
``Optional[List[str]]``
:说明:
重载监控文件夹列表默认为 uvicorn 默认值
"""
quart_reload_delay: Optional[float] = None
"""
:类型:
``Optional[float]``
:说明:
重载延迟默认为 uvicorn 默认值
"""
quart_reload_includes: Optional[List[str]] = None
"""
:类型:
``Optional[List[str]]``
:说明:
要监听的文件列表支持 glob pattern默认为 uvicorn 默认值
"""
quart_reload_excludes: Optional[List[str]] = None
"""
:类型:
``Optional[List[str]]``
:说明:
不要监听的文件列表支持 glob pattern默认为 uvicorn 默认值
2021-06-10 21:52:20 +08:00
"""
class Config:
extra = "ignore"
2021-05-22 06:43:53 +08:00
class Driver(ReverseDriver):
2021-02-06 10:46:17 +08:00
"""
Quart 驱动框架
"""
def __init__(self, env: Env, config: NoneBotConfig):
super().__init__(env, config)
2021-06-10 21:52:20 +08:00
self.quart_config = Config(**config.dict())
self._server_app = Quart(self.__class__.__qualname__)
@property
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def type(self) -> str:
2021-02-06 10:46:17 +08:00
"""驱动名称: ``quart``"""
2021-06-10 21:52:20 +08:00
return "quart"
@property
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def server_app(self) -> Quart:
2021-02-06 10:46:17 +08:00
"""``Quart`` 对象"""
return self._server_app
@property
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def asgi(self):
2021-02-06 10:46:17 +08:00
"""``Quart`` 对象"""
return self._server_app
@property
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
2021-02-06 10:34:52 +08:00
def logger(self):
2021-06-10 21:52:20 +08:00
"""Quart 使用的 logger"""
return self._server_app.logger
2021-12-20 15:46:23 +08:00
@overrides(ReverseDriver)
def setup_http_server(self, setup: HTTPServerSetup):
self._server_app.add_url_rule(
setup.path.path,
methods=[setup.method],
view_func=partial(self._handle_http, setup=setup),
)
@overrides(ReverseDriver)
def setup_websocket_server(self, setup: WebSocketServerSetup) -> None:
self._server_app.add_websocket(
setup.path.path,
view_func=partial(self._handle_ws, setup=setup),
)
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def on_startup(self, func: _AsyncCallable) -> _AsyncCallable:
2021-06-10 21:52:20 +08:00
"""参考文档: `Startup and Shutdown`_
.. _Startup and Shutdown:
https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html
"""
return self.server_app.before_serving(func) # type: ignore
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def on_shutdown(self, func: _AsyncCallable) -> _AsyncCallable:
2021-06-10 21:52:20 +08:00
"""参考文档: `Startup and Shutdown`_"""
return self.server_app.after_serving(func) # type: ignore
2021-05-22 06:43:53 +08:00
@overrides(ReverseDriver)
def run(
self,
host: Optional[str] = None,
port: Optional[int] = None,
*,
app: Optional[str] = None,
**kwargs,
):
2021-02-06 10:34:52 +08:00
"""使用 ``uvicorn`` 启动 Quart"""
super().run(host, port, app, **kwargs)
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"default": {
"class": "nonebot.log.LoguruHandler",
},
},
"loggers": {
"uvicorn.error": {"handlers": ["default"], "level": "INFO"},
2021-02-06 10:34:52 +08:00
"uvicorn.access": {
"handlers": ["default"],
"level": "INFO",
},
},
}
uvicorn.run(
app or self.server_app, # type: ignore
host=host or str(self.config.host),
port=port or self.config.port,
reload=self.quart_config.quart_reload
if self.quart_config.quart_reload is not None
else (bool(app) and self.config.debug),
reload_dirs=self.quart_config.quart_reload_dirs,
reload_delay=self.quart_config.quart_reload_delay,
reload_includes=self.quart_config.quart_reload_includes,
reload_excludes=self.quart_config.quart_reload_excludes,
debug=self.config.debug,
log_config=LOGGING_CONFIG,
**kwargs,
)
2021-12-20 15:46:23 +08:00
async def _handle_http(self, setup: HTTPServerSetup) -> Response:
request: Request = _request
2021-12-20 15:46:23 +08:00
http_request = BaseRequest(
request.method,
2021-12-20 15:46:23 +08:00
request.url,
headers=request.headers.items(),
cookies=list(request.cookies.items()),
content=await request.get_data(
cache=False, as_text=False, parse_form_data=False
),
version=request.http_version,
)
2021-02-06 10:34:52 +08:00
2021-12-20 15:46:23 +08:00
response = await setup.handle_func(http_request)
return Response(
2021-12-20 15:46:23 +08:00
response.content or "",
response.status_code or 200,
headers=dict(response.headers),
)
2021-12-20 15:46:23 +08:00
async def _handle_ws(self, setup: WebSocketServerSetup) -> None:
websocket: QuartWebSocket = _websocket
2021-06-10 21:52:20 +08:00
2021-12-20 15:46:23 +08:00
http_request = BaseRequest(
websocket.method,
websocket.url,
headers=websocket.headers.items(),
cookies=list(websocket.cookies.items()),
version=websocket.http_version,
)
2021-12-20 15:46:23 +08:00
ws = WebSocket(request=http_request, websocket=websocket)
await setup.handle_func(ws)
class WebSocket(BaseWebSocket):
2021-12-20 15:46:23 +08:00
def __init__(self, *, request: BaseRequest, websocket: QuartWebSocket):
super().__init__(request=request)
self.websocket = websocket
@property
@overrides(BaseWebSocket)
def closed(self):
2021-12-21 18:22:14 +08:00
# FIXME
return True
@overrides(BaseWebSocket)
async def accept(self):
await self.websocket.accept()
@overrides(BaseWebSocket)
2021-12-21 18:22:14 +08:00
async def close(self, code: int = 1000, reason: str = ""):
await self.websocket.close(code, reason)
@overrides(BaseWebSocket)
2021-06-10 21:52:20 +08:00
async def receive(self) -> str:
2021-12-20 15:46:23 +08:00
msg = await self.websocket.receive()
if isinstance(msg, bytes):
raise TypeError("WebSocket received unexpected frame type: bytes")
return msg
2021-06-10 21:52:20 +08:00
@overrides(BaseWebSocket)
async def receive_bytes(self) -> bytes:
2021-12-20 15:46:23 +08:00
msg = await self.websocket.receive()
if isinstance(msg, str):
raise TypeError("WebSocket received unexpected frame type: str")
return msg
2021-06-10 21:52:20 +08:00
@overrides(BaseWebSocket)
async def send(self, data: str):
await self.websocket.send(data)
@overrides(BaseWebSocket)
2021-06-10 21:52:20 +08:00
async def send_bytes(self, data: bytes):
await self.websocket.send(data)
2021-12-22 16:53:55 +08:00
FullDriver = combine_driver(Driver, HttpxMixin, WebSocketsMixin)