MCPcopy
hub / github.com/nonebot/nonebot2 / load_from_toml

Function load_from_toml

nonebot/plugin/load.py:102–158  ·  view source on GitHub ↗

导入指定 toml 文件 `[tool.nonebot]` 中的 `plugins` 以及 `plugin_dirs` 下多个插件。 以 `_` 开头的插件不会被导入! 参数: file_path: 指定 toml 文件路径 encoding: 指定 toml 文件编码 用法: 新格式: ```toml title=pyproject.toml [tool.nonebot] plugin_dirs = ["some_dir"] [tool.no

(file_path: str, encoding: str = "utf-8")

Source from the content-addressed store, hash-verified

100
101
102def load_from_toml(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
103 """导入指定 toml 文件 `[tool.nonebot]` 中的
104 `plugins` 以及 `plugin_dirs` 下多个插件。
105 以 `_` 开头的插件不会被导入!
106
107 参数:
108 file_path: 指定 toml 文件路径
109 encoding: 指定 toml 文件编码
110
111 用法:
112 新格式:
113
114 ```toml title=pyproject.toml
115 [tool.nonebot]
116 plugin_dirs = ["some_dir"]
117
118 [tool.nonebot.plugins]
119 some-store-plugin = ["some_store_plugin"]
120 "@local" = ["some_local_plugin"]
121 ```
122
123 旧格式:
124
125 ```toml title=pyproject.toml
126 [tool.nonebot]
127 plugins = ["some_plugin"]
128 plugin_dirs = ["some_dir"]
129 ```
130
131 ```python
132 nonebot.load_from_toml("pyproject.toml")
133 ```
134 """
135 with open(file_path, encoding=encoding) as f:
136 data = tomllib.loads(f.read())
137
138 nonebot_data = data.get("tool", {}).get("nonebot")
139 if nonebot_data is None:
140 raise ValueError("Cannot find '[tool.nonebot]' in given toml file!")
141 if not isinstance(nonebot_data, dict):
142 raise TypeError("'[tool.nonebot]' must be a Table!")
143 plugins = nonebot_data.get("plugins", {})
144 plugin_dirs = nonebot_data.get("plugin_dirs", [])
145 assert isinstance(plugins, (list, dict)), (
146 "plugins must be a list or a dict of plugin name"
147 )
148 assert isinstance(plugin_dirs, list), "plugin_dirs must be a list of directories"
149 if isinstance(plugins, list):
150 logger.warning("Legacy project format found! Upgrade with `nb upgrade-format`.")
151 return load_all_plugins(
152 set(
153 chain.from_iterable(plugins.values())
154 if isinstance(plugins, dict)
155 else plugins
156 ),
157 plugin_dirs,
158 )
159

Callers

nothing calls this directly

Calls 3

load_all_pluginsFunction · 0.85
getMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected