📝 使用 driver_manager自动管理启用的驱动器,无需手动配置环境变量 (当配置了环境变量时环境变量优先)

This commit is contained in:
Expliyh 2024-05-17 16:26:30 +08:00
parent 309397b72c
commit a72eeb4c3f
No known key found for this signature in database
GPG Key ID: F30DE0CFF6157916
5 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,6 @@
from .auto_set_env import auto_set_env
def init(config: dict):
auto_set_env(config)
return

View File

@ -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

View File

@ -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)}"

View File

@ -5,12 +5,14 @@ from liteyuki.utils import init
from liteyuki.utils.base.config import load_from_yaml from liteyuki.utils.base.config import load_from_yaml
from liteyuki.utils.base.data_manager import StoredConfig, common_db from liteyuki.utils.base.data_manager import StoredConfig, common_db
from liteyuki.utils.base.ly_api import liteyuki_api from liteyuki.utils.base.ly_api import liteyuki_api
from liteyuki import driver_manager
if __name__ == "__mp_main__": if __name__ == "__mp_main__":
init() init()
store_config: dict = common_db.where_one(StoredConfig(), default=StoredConfig()).config store_config: dict = common_db.where_one(StoredConfig(), default=StoredConfig()).config
static_config = load_from_yaml("config.yml") static_config = load_from_yaml("config.yml")
store_config.update(static_config) store_config.update(static_config)
driver_manager.init(config=store_config)
nonebot.init(**store_config) nonebot.init(**store_config)
if not store_config.get("enable_satori", False): if not store_config.get("enable_satori", False):
@ -18,7 +20,7 @@ if __name__ == "__mp_main__":
else: else:
adapters = [v11.Adapter, v12.Adapter, satori.Adapter] adapters = [v11.Adapter, v12.Adapter, satori.Adapter]
driver = nonebot.get_driver() driver = nonebot.get_driver()
# print(driver.__dict__)
for adapter in adapters: for adapter in adapters:
driver.register_adapter(adapter) driver.register_adapter(adapter)

View File

@ -23,3 +23,7 @@ importlib_metadata~=7.0.2
requests~=2.31.0 requests~=2.31.0
watchdog~=4.0.0 watchdog~=4.0.0
pillow~=10.2.0 pillow~=10.2.0
pip~=23.2.1
fastapi~=0.110.0
python-dotenv~=1.0.1