依赖注入扩展,允许对非handle函数进行依赖注入
Project description
📖 介绍
该插件允许在Matcher响应范围内,对所有调用的函数使用依赖注入向上下文获取参数,不再需要handle函数传参了
💿 安装
使用 nb-cli 安装
在 nonebot2 项目的根目录下打开命令行, 输入以下指令即可安装nb plugin install nonebot-plugin-exdi
使用包管理器安装
在 nonebot2 项目的插件目录下, 打开命令行, 根据你使用的包管理器, 输入相应的安装命令pip
pip install nonebot-plugin-exdi
pdm
pdm add nonebot-plugin-exdi
poetry
poetry add nonebot-plugin-exdi
conda
conda install nonebot-plugin-exdi
打开 nonebot2 项目根目录下的 pyproject.toml 文件, 在 [tool.nonebot] 部分追加写入
plugins = ["nonebot_plugin_exdi"]
⚙️ 使用说明
一般使用
exdi = require("nonebot_plugin_exdi")
matcher1 = on_command('cs')
@matcher1.handle(parameterless=[exdi.init_di()])
async def my_handle():
await my_func1()
@exdi.di()
async def my_func1(bot: Bot, matcher: Matcher, arg: Message = CommandArg()):
await matcher.send(f'你的bot是:{bot}')
await matcher.send(f'收到的参数是:{arg}')
运行结果:
自定义Depends支持
exdi = require("nonebot_plugin_exdi")
def _depend1(bot: Bot, event: Event):
# do someting
return 'depend1返回值'
def _depend2(bot: Bot, event: Event):
# do someting
return 'depend2返回值'
depend1 = Annotated[str, Depends(_depend1)]
depend2 = Depends(_depend2)
@dataclass
class ClassDependency:
event: Event
depend1: depend1
def __str__(self) -> str:
return f'(event type:{self.event.get_type()}, depend1: {self.depend1})'
classDepend = Annotated[ClassDependency, Depends(ClassDependency)]
matcher1 = on_command('cs')
@matcher1.handle(parameterless=[exdi.init_di()])
async def my_handle():
for msg in my_func2():
await matcher1.send(msg)
@exdi.di()
def my_func2(d1: depend1, classDepend: classDepend, d2: str = depend2):
yield f'depend1: {d1}, depend2: {d2}'
yield f'classDepend: {classDepend}'
运行结果:
手动传入参数,默认值参数支持
exdi = require("nonebot_plugin_exdi")
matcher1 = on_command('cs')
@matcher1.handle(parameterless=[exdi.init_di()])
async def my_handle():
await my_func3(arg1='cillo world')
@exdi.di()
async def my_func3(matcher: Matcher, arg1: str, arg2: str = 'defaultValue'):
await matcher.send(f'arg1: {arg1}')
await matcher.send(f'arg2: {arg2}')
运行结果:
注意,通常情况下不支持生成器依赖,同时不和matcher共用缓存。这是由于nonebot不对外暴漏dependency_cache和stack,缓存需要dependency_cache,生成器依赖需要stack。如果有需要请参考下面的重写NoneBot。
重写NoneBot
搞清楚你在做什么后再继续往下看,如果你不了解nonebot的运作规律,请勿使用该项
如果想要更好的运行效果,必须要重写nonebot.message.handle_event函数来获取dependency_cache和stack。重写该函数后不再需要init_di来初始化依赖注入。
| 配置项 | 必填 | 默认值 | 说明 |
|---|---|---|---|
| exdi_overwrite_nb | 否 | False | 重写nonebot的nonebot.message.handle_event函数 |
| exdi_hand_overwrite | 否 | False | 你手动重写nonebot.message.handle_event函数 |
你可以通过设置exdi_overwrite_nb为True来让插件自己覆盖handle_event,但注意的是插件需要在
你也可以自己手动重写handle_event,并且符合一下要求:
将原handle_event处创建stack的代码
async with AsyncExitStack() as stack:
替换为以下代码
exdi = require("nonebot_plugin_exdi")
async with exdi.DiBaseParamsManager(bot=bot, event=event) as base_params:
DiBaseParamsManager的模型设计
| 属性 | 说明 |
|---|---|
| bot | bot |
| event | event |
| state | state |
| stack | stack |
| dependency_cache | dependency_cache |
完成重写后,你可以把exdi_overwrite_nb设置为False,把exdi_hand_overwrite设置为True。此时插件不会尝试重写handle_event,但同时会按照重写过handle_event的逻辑运行
结尾
readme模板来自A-kirami
这是萌新的第一个插件,异步学的有限...有问题,大佬不要骂了,欢迎提issue
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nonebot-plugin-exdi-0.1.2.tar.gz.
File metadata
- Download URL: nonebot-plugin-exdi-0.1.2.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.2.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd8b18a5025e57524a6ddeb8cd4149e15021f6f4bfa96255569350e7ed70483e
|
|
| MD5 |
e614ab2f7be288a9294dcdb6381753e8
|
|
| BLAKE2b-256 |
e05eda035066e06e57648003a5d761fc157510ad1a7b135a3990fb7e39918e83
|
File details
Details for the file nonebot_plugin_exdi-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nonebot_plugin_exdi-0.1.2-py3-none-any.whl
- Upload date:
- Size: 22.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.2.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecd5b87a0a36560b787d9bc8a67ff29a06fcb0de8cc2fd428d18fca709dd6d10
|
|
| MD5 |
84811b2204d046e60c12f4f834f8be6a
|
|
| BLAKE2b-256 |
c7ac89aa9e3f423a8c34f95ef44bad4910b2b0a90e29a2171e1a9005db2bb995
|