From 69edb9883503197a97ff340264e68bf873110765 Mon Sep 17 00:00:00 2001 From: uy/sun Date: Sun, 1 Jan 2023 15:08:00 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feature:=20=E5=B0=86=20block=20dri?= =?UTF-8?q?ver=20=E8=BD=AC=E6=AD=A3=E4=B8=BA=20none=20=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E5=99=A8=20(#1522)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/drivers/aiohttp.py | 4 +- nonebot/drivers/httpx.py | 4 +- nonebot/drivers/{_block_driver.py => none.py} | 62 ++++++++++++------- nonebot/drivers/websockets.py | 4 +- tests/test_driver.py | 2 +- website/static/drivers.json | 10 +++ 6 files changed, 55 insertions(+), 31 deletions(-) rename nonebot/drivers/{_block_driver.py => none.py} (78%) diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index c362174d..32814279 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -21,7 +21,7 @@ from contextlib import asynccontextmanager from nonebot.typing import overrides from nonebot.drivers import Request, Response from nonebot.exception import WebSocketClosed -from nonebot.drivers._block_driver import BlockDriver +from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import HTTPVersion, ForwardMixin, ForwardDriver, combine_driver @@ -170,5 +170,5 @@ class WebSocket(BaseWebSocket): await self.websocket.send_bytes(data) -Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore +Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore """AIOHTTP Driver""" diff --git a/nonebot/drivers/httpx.py b/nonebot/drivers/httpx.py index de5617d2..c3c1165b 100644 --- a/nonebot/drivers/httpx.py +++ b/nonebot/drivers/httpx.py @@ -18,7 +18,7 @@ from typing import Type, AsyncGenerator from contextlib import asynccontextmanager from nonebot.typing import overrides -from nonebot.drivers._block_driver import BlockDriver +from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers import ( Request, Response, @@ -77,5 +77,5 @@ class Mixin(ForwardMixin): yield ws -Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore +Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore """HTTPX Driver""" diff --git a/nonebot/drivers/_block_driver.py b/nonebot/drivers/none.py similarity index 78% rename from nonebot/drivers/_block_driver.py rename to nonebot/drivers/none.py index ee020423..dfd2d019 100644 --- a/nonebot/drivers/_block_driver.py +++ b/nonebot/drivers/none.py @@ -1,12 +1,24 @@ +"""None 驱动适配 + +:::tip 提示 +本驱动不支持任何服务器或客户端连接 +::: + +FrontMatter: + sidebar_position: 6 + description: nonebot.drivers.none 模块 +""" + + import signal import asyncio import threading from typing import Set, Union, Callable, Awaitable, cast from nonebot.log import logger -from nonebot.drivers import Driver from nonebot.typing import overrides from nonebot.config import Env, Config +from nonebot.drivers import Driver as BaseDriver from nonebot.utils import run_sync, is_coroutine_callable HOOK_FUNC = Union[Callable[[], None], Callable[[], Awaitable[None]]] @@ -16,7 +28,9 @@ HANDLED_SIGNALS = ( ) -class BlockDriver(Driver): +class Driver(BaseDriver): + """None 驱动框架""" + def __init__(self, env: Env, config: Config): super().__init__(env, config) self.startup_funcs: Set[HOOK_FUNC] = set() @@ -25,18 +39,18 @@ class BlockDriver(Driver): self.force_exit: bool = False @property - @overrides(Driver) + @overrides(BaseDriver) def type(self) -> str: - """驱动名称: `block_driver`""" - return "block_driver" + """驱动名称: `none`""" + return "none" @property - @overrides(Driver) + @overrides(BaseDriver) def logger(self): - """block driver 使用的 logger""" + """none driver 使用的 logger""" return logger - @overrides(Driver) + @overrides(BaseDriver) def on_startup(self, func: HOOK_FUNC) -> HOOK_FUNC: """ 注册一个启动时执行的函数 @@ -44,7 +58,7 @@ class BlockDriver(Driver): self.startup_funcs.add(func) return func - @overrides(Driver) + @overrides(BaseDriver) def on_shutdown(self, func: HOOK_FUNC) -> HOOK_FUNC: """ 注册一个停止时执行的函数 @@ -52,22 +66,22 @@ class BlockDriver(Driver): self.shutdown_funcs.add(func) return func - @overrides(Driver) + @overrides(BaseDriver) def run(self, *args, **kwargs): - """启动 block driver""" + """启动 none driver""" super().run(*args, **kwargs) loop = asyncio.get_event_loop() - loop.run_until_complete(self.serve()) + loop.run_until_complete(self._serve()) - async def serve(self): - self.install_signal_handlers() - await self.startup() + async def _serve(self): + self._install_signal_handlers() + await self._startup() if self.should_exit.is_set(): return - await self.main_loop() - await self.shutdown() + await self._main_loop() + await self._shutdown() - async def startup(self): + async def _startup(self): # run startup cors = [ cast(Callable[..., Awaitable[None]], startup)() @@ -86,10 +100,10 @@ class BlockDriver(Driver): logger.info("Application startup completed.") - async def main_loop(self): + async def _main_loop(self): await self.should_exit.wait() - async def shutdown(self): + async def _shutdown(self): logger.info("Shutting down") logger.info("Waiting for application shutdown.") @@ -130,7 +144,7 @@ class BlockDriver(Driver): loop = asyncio.get_event_loop() loop.stop() - def install_signal_handlers(self) -> None: + def _install_signal_handlers(self) -> None: if threading.current_thread() is not threading.main_thread(): # Signals can only be listened to from the main thread. return @@ -139,13 +153,13 @@ class BlockDriver(Driver): try: for sig in HANDLED_SIGNALS: - loop.add_signal_handler(sig, self.handle_exit, sig, None) + loop.add_signal_handler(sig, self._handle_exit, sig, None) except NotImplementedError: # Windows for sig in HANDLED_SIGNALS: - signal.signal(sig, self.handle_exit) + signal.signal(sig, self._handle_exit) - def handle_exit(self, sig, frame): + def _handle_exit(self, sig, frame): if self.should_exit.is_set(): self.force_exit = True else: diff --git a/nonebot/drivers/websockets.py b/nonebot/drivers/websockets.py index 0701dfec..3dc166a1 100644 --- a/nonebot/drivers/websockets.py +++ b/nonebot/drivers/websockets.py @@ -23,7 +23,7 @@ from nonebot.typing import overrides from nonebot.log import LoguruHandler from nonebot.drivers import Request, Response from nonebot.exception import WebSocketClosed -from nonebot.drivers._block_driver import BlockDriver +from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import ForwardMixin, ForwardDriver, combine_driver @@ -128,5 +128,5 @@ class WebSocket(BaseWebSocket): await self.websocket.send(data) -Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore +Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore """Websockets Driver""" diff --git a/tests/test_driver.py b/tests/test_driver.py index 0402b47c..37342a69 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -149,7 +149,7 @@ async def test_http_driver(app: App): ), pytest.param( {"driver": "~httpx:Driver+~websockets"}, - "block_driver+httpx+websockets", + "none+httpx+websockets", id="httpx+websockets", ), ], diff --git a/website/static/drivers.json b/website/static/drivers.json index 8eef4022..380379f9 100644 --- a/website/static/drivers.json +++ b/website/static/drivers.json @@ -48,5 +48,15 @@ "homepage": "/docs/tutorial/choose-driver", "tags": [], "is_official": true + }, + { + "module_name": "nonebot.drivers.none", + "project_link": "", + "name": "None", + "desc": "None 驱动器", + "author": "yanyongyu", + "homepage": "/docs/tutorial/choose-driver", + "tags": [], + "is_official": true } ]