nonebot-plugin-wait-a-minute 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 shoucandanghehe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,64 @@
1
+ Metadata-Version: 2.1
2
+ Name: nonebot-plugin-wait-a-minute
3
+ Version: 0.1.0
4
+ Summary: A nonebot plugin for running some func before closing the bot
5
+ Author-Email: shoucandanghehe <wallfjjd@gmail.com>
6
+ License: MIT
7
+ Requires-Python: >=3.9
8
+ Requires-Dist: nonebot2>=2.3.2
9
+ Description-Content-Type: text/markdown
10
+
11
+ <!-- markdownlint-disable MD033 MD036 MD041 -->
12
+ <div align="center">
13
+ <a href="https://v2.nonebot.dev/store">
14
+ <img src="./img/NoneBotPlugin.png" width="300" alt="logo" />
15
+ </a>
16
+
17
+ # NoneBot Plugin Wait a minute
18
+
19
+ ✨ A nonebot plugin for running some func before closing the bot ✨
20
+
21
+ ![Python](https://img.shields.io/badge/Python-3.9+-blue.svg)
22
+ ![PyPI - Version](https://img.shields.io/pypi/v/nonebot-plugin-wait-a-minute)
23
+ [![pdm-managed](https://img.shields.io/endpoint?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fpdm-project%2F.github%2Fbadge.json)](https://pdm-project.org)
24
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
25
+ </div>
26
+
27
+ ## 💿 Install
28
+
29
+ ### 🚀 Use PDM
30
+
31
+ ```bash
32
+ pdm add nonebot-plugin-wait-a-minute
33
+ ```
34
+
35
+ ### 🚀 Use poetry
36
+
37
+ ```bash
38
+ poetry add nonebot-plugin-wait-a-minute
39
+ ```
40
+
41
+ ## ♿️ How To Use
42
+
43
+ ```python
44
+ from nonebot import require
45
+
46
+ require('nonebot_plugin_wait_a_minute') # require plugin
47
+
48
+ from nonebot_plugin_wait_a_minute import on_shutdown_before
49
+
50
+ @on_shutdown_before
51
+ def _():
52
+ # do something
53
+ ...
54
+
55
+ # or use async func
56
+ @on_shutdown_before
57
+ async def _():
58
+ # await do something
59
+ ...
60
+ ```
61
+
62
+ ## 📄 LICENSE
63
+
64
+ This project is open source using the [MIT](./LICENSE) license
@@ -0,0 +1,54 @@
1
+ <!-- markdownlint-disable MD033 MD036 MD041 -->
2
+ <div align="center">
3
+ <a href="https://v2.nonebot.dev/store">
4
+ <img src="./img/NoneBotPlugin.png" width="300" alt="logo" />
5
+ </a>
6
+
7
+ # NoneBot Plugin Wait a minute
8
+
9
+ ✨ A nonebot plugin for running some func before closing the bot ✨
10
+
11
+ ![Python](https://img.shields.io/badge/Python-3.9+-blue.svg)
12
+ ![PyPI - Version](https://img.shields.io/pypi/v/nonebot-plugin-wait-a-minute)
13
+ [![pdm-managed](https://img.shields.io/endpoint?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fpdm-project%2F.github%2Fbadge.json)](https://pdm-project.org)
14
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
15
+ </div>
16
+
17
+ ## 💿 Install
18
+
19
+ ### 🚀 Use PDM
20
+
21
+ ```bash
22
+ pdm add nonebot-plugin-wait-a-minute
23
+ ```
24
+
25
+ ### 🚀 Use poetry
26
+
27
+ ```bash
28
+ poetry add nonebot-plugin-wait-a-minute
29
+ ```
30
+
31
+ ## ♿️ How To Use
32
+
33
+ ```python
34
+ from nonebot import require
35
+
36
+ require('nonebot_plugin_wait_a_minute') # require plugin
37
+
38
+ from nonebot_plugin_wait_a_minute import on_shutdown_before
39
+
40
+ @on_shutdown_before
41
+ def _():
42
+ # do something
43
+ ...
44
+
45
+ # or use async func
46
+ @on_shutdown_before
47
+ async def _():
48
+ # await do something
49
+ ...
50
+ ```
51
+
52
+ ## 📄 LICENSE
53
+
54
+ This project is open source using the [MIT](./LICENSE) license
@@ -0,0 +1,92 @@
1
+ from __future__ import annotations
2
+
3
+ import signal
4
+ from asyncio import Task, gather, get_event_loop
5
+ from enum import Enum, auto
6
+ from threading import Lock
7
+ from types import FrameType
8
+ from typing import Any, Callable, ClassVar, Optional, TypeVar
9
+
10
+ from nonebot import get_driver
11
+ from nonebot.log import logger
12
+ from nonebot.plugin import PluginMetadata
13
+ from nonebot.utils import is_coroutine_callable, run_sync
14
+ from typing_extensions import TypeAlias
15
+
16
+ __plugin_meta__ = PluginMetadata(
17
+ name='nonebot-plugin-wait-a-minute',
18
+ description='A nonebot plugin for waiting for an event to complete before closing it',
19
+ usage='@on_shutdown_before',
20
+ type='library',
21
+ homepage='https://github.com/shoucandanghehe/nonebot-plugin-wait-a-minute',
22
+ config=None,
23
+ supported_adapters=None,
24
+ )
25
+
26
+ T = TypeVar('T')
27
+
28
+ SigHandleFunc: TypeAlias = Callable[[int, Optional[FrameType]], Any]
29
+
30
+ driver = get_driver()
31
+
32
+
33
+ class Status(Enum):
34
+ NOT_RUNNING_YET = auto()
35
+ RUNNING = auto()
36
+ FINISHED = auto()
37
+
38
+
39
+ class Hook:
40
+ status: Status = Status.NOT_RUNNING_YET
41
+ funcs: ClassVar[list[Callable[[], Any]]] = []
42
+ tasks: ClassVar[list[Task]] = []
43
+ callback: Task
44
+ lock: Lock = Lock()
45
+
46
+ @classmethod
47
+ def register(cls, func: Callable[[], T]) -> Callable[[], T]:
48
+ cls.funcs.append(func)
49
+ return func
50
+
51
+ @classmethod
52
+ def sig_handle(cls, original_handle: signal._HANDLER) -> SigHandleFunc:
53
+ def inner(signum: int, frame: FrameType | None) -> None: # noqa: ARG001
54
+ with cls.lock:
55
+ if not cls.funcs:
56
+ cls.status = Status.FINISHED
57
+
58
+ if cls.status == Status.NOT_RUNNING_YET:
59
+ loop = get_event_loop()
60
+ for i in cls.funcs:
61
+ if is_coroutine_callable(i):
62
+ cls.tasks.append(loop.create_task(i()))
63
+ else:
64
+ cls.tasks.append(loop.create_task(run_sync(i())()))
65
+ cls.status = Status.RUNNING
66
+ cls.callback = loop.create_task(cls.check_task(signum))
67
+ elif cls.status == Status.RUNNING:
68
+ logger.warning(f'signal {signum} received, but wait a minute pls')
69
+ elif cls.status == Status.FINISHED:
70
+ signal.signal(signum, original_handle)
71
+ signal.raise_signal(signum)
72
+
73
+ return inner
74
+
75
+ @classmethod
76
+ async def check_task(cls, signum: int) -> None:
77
+ if cls.tasks:
78
+ await gather(*cls.tasks)
79
+ cls.status = Status.FINISHED
80
+ signal.raise_signal(signum)
81
+
82
+
83
+ @driver.on_startup
84
+ async def _() -> None:
85
+ signal.signal(signal.SIGINT, Hook.sig_handle(signal.getsignal(signal.SIGINT)))
86
+ signal.signal(signal.SIGTERM, Hook.sig_handle(signal.getsignal(signal.SIGTERM)))
87
+ logger.success('Signal hook installed')
88
+
89
+
90
+ on_shutdown_before = Hook.register
91
+
92
+ __all__ = ['on_shutdown_before']
@@ -0,0 +1,115 @@
1
+ [project]
2
+ name = "nonebot-plugin-wait-a-minute"
3
+ version = "0.1.0"
4
+ description = "A nonebot plugin for running some func before closing the bot"
5
+ authors = [
6
+ { name = "shoucandanghehe", email = "wallfjjd@gmail.com" },
7
+ ]
8
+ dependencies = [
9
+ "nonebot2>=2.3.2",
10
+ ]
11
+ requires-python = ">=3.9"
12
+ readme = "README.md"
13
+
14
+ [project.license]
15
+ text = "MIT"
16
+
17
+ [build-system]
18
+ requires = [
19
+ "pdm-backend",
20
+ ]
21
+ build-backend = "pdm.backend"
22
+
23
+ [tool.pdm]
24
+ distribution = true
25
+
26
+ [tool.pdm.dev-dependencies]
27
+ dev = [
28
+ "nonebot2[all]>=2.3.2",
29
+ "ruff>=0.6.1",
30
+ "mypy>=1.11.1",
31
+ ]
32
+
33
+ [tool.ruff]
34
+ line-length = 120
35
+ target-version = "py39"
36
+
37
+ [tool.ruff.lint]
38
+ select = [
39
+ "F",
40
+ "E",
41
+ "W",
42
+ "C90",
43
+ "I",
44
+ "N",
45
+ "UP",
46
+ "YTT",
47
+ "ANN",
48
+ "ASYNC",
49
+ "S",
50
+ "BLE",
51
+ "FBT",
52
+ "B",
53
+ "A",
54
+ "COM",
55
+ "C4",
56
+ "DTZ",
57
+ "T10",
58
+ "EM",
59
+ "FA",
60
+ "ISC",
61
+ "ICN",
62
+ "PIE",
63
+ "T20",
64
+ "PYI",
65
+ "Q",
66
+ "RSE",
67
+ "RET",
68
+ "SLF",
69
+ "SLOT",
70
+ "SIM",
71
+ "TID",
72
+ "TCH",
73
+ "ARG",
74
+ "PTH",
75
+ "ERA",
76
+ "PD",
77
+ "PGH",
78
+ "PL",
79
+ "TRY",
80
+ "FLY",
81
+ "FAST",
82
+ "PERF",
83
+ "FURB",
84
+ "RUF",
85
+ ]
86
+ ignore = [
87
+ "E501",
88
+ "ANN101",
89
+ "ANN102",
90
+ "ANN202",
91
+ "TRY003",
92
+ "COM812",
93
+ "TID252",
94
+ "ISC001",
95
+ ]
96
+
97
+ [tool.ruff.lint.flake8-quotes]
98
+ inline-quotes = "single"
99
+ multiline-quotes = "double"
100
+
101
+ [tool.ruff.lint.flake8-annotations]
102
+ mypy-init-return = true
103
+
104
+ [tool.ruff.lint.flake8-builtins]
105
+ builtins-ignorelist = [
106
+ "id",
107
+ ]
108
+
109
+ [tool.ruff.format]
110
+ quote-style = "single"
111
+
112
+ [tool.nonebot]
113
+ plugins = [
114
+ "nonebot_plugin_wait_a_minute",
115
+ ]