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