Feature: 使用 tomllib 读取 toml 配置 (#1720)

This commit is contained in:
Ju4tCode
2023-02-20 22:25:14 +08:00
committed by GitHub
parent e11298d15b
commit 8f79ba1ccd
3 changed files with 266 additions and 180 deletions

View File

@ -9,14 +9,17 @@ from pathlib import Path
from types import ModuleType
from typing import Set, Union, Iterable, Optional
import tomlkit
from nonebot.utils import path_to_module_name
from .plugin import Plugin
from .manager import PluginManager
from . import _managers, get_plugin, _module_name_to_plugin_name
try:
import tomllib # pyright: reportMissingImports=false
except ModuleNotFoundError:
import tomli as tomllib
def load_plugin(module_path: Union[str, Path]) -> Optional[Plugin]:
"""加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
@ -108,7 +111,7 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
```
"""
with open(file_path, "r", encoding=encoding) as f:
data = tomlkit.parse(f.read()) # type: ignore
data = tomllib.loads(f.read())
nonebot_data = data.get("tool", {}).get("nonebot")
if nonebot_data is None: