From a72eeb4c3f30e3a20ff51ef6465b09d62b626d49 Mon Sep 17 00:00:00 2001 From: Expliyh Date: Fri, 17 May 2024 16:26:30 +0800 Subject: [PATCH] =?UTF-8?q?:pencil:=20=E4=BD=BF=E7=94=A8=20driver=5Fmanage?= =?UTF-8?q?r=E8=87=AA=E5=8A=A8=E7=AE=A1=E7=90=86=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E9=A9=B1=E5=8A=A8=E5=99=A8=EF=BC=8C=E6=97=A0=E9=9C=80?= =?UTF-8?q?=E6=89=8B=E5=8A=A8=E9=85=8D=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=20(=E5=BD=93=E9=85=8D=E7=BD=AE=E4=BA=86=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=97=B6=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E4=BC=98=E5=85=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/driver_manager/__init__.py | 6 ++++++ liteyuki/driver_manager/auto_set_env.py | 20 ++++++++++++++++++++ liteyuki/driver_manager/defines.py | 17 +++++++++++++++++ main.py | 4 +++- requirements.txt | 4 ++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 liteyuki/driver_manager/__init__.py create mode 100644 liteyuki/driver_manager/auto_set_env.py create mode 100644 liteyuki/driver_manager/defines.py diff --git a/liteyuki/driver_manager/__init__.py b/liteyuki/driver_manager/__init__.py new file mode 100644 index 0000000..89d9009 --- /dev/null +++ b/liteyuki/driver_manager/__init__.py @@ -0,0 +1,6 @@ +from .auto_set_env import auto_set_env + + +def init(config: dict): + auto_set_env(config) + return diff --git a/liteyuki/driver_manager/auto_set_env.py b/liteyuki/driver_manager/auto_set_env.py new file mode 100644 index 0000000..2568d80 --- /dev/null +++ b/liteyuki/driver_manager/auto_set_env.py @@ -0,0 +1,20 @@ +import os + +import dotenv +import nonebot + +from .defines import * + + +def auto_set_env(config: dict): + dotenv.load_dotenv() + if os.getenv("DRIVER", None) is not None: + nonebot.logger.info("Driver already set in environment variable, skip auto configure.") + return + if config.get("enable_satori", False): + os.environ["DRIVER"] = get_driver_string(ASGI_DRIVER, HTTPX_DRIVER, WEBSOCKETS_DRIVER) + nonebot.logger.info("Enable Satori, set driver to ASGI+HTTPX+WEBSOCKETS") + else: + os.environ["DRIVER"] = get_driver_string(ASGI_DRIVER) + nonebot.logger.info("Disable Satori, set driver to ASGI") + return diff --git a/liteyuki/driver_manager/defines.py b/liteyuki/driver_manager/defines.py new file mode 100644 index 0000000..b4756bf --- /dev/null +++ b/liteyuki/driver_manager/defines.py @@ -0,0 +1,17 @@ +ASGI_DRIVER = "~fastapi" +HTTPX_DRIVER = "~httpx" +WEBSOCKETS_DRIVER = "~websockets" + + +def get_driver_string(*argv): + output_string = "" + if ASGI_DRIVER in argv: + output_string += ASGI_DRIVER + for arg in argv: + if arg != ASGI_DRIVER: + output_string = f"{output_string}+{arg}" + return output_string + + +def get_driver_full_string(*argv): + return f"DRIVER={get_driver_string(argv)}" diff --git a/main.py b/main.py index b332f58..9e7f152 100644 --- a/main.py +++ b/main.py @@ -5,12 +5,14 @@ from liteyuki.utils import init from liteyuki.utils.base.config import load_from_yaml from liteyuki.utils.base.data_manager import StoredConfig, common_db from liteyuki.utils.base.ly_api import liteyuki_api +from liteyuki import driver_manager if __name__ == "__mp_main__": init() store_config: dict = common_db.where_one(StoredConfig(), default=StoredConfig()).config static_config = load_from_yaml("config.yml") store_config.update(static_config) + driver_manager.init(config=store_config) nonebot.init(**store_config) if not store_config.get("enable_satori", False): @@ -18,7 +20,7 @@ if __name__ == "__mp_main__": else: adapters = [v11.Adapter, v12.Adapter, satori.Adapter] driver = nonebot.get_driver() - + # print(driver.__dict__) for adapter in adapters: driver.register_adapter(adapter) diff --git a/requirements.txt b/requirements.txt index 960afaa..b7b7493 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,3 +23,7 @@ importlib_metadata~=7.0.2 requests~=2.31.0 watchdog~=4.0.0 pillow~=10.2.0 + +pip~=23.2.1 +fastapi~=0.110.0 +python-dotenv~=1.0.1 \ No newline at end of file