nonebot-plugin-tempfile
是帮助开发者利用依赖注入特性辅助管理临时文件/IO 的库插件,支持结束会话时自动清理临时文件(夹)。
通过 nb-cli
:
nb plugin install nonebot-plugin-tempfile
from nonebot import require
require("nonebot_plugin_tempfile")
from nonebot_plugin_tempfile import SimpleTempFile, SimpleTempDir
@some_matcher.handle()
async def some_handler(tmpfile: SimpleTempFile):
# tmp is a `pathlib.Path` in function
...
@some_matcher.handle()
async def some_handler(tmpdir: SimpleTempDir):
# tmp is a `pathlib.Path` in function
...
注意:
Simple
开头的依赖类型无法同时使用多个,注入值在同一上下文内相同。如有管理多个临时文件的需求,请另行声明自有依赖。
from nonebot_plugin_tempfile import TempFile, TempDir
@some_matcher.handle()
async def some_handler(tmp1: Path = TempFile(".png"), tmp2: Path = TempFile(".txt", "tmp-someplugin-")):
...
tmpdir = TempDir(prefix="tmp-kazoo-")
tmpdir2 = TempDir(dir=tmpdir)
# 支持目录嵌套
@some_matcher.handle()
async def some_handler(
tmp1: Path = TempFile(".png", dir=tmpdir),
tmp2: Path = TempFile(".txt", "tmp-someplugin-", dir=tmpdir2)
):
...
from io import StringIO, BytesIO
from nonebot_plugin_tempfile import TempStringIO, TempBytesIO
@some_matcher.handle()
async def some_handler(tmp1: StringIO = TempStringIO(), tmp2: BytesIO = TempBytesIO()):
...
@some_matcher.handle()
async def some_handler(tmp1: BytesIO = TempBytesIO(), tmp2: BytesIO = TempBytesIO()):
...
本插件增加了下列可选配置项,有需要的用户请自行在 .env
或选用的以 .env
开头的文件中配置:
# 下列配置项请按需解除注释并配置
# 临时文件默认前缀(字符串)
# tempfile_default_prefix=tmpnonebot