mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-26 15:51:26 +00:00
♿ add plugin loading using json/toml
This commit is contained in:
@ -19,6 +19,8 @@
|
||||
- ``load_plugin`` => ``nonebot.plugin.load_plugin``
|
||||
- ``load_plugins`` => ``nonebot.plugin.load_plugins``
|
||||
- ``load_all_plugins`` => ``nonebot.plugin.load_all_plugins``
|
||||
- ``load_from_json`` => ``nonebot.plugin.load_from_json``
|
||||
- ``load_from_toml`` => ``nonebot.plugin.load_from_toml``
|
||||
- ``load_builtin_plugins`` => ``nonebot.plugin.load_builtin_plugins``
|
||||
- ``get_plugin`` => ``nonebot.plugin.get_plugin``
|
||||
- ``get_loaded_plugins`` => ``nonebot.plugin.get_loaded_plugins``
|
||||
@ -228,4 +230,5 @@ def run(host: Optional[str] = None,
|
||||
from nonebot.plugin import on_message, on_notice, on_request, on_metaevent, CommandGroup, MatcherGroup
|
||||
from nonebot.plugin import on_startswith, on_endswith, on_keyword, on_command, on_shell_command, on_regex
|
||||
from nonebot.plugin import load_plugin, load_plugins, load_all_plugins, load_builtin_plugins
|
||||
from nonebot.plugin import load_from_json, load_from_toml
|
||||
from nonebot.plugin import export, require, get_plugin, get_loaded_plugins
|
||||
|
@ -73,6 +73,8 @@ class Driver(abc.ABC):
|
||||
* ``name: str``: 适配器名称,用于在连接时进行识别
|
||||
* ``adapter: Type[Bot]``: 适配器 Class
|
||||
"""
|
||||
if name in self._adapters:
|
||||
print("============", name)
|
||||
self._adapters[name] = adapter
|
||||
adapter.register(self, self.config, **kwargs)
|
||||
logger.opt(
|
||||
|
@ -5,12 +5,13 @@
|
||||
为 NoneBot 插件开发提供便携的定义函数。
|
||||
"""
|
||||
import re
|
||||
import json
|
||||
from types import ModuleType
|
||||
from dataclasses import dataclass
|
||||
from importlib._bootstrap import _load
|
||||
from contextvars import Context, ContextVar, copy_context
|
||||
from typing import Any, Set, List, Dict, Type, Tuple, Union, Optional, TYPE_CHECKING
|
||||
|
||||
import tomlkit
|
||||
from nonebot.log import logger
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.permission import Permission
|
||||
@ -1069,6 +1070,32 @@ def load_all_plugins(module_path: Set[str],
|
||||
return loaded_plugins
|
||||
|
||||
|
||||
def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
||||
with open(file_path, "r", encoding=encoding) as f:
|
||||
data = json.load(f)
|
||||
plugins = data.get("plugins")
|
||||
plugin_dirs = data.get("plugin_dirs")
|
||||
assert isinstance(plugins, list), "plugins must be a list of plugin name"
|
||||
assert isinstance(plugin_dirs,
|
||||
list), "plugin_dirs must be a list of directories"
|
||||
return load_all_plugins(set(plugins), set(plugin_dirs))
|
||||
|
||||
|
||||
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())
|
||||
|
||||
nonebot_data = data.get("nonebot", {}).get("plugins")
|
||||
if not nonebot_data:
|
||||
raise ValueError("Cannot find '[nonebot.plugins]' in given toml file!")
|
||||
plugins = nonebot_data.get("plugins", [])
|
||||
plugin_dirs = nonebot_data.get("plugin_dirs", [])
|
||||
assert isinstance(plugins, list), "plugins must be a list of plugin name"
|
||||
assert isinstance(plugin_dirs,
|
||||
list), "plugin_dirs must be a list of directories"
|
||||
return load_all_plugins(set(plugins), set(plugin_dirs))
|
||||
|
||||
|
||||
def load_builtin_plugins(name: str = "echo") -> Optional[Plugin]:
|
||||
"""
|
||||
:说明:
|
||||
|
@ -181,6 +181,14 @@ def load_all_plugins(module_path: Set[str],
|
||||
...
|
||||
|
||||
|
||||
def load_from_json(file_path: str, encoding: str = ...) -> Set[Plugin]:
|
||||
...
|
||||
|
||||
|
||||
def load_from_toml(file_path: str, encoding: str = ...) -> Set[Plugin]:
|
||||
...
|
||||
|
||||
|
||||
def load_builtin_plugins(name: str = ...):
|
||||
...
|
||||
|
||||
|
Reference in New Issue
Block a user