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

Function load_from_json

nonebot/plugin/load.py:71–99  ·  view source on GitHub ↗

导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件。 以 `_` 开头的插件不会被导入! 参数: file_path: 指定 json 文件路径 encoding: 指定 json 文件编码 用法: ```json title=plugins.json { "plugins": ["some_plugin"], "plugin_dirs": ["some_dir"] } ```

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

Source from the content-addressed store, hash-verified

69
70
71def load_from_json(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
72 """导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件。
73 以 `_` 开头的插件不会被导入!
74
75 参数:
76 file_path: 指定 json 文件路径
77 encoding: 指定 json 文件编码
78
79 用法:
80 ```json title=plugins.json
81 {
82 "plugins": ["some_plugin"],
83 "plugin_dirs": ["some_dir"]
84 }
85 ```
86
87 ```python
88 nonebot.load_from_json("plugins.json")
89 ```
90 """
91 with open(file_path, encoding=encoding) as f:
92 data = json.load(f)
93 if not isinstance(data, dict):
94 raise TypeError("json file must contains a dict!")
95 plugins = data.get("plugins")
96 plugin_dirs = data.get("plugin_dirs")
97 assert isinstance(plugins, list), "plugins must be a list of plugin name"
98 assert isinstance(plugin_dirs, list), "plugin_dirs must be a list of directories"
99 return load_all_plugins(set(plugins), set(plugin_dirs))
100
101
102def load_from_toml(file_path: str, encoding: str = "utf-8") -> set[Plugin]:

Callers

nothing calls this directly

Calls 2

load_all_pluginsFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected