Skip to content

Commit d1ac348

Browse files
committed
refactor: rename command attributes for clarity and consistency
1 parent 78b62eb commit d1ac348

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

src/korone/filters/command.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,22 @@ class Command(Filter):
153153
prefixes, case sensitivity, and mention requirements.
154154
155155
Attributes:
156-
commands: Compiled patterns of allowed commands
157-
prefixes: Command prefixes to recognize
156+
command_patterns: Compiled patterns of allowed commands
157+
command_prefixes: Command prefixes to recognize
158158
ignore_case: Whether to ignore case when matching commands
159159
ignore_mention: Whether to ignore bot mentions after commands
160160
magic: Optional magic filter to further restrict command matches
161161
disableable: Whether this command can be disabled in specific chats
162162
"""
163163

164-
__slots__ = ("commands", "disableable", "ignore_case", "ignore_mention", "magic", "prefixes")
164+
__slots__ = (
165+
"command_patterns",
166+
"command_prefixes",
167+
"disableable",
168+
"ignore_case",
169+
"ignore_mention",
170+
"magic",
171+
)
165172

166173
def __init__(
167174
self,
@@ -187,14 +194,14 @@ def __init__(
187194
Raises:
188195
ValueError: If no command patterns are provided
189196
"""
190-
self.prefixes = prefixes
197+
self.command_prefixes = prefixes
191198
self.ignore_case = ignore_case
192199
self.ignore_mention = ignore_mention
193200
self.magic = magic
194201
self.disableable = disableable
195-
self.commands = tuple(self._prepare_commands(values, commands))
202+
self.command_patterns = tuple(self._prepare_commands(values, commands))
196203

197-
if not self.commands:
204+
if not self.command_patterns:
198205
msg = "Command filter requires at least one command."
199206
raise ValueError(msg)
200207

@@ -272,7 +279,11 @@ def validate_prefix(self, command: CommandObject) -> None:
272279
CommandError: If the prefix is not among the allowed prefixes
273280
"""
274281
prefix = command.prefix.lower() if self.ignore_case and command.prefix else command.prefix
275-
valid_prefixes = [p.lower() for p in self.prefixes] if self.ignore_case else self.prefixes
282+
valid_prefixes = (
283+
[p.lower() for p in self.command_prefixes]
284+
if self.ignore_case
285+
else self.command_prefixes
286+
)
276287
if prefix not in valid_prefixes:
277288
msg = f"Invalid prefix: {command.prefix!r}."
278289
raise CommandError(msg)
@@ -306,7 +317,7 @@ def validate_command(self, command: CommandObject) -> CommandObject:
306317
CommandError: If the command doesn't match any allowed pattern
307318
"""
308319
cmd_name = command.command.lower() if self.ignore_case else command.command
309-
for allowed_cmd in self.commands:
320+
for allowed_cmd in self.command_patterns:
310321
match = allowed_cmd.match(cmd_name)
311322
if match:
312323
return replace(command, regexp_match=match)

src/korone/modules/core.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def fetch_command_state(command: str) -> Documents | None:
8282
return result
8383

8484

85-
def extract_commands(filters: Filter) -> list[str] | None:
85+
def extract_commands(filters: Filter) -> list[str | None] | None:
8686
"""
8787
Extracts command patterns from filter objects.
8888
@@ -92,17 +92,20 @@ def extract_commands(filters: Filter) -> list[str] | None:
9292
Returns:
9393
A list of command patterns or None if no commands found.
9494
"""
95+
# Import here to avoid circular import
96+
from korone.filters import Command, Regex # noqa: PLC0415
97+
9598
try:
96-
if hasattr(filters, "commands"):
97-
return [cmd.pattern for cmd in filters.commands if filters.disableable] # type: ignore
98-
if hasattr(filters, "friendly_name"):
99-
return [filters.friendly_name] # type: ignore
99+
if isinstance(filters, Command) and filters.disableable:
100+
return [cmd.pattern for cmd in filters.command_patterns]
101+
if isinstance(filters, Regex):
102+
return [filters.friendly_name]
100103
except AttributeError:
101104
pass
102105
return None
103106

104107

105-
async def update_command_structure(commands: list[str]) -> None:
108+
async def update_command_structure(commands: list[str | None]) -> None:
106109
"""
107110
Updates the command structure with parent-child relationships and chat states.
108111

src/korone/modules/errors/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626

2727
ERROR_NOTIFICATION_TTL: Final[float] = 300.0
28-
_error_notification_state: dict[str, float] = {}
28+
_ERROR_NOTIFICATION_STATE: dict[str, float] = {}
2929

3030

3131
def compute_error_signature(exc: BaseException) -> str:
@@ -37,18 +37,18 @@ def compute_error_signature(exc: BaseException) -> str:
3737
def should_notify(signature: str, *, ttl: float = ERROR_NOTIFICATION_TTL) -> bool:
3838
now = time.monotonic()
3939

40-
expiry = _error_notification_state.get(signature)
40+
expiry = _ERROR_NOTIFICATION_STATE.get(signature)
4141
if expiry and expiry > now:
4242
return False
4343

44-
_error_notification_state[signature] = now + ttl
44+
_ERROR_NOTIFICATION_STATE[signature] = now + ttl
4545
_purge_expired_notifications(now)
4646
return True
4747

4848

4949
def _purge_expired_notifications(now: float) -> None:
5050
expired_signatures = [
51-
key for key, expiry in _error_notification_state.items() if expiry <= now
51+
key for key, expiry in _ERROR_NOTIFICATION_STATE.items() if expiry <= now
5252
]
5353
for signature in expired_signatures:
54-
_error_notification_state.pop(signature, None)
54+
_ERROR_NOTIFICATION_STATE.pop(signature, None)

src/korone/utils/concurrency.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
from __future__ import annotations
55

6-
import typing as t
76
from functools import partial
87
from os import cpu_count
8+
from typing import TYPE_CHECKING, Any, Final
99

1010
from anyio import CapacityLimiter, to_thread
1111

12-
_DEFAULT_THREAD_TOKENS: t.Final[int] = max(16, min(64, (cpu_count() or 1) * 5))
13-
BLOCKING_CALLS_LIMITER: t.Final[CapacityLimiter] = CapacityLimiter(_DEFAULT_THREAD_TOKENS)
12+
if TYPE_CHECKING:
13+
from collections.abc import Callable
14+
15+
_DEFAULT_THREAD_TOKENS: Final[int] = max(16, min(64, (cpu_count() or 1) * 5))
16+
BLOCKING_CALLS_LIMITER: Final[CapacityLimiter] = CapacityLimiter(_DEFAULT_THREAD_TOKENS)
1417

1518

1619
async def run_blocking[R](
17-
func: t.Callable[..., R],
20+
func: Callable[..., R],
1821
/,
19-
*args: t.Any,
22+
*args: Any,
2023
limiter: CapacityLimiter | None = None,
2124
cancellable: bool = False,
2225
abandon_on_cancel: bool = False,
23-
**kwargs: t.Any,
26+
**kwargs: Any,
2427
) -> R:
2528
"""Run a blocking callable in a worker thread.
2629

0 commit comments

Comments
 (0)