88"""
99
1010from collections .abc import Iterable
11+ from itertools import chain
1112import json
1213from pathlib import Path
1314from types import ModuleType
1415from typing import Optional , Union
1516
17+ from nonebot .log import logger
1618from nonebot .utils import path_to_module_name
1719
1820from . import _managers , _module_name_to_plugin_id , get_plugin
@@ -108,6 +110,19 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
108110 encoding: 指定 toml 文件编码
109111
110112 用法:
113+ 新格式:
114+
115+ ```toml title=pyproject.toml
116+ [tool.nonebot]
117+ plugin_dirs = ["some_dir"]
118+
119+ [tool.nonebot.plugins]
120+ some-store-plugin = ["some_store_plugin"]
121+ "@local" = ["some_local_plugin"]
122+ ```
123+
124+ 旧格式:
125+
111126 ```toml title=pyproject.toml
112127 [tool.nonebot]
113128 plugins = ["some_plugin"]
@@ -126,11 +141,22 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
126141 raise ValueError ("Cannot find '[tool.nonebot]' in given toml file!" )
127142 if not isinstance (nonebot_data , dict ):
128143 raise TypeError ("'[tool.nonebot]' must be a Table!" )
129- plugins = nonebot_data .get ("plugins" , [] )
144+ plugins = nonebot_data .get ("plugins" , {} )
130145 plugin_dirs = nonebot_data .get ("plugin_dirs" , [])
131- assert isinstance (plugins , list ), "plugins must be a list of plugin name"
146+ assert isinstance (plugins , (list , dict )), (
147+ "plugins must be a list or a dict of plugin name"
148+ )
132149 assert isinstance (plugin_dirs , list ), "plugin_dirs must be a list of directories"
133- return load_all_plugins (plugins , plugin_dirs )
150+ if isinstance (plugins , list ):
151+ logger .warning ("Legacy project format found! Upgrade with `nb upgrade-format`." )
152+ return load_all_plugins (
153+ set (
154+ chain .from_iterable (plugins .values ())
155+ if isinstance (plugins , dict )
156+ else plugins
157+ ),
158+ plugin_dirs ,
159+ )
134160
135161
136162def load_builtin_plugin (name : str ) -> Optional [Plugin ]:
0 commit comments