From d48d3084f776f788767939d73774146086358887 Mon Sep 17 00:00:00 2001 From: GreyElaina Date: Mon, 6 Jun 2022 13:27:32 +0800 Subject: [PATCH] release 0.5.2: adapted to launart 0.3.3 --- pdm.lock | 18 +++++++-------- pyproject.toml | 4 ++-- src/graia/amnesia/builtins/aiohttp.py | 8 +++---- src/graia/amnesia/builtins/common.py | 33 +++++++++++++++++++++++++++ src/graia/amnesia/builtins/uvicorn.py | 6 ++--- src/test_transport.py | 4 ++-- 6 files changed, 53 insertions(+), 20 deletions(-) diff --git a/pdm.lock b/pdm.lock index c8551d7..4b8a244 100644 --- a/pdm.lock +++ b/pdm.lock @@ -238,8 +238,9 @@ dependencies = [ [[package]] name = "launart" -version = "0.3.1" +version = "0.3.3" requires_python = ">=3.8" +url = "https://github.com/GraiaProject/launart/archive/refs/heads/master.zip" summary = "UNKNOWN" dependencies = [ "loguru>=0.6.0", @@ -426,7 +427,7 @@ dependencies = [ [[package]] name = "richuru" -version = "0.1.0" +version = "0.1.1" requires_python = ">=3.8" summary = "a bridge between loguru & rich, rich's powerful render in loguru!" dependencies = [ @@ -580,7 +581,7 @@ dependencies = [ [metadata] lock_version = "3.1" -content_hash = "sha256:ffabc84f52997443164a23ce65078409011d6d1bc4ce9bae256e964c09d898e4" +content_hash = "sha256:8d34341c315d785a6e5a375cabd0be446cc762bb023b00787dc833b5171bbe8e" [metadata.files] "aiohttp 3.8.1" = [ @@ -939,9 +940,8 @@ content_hash = "sha256:ffabc84f52997443164a23ce65078409011d6d1bc4ce9bae256e964c0 {file = "jupyter_core-4.10.0-py3-none-any.whl", hash = "sha256:e7f5212177af7ab34179690140f188aa9bf3d322d8155ed972cbded19f55b6f3"}, {file = "jupyter_core-4.10.0.tar.gz", hash = "sha256:a6de44b16b7b31d7271130c71a6792c4040f077011961138afed5e5e73181aec"}, ] -"launart 0.3.1" = [ - {file = "launart-0.3.1-py3-none-any.whl", hash = "sha256:b1e5a775c9578bfe0929361a5fdddfa77120bf0742736932a689cc7bbf76a3c1"}, - {file = "launart-0.3.1.tar.gz", hash = "sha256:817f00796e7ed4edae62efe940f97922604007c26e06217a47031c167df783a6"}, +"launart 0.3.3" = [ + {file = "master.zip", hash = "sha256:b79ac95529e262208026540a62cc3b9ced98bdf3dcde9106b5b96c1e880635e8"}, ] "loguru 0.6.0" = [ {file = "loguru-0.6.0-py3-none-any.whl", hash = "sha256:4e2414d534a2ab57573365b3e6d0234dfb1d84b68b7f3b948e6fb743860a77c3"}, @@ -1262,9 +1262,9 @@ content_hash = "sha256:ffabc84f52997443164a23ce65078409011d6d1bc4ce9bae256e964c0 {file = "rich-12.4.4-py3-none-any.whl", hash = "sha256:d2bbd99c320a2532ac71ff6a3164867884357da3e3301f0240090c5d2fdac7ec"}, {file = "rich-12.4.4.tar.gz", hash = "sha256:4c586de507202505346f3e32d1363eb9ed6932f0c2f63184dea88983ff4971e2"}, ] -"richuru 0.1.0" = [ - {file = "richuru-0.1.0-py3-none-any.whl", hash = "sha256:327e0fec4246f6df73c52e10e25da413d52e698862860f68096f2c0ea2d1b48c"}, - {file = "richuru-0.1.0.tar.gz", hash = "sha256:f655a0f91e967c98277accce6a6f575a5faeb0d6e2e9ed612cadf2b7ca6fc44f"}, +"richuru 0.1.1" = [ + {file = "richuru-0.1.1-py3-none-any.whl", hash = "sha256:e73d43913a372d741ff61106001b5e6adfa24c6d0665dd390328a33e34cd210d"}, + {file = "richuru-0.1.1.tar.gz", hash = "sha256:815816f3a0142b67c53bd424fcced0821aa1104b5386301cff47fc7bbb6d90ea"}, ] "setuptools 62.3.2" = [ {file = "setuptools-62.3.2-py3-none-any.whl", hash = "sha256:68e45d17c9281ba25dc0104eadd2647172b3472d9e01f911efa57965e8d51a36"}, diff --git a/pyproject.toml b/pyproject.toml index 66dbd2b..79251d9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ # PEP 621 project metadata # See https://www.python.org/dev/peps/pep-0621/ name = "graia-amnesia" -version = "0.5.1" +version = "0.5.2" authors = [ {name = "GreyElaina", email = "GreyElaina@outlook.com"}, ] @@ -11,7 +11,7 @@ license-expression = "MIT" dependencies = [ "loguru<1.0.0,>=0.6.0", "yarl<2.0.0,>=1.7.2", - "launart>=0.3.1", + "launart>=0.3.3", "statv>=0.2.2", ] readme = "README.md" diff --git a/src/graia/amnesia/builtins/aiohttp.py b/src/graia/amnesia/builtins/aiohttp.py index cd1e79d..f8f7ae1 100644 --- a/src/graia/amnesia/builtins/aiohttp.py +++ b/src/graia/amnesia/builtins/aiohttp.py @@ -339,14 +339,14 @@ def get_interface(self, interface_type): @property def stages(self): - return {"prepare", "cleanup"} + return {"preparing", "cleanup"} @property def required(self): return set() async def launch(self, mgr: Launart): - async with self.stage("prepare"): + async with self.stage("preparing"): if not self.session: self.session = ClientSession(timeout=ClientTimeout(total=None)) async with self.stage("cleanup"): @@ -550,14 +550,14 @@ def get_interface(self, interface_type): @property def stages(self): - return {"prepare", "blocking", "cleanup"} + return {"preparing", "blocking", "cleanup"} @property def required(self): return set() async def launch(self, manager: Launart): - async with self.stage("prepare"): + async with self.stage("preparing"): logger.info(f"starting server on {self.host}:{self.port}") runner = web.AppRunner(self.wsgi_handler) await runner.setup() diff --git a/src/graia/amnesia/builtins/common.py b/src/graia/amnesia/builtins/common.py index a21094d..199696b 100644 --- a/src/graia/amnesia/builtins/common.py +++ b/src/graia/amnesia/builtins/common.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING from launart import ExportInterface +from launart.service import Service if TYPE_CHECKING: from typing import Any, Awaitable, Callable, MutableMapping, Type @@ -17,3 +18,35 @@ def get_asgi_handler( Callable[[MutableMapping[str, Any]], Awaitable[None]] ], None]""": ... + + +class ExternalASGIHandlerService(Service, ASGIHandlerProvider): + supported_interface_types = {ASGIHandlerProvider} + + asgi: """Callable[[ + Type[MutableMapping[str, Any]], + Awaitable[MutableMapping[str, Any]], + Callable[[MutableMapping[str, Any]], Awaitable[None]] + ], None]""" + + def __init__( + self, + asgi_handler: """Callable[[ + Type[MutableMapping[str, Any]], + Awaitable[MutableMapping[str, Any]], + Callable[[MutableMapping[str, Any]], Awaitable[None]] + ], None]""", + ) -> None: + self.asgi = asgi_handler + + def get_interface(self, _): + return self + + def get_asgi_handler( + self, + ) -> """Callable[[ + Type[MutableMapping[str, Any]], + Awaitable[MutableMapping[str, Any]], + Callable[[MutableMapping[str, Any]], Awaitable[None]] + ], None]""": + return self.asgi diff --git a/src/graia/amnesia/builtins/uvicorn.py b/src/graia/amnesia/builtins/uvicorn.py index a872ea2..0dceb2d 100644 --- a/src/graia/amnesia/builtins/uvicorn.py +++ b/src/graia/amnesia/builtins/uvicorn.py @@ -55,10 +55,10 @@ def required(self): @property def stages(self): - return {"prepare", "cleanup"} + return {"preparing", "cleanup"} async def launch(self, mgr: Launart): - async with self.stage("prepare"): + async with self.stage("preparing"): asgi_handler = mgr.get_interface(ASGIHandlerProvider).get_asgi_handler() self.server = WithoutSigHandlerServer(Config(asgi_handler, host=self.host, port=self.port)) # TODO: 使用户拥有更多的对 Config 的配置能力. @@ -69,8 +69,8 @@ async def launch(self, mgr: Launart): target = logging.getLogger(name) target.handlers = [LoguruHandler(level=level)] target.propagate = False - async with self.stage("cleanup"): serve_task = asyncio.create_task(self.server.serve()) + async with self.stage("cleanup"): logger.warning("try to shutdown uvicorn server...") self.server.should_exit = True await wait_fut([serve_task, asyncio.sleep(10)], return_when=asyncio.FIRST_COMPLETED) diff --git a/src/test_transport.py b/src/test_transport.py index 0845244..c6ee241 100644 --- a/src/test_transport.py +++ b/src/test_transport.py @@ -41,7 +41,7 @@ mgr.add_service(AiohttpService()) mgr.add_service(StarletteService()) mgr.add_service(UvicornService(port=21447)) -install() +# install() cbr = TransportRegistrar() @@ -153,7 +153,7 @@ async def launch(self, mgr: Launart): ai = mgr.get_interface(AiohttpClientInterface) rider = ai.websocket("http://localhost:21447/ws_test") await asyncio.wait( - [rider.use(TestWsClient()), mgr.status.wait_for_completed()], return_when=asyncio.FIRST_COMPLETED + [rider.use(TestWsClient()), mgr.status.wait_for_sigexit()], return_when=asyncio.FIRST_COMPLETED )