From 04f4d5028e1aa31b4821b70624e28b7fa44d1608 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Thu, 13 Aug 2020 18:16:59 +0800 Subject: [PATCH] fix config and import nonebot-test --- nonebot/__init__.py | 10 ++++++++++ nonebot/config.py | 12 ++++++++---- pyproject.toml | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/nonebot/__init__.py b/nonebot/__init__.py index c4b93b2c..54a409ce 100644 --- a/nonebot/__init__.py +++ b/nonebot/__init__.py @@ -11,6 +11,11 @@ from nonebot.drivers import BaseDriver from nonebot.adapters.cqhttp import Bot as CQBot from nonebot.typing import Union, Optional, NoReturn +try: + import nonebot_test +except ImportError: + nonebot_test = None + _driver: Optional[BaseDriver] = None @@ -41,8 +46,13 @@ def init(*, _env_file: Optional[str] = None, **kwargs): Driver = getattr(importlib.import_module(config.driver), "Driver") _driver = Driver(env, config) + # register build-in adapters _driver.register_adapter("cqhttp", CQBot) + # load nonebot test frontend if debug + if config.debug and nonebot_test: + nonebot_test.init() + def run(host: Optional[IPv4Address] = None, port: Optional[int] = None, diff --git a/nonebot/config.py b/nonebot/config.py index 34da187d..0223d4b0 100644 --- a/nonebot/config.py +++ b/nonebot/config.py @@ -16,8 +16,9 @@ class BaseConfig(BaseSettings): def _build_environ( self, - _env_file: Union[Path, str, - None] = None) -> Dict[str, Optional[str]]: + _env_file: Union[Path, str, None] = None, + _env_file_encoding: Optional[str] = None + ) -> Dict[str, Optional[str]]: """ Build environment variables suitable for passing to the Model. """ @@ -28,13 +29,16 @@ class BaseConfig(BaseSettings): else: env_vars = {k.lower(): v for k, v in os.environ.items()} + env_file_vars: Dict[str, Optional[str]] = {} env_file = _env_file if _env_file != env_file_sentinel else self.__config__.env_file - env_file_vars = {} + env_file_encoding = _env_file_encoding if _env_file_encoding is not None else self.__config__.env_file_encoding if env_file is not None: env_path = Path(env_file) if env_path.is_file(): env_file_vars = read_env_file( - env_path, case_sensitive=self.__config__.case_sensitive) + env_path, + encoding=env_file_encoding, + case_sensitive=self.__config__.case_sensitive) env_vars = {**env_file_vars, **env_vars} for field in self.__fields__.values(): diff --git a/pyproject.toml b/pyproject.toml index 43973465..57535f89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,9 +23,9 @@ python = "^3.7" httpx = "^0.13.3" fastapi = "^0.58.1" uvicorn = "^0.11.5" -pydantic = { extras = ["dotenv"], version = "^1.5.1" } +pydantic = { extras = ["dotenv"], version = "^1.6.1" } apscheduler = { version = "^3.6.3", optional = true } -# nonebot-test = { version = "^0.1.0", optional = true } +nonebot-test = { version = "^0.1.0", optional = true } [tool.poetry.dev-dependencies] yapf = "^0.30.0"