适用于 NoneBot2 插件的临时文件/IO 依赖注入支持
Project description
介绍
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)
):
...
注入虚拟 IO
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
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file nonebot_plugin_tempfile-0.2.0.tar.gz
.
File metadata
- Download URL: nonebot_plugin_tempfile-0.2.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d331be47f42cf22d4b5854dce52d7e12e9294c3b6ca57ccd9ddd62e19dff6d39 |
|
MD5 | 282ec95c3d3ee8711afbbdf8bd75045e |
|
BLAKE2b-256 | e361842fcaa914242022dc85838b473a29354f5fe0474cf8507d748c6539ad39 |
File details
Details for the file nonebot_plugin_tempfile-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: nonebot_plugin_tempfile-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca611dc6cceda3443757db9e43c85eb790688488f492c5ecafd467cd0b68a957 |
|
MD5 | 46abf53a701a9ec58a248eb8875d7032 |
|
BLAKE2b-256 | 8e00d79ef7bafe3abc9b150a2ed71f8f64d925d95ce4f2ae7205610b267b5206 |