Enabled all Ruff lint rules and resolved findings with justified inline suppressions.
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 14s
CI / Tests (Python 3.13) (push) Successful in 14s
CI / Tests (Python 3.14) (push) Successful in 11s
CI / Type Checking (push) Successful in 10s
CI / Spelling (push) Successful in 5s
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 14s
CI / Tests (Python 3.13) (push) Successful in 14s
CI / Tests (Python 3.14) (push) Successful in 11s
CI / Type Checking (push) Successful in 10s
CI / Spelling (push) Successful in 5s
This commit is contained in:
@@ -26,15 +26,16 @@ import time
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import TYPE_CHECKING, cast
|
||||
|
||||
from ..api.commands import CommandEvent, CommandHandler, CommandInfo, CommandMark
|
||||
from ..api.context import CommandContext, EventContext
|
||||
from ..api.event_types import ChatEvent
|
||||
from owlbot._version import __version__
|
||||
from owlbot.api.commands import CommandEvent, CommandHandler, CommandInfo, CommandMark
|
||||
from owlbot.api.context import CommandContext, EventContext
|
||||
from owlbot.api.event_types import ChatEvent
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from types import ModuleType
|
||||
|
||||
from ..api.context import ModuleContext
|
||||
from ..api.owncast_client import OwncastClient
|
||||
from owlbot.api.context import ModuleContext
|
||||
from owlbot.api.owncast_client import OwncastClient
|
||||
|
||||
type BuiltinCommandHandler = Callable[[ChatEvent, "OwncastClient"], Awaitable[None]]
|
||||
|
||||
@@ -72,7 +73,7 @@ class CommandRegistry:
|
||||
aliases: list[str] | tuple[str, ...] | None = None,
|
||||
requires_authenticated: bool = False,
|
||||
requires_moderator: bool = False,
|
||||
cooldown: int | float = 0,
|
||||
cooldown: int = 0,
|
||||
module_name: str,
|
||||
) -> None:
|
||||
"""Register a command handler.
|
||||
@@ -317,7 +318,7 @@ class CommandDispatcher:
|
||||
aliases: list[str] | tuple[str, ...] | None = None,
|
||||
requires_authenticated: bool = False,
|
||||
requires_moderator: bool = False,
|
||||
cooldown: int | float = 0,
|
||||
cooldown: int = 0,
|
||||
module_name: str,
|
||||
) -> None:
|
||||
"""Register a command handler.
|
||||
@@ -438,14 +439,17 @@ class CommandDispatcher:
|
||||
user = event.user
|
||||
|
||||
logger.info(
|
||||
f"Command '{command_info.name}' invoked by {user.display_name}"
|
||||
f" with args: {args!r}"
|
||||
"Command '%s' invoked by %s with args: %r",
|
||||
command_info.name,
|
||||
user.display_name,
|
||||
args,
|
||||
)
|
||||
|
||||
if command_info.requires_authenticated and not user.is_authenticated:
|
||||
logger.info(
|
||||
f"Command '{command_name}' denied for {user.display_name}: "
|
||||
"authentication required"
|
||||
"Command '%s' denied for %s: authentication required",
|
||||
command_name,
|
||||
user.display_name,
|
||||
)
|
||||
await self._owncast_client.send_system_message_to_client(
|
||||
event.client_id,
|
||||
@@ -455,8 +459,9 @@ class CommandDispatcher:
|
||||
|
||||
if command_info.requires_moderator and not user.is_moderator:
|
||||
logger.info(
|
||||
f"Command '{command_name}' denied for {user.display_name}: "
|
||||
"moderator required"
|
||||
"Command '%s' denied for %s: moderator required",
|
||||
command_name,
|
||||
user.display_name,
|
||||
)
|
||||
await self._owncast_client.send_system_message_to_client(
|
||||
event.client_id,
|
||||
@@ -470,8 +475,10 @@ class CommandDispatcher:
|
||||
if last is not None and now - last < command_info.cooldown:
|
||||
remaining = math.ceil(command_info.cooldown - (now - last))
|
||||
logger.info(
|
||||
f"Command '{command_name}' denied for {user.display_name}: "
|
||||
f"on cooldown ({remaining}s remaining)"
|
||||
"Command '%s' denied for %s: on cooldown (%ds remaining)",
|
||||
command_name,
|
||||
user.display_name,
|
||||
remaining,
|
||||
)
|
||||
await self._owncast_client.send_system_message_to_client(
|
||||
event.client_id,
|
||||
@@ -501,12 +508,14 @@ class CommandDispatcher:
|
||||
)
|
||||
except TimeoutError:
|
||||
logger.warning(
|
||||
f"Built-in command '{command_info.name}' "
|
||||
f"cancelled after {self._handler_timeout}s timeout."
|
||||
"Built-in command '%s' cancelled after %ss timeout.",
|
||||
command_info.name,
|
||||
self._handler_timeout,
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
logger.exception(
|
||||
f"Built-in command '{command_info.name}' raised exception: {e}"
|
||||
"Built-in command '%s' raised exception.",
|
||||
command_info.name,
|
||||
)
|
||||
return
|
||||
|
||||
@@ -544,26 +553,25 @@ class CommandDispatcher:
|
||||
)
|
||||
except TimeoutError:
|
||||
logger.warning(
|
||||
f"Command handler '{command_info.name}' "
|
||||
f"from module '{command_info.module_name}' "
|
||||
f"cancelled after "
|
||||
f"{self._handler_timeout}s timeout."
|
||||
"Command handler '%s' from module '%s' cancelled after %ss timeout.",
|
||||
command_info.name,
|
||||
command_info.module_name,
|
||||
self._handler_timeout,
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
logger.exception(
|
||||
f"Command handler '{command_info.name}' "
|
||||
f"from module '{command_info.module_name}' "
|
||||
f"raised exception: {e}"
|
||||
"Command handler '%s' from module '%s' raised exception.",
|
||||
command_info.name,
|
||||
command_info.module_name,
|
||||
)
|
||||
|
||||
def _register_builtin_commands(self) -> None:
|
||||
"""Register all built-in commands."""
|
||||
from .._version import __version__
|
||||
|
||||
loaded_modules = self._loaded_modules
|
||||
|
||||
async def about_with_modules(
|
||||
event: ChatEvent, owncast_client: OwncastClient
|
||||
event: ChatEvent, # noqa: ARG001 # required by builtin handler signature
|
||||
owncast_client: OwncastClient,
|
||||
) -> None:
|
||||
module_count = len(loaded_modules)
|
||||
module_list = (
|
||||
@@ -612,7 +620,7 @@ class ModuleCommands:
|
||||
aliases: list[str] | tuple[str, ...] | None = None,
|
||||
requires_authenticated: bool = False,
|
||||
requires_moderator: bool = False,
|
||||
cooldown: int | float = 0,
|
||||
cooldown: int = 0,
|
||||
) -> None:
|
||||
"""Register a command handler for this module.
|
||||
|
||||
|
||||
+15
-16
@@ -26,9 +26,9 @@ import time
|
||||
from collections import defaultdict
|
||||
from typing import TYPE_CHECKING, NamedTuple, cast
|
||||
|
||||
from ..api.context import EventContext, PropagationState
|
||||
from ..api.event_types import ChatEvent, Event, EventType, log_event
|
||||
from ..api.events import EventHandler, EventMark, Priority
|
||||
from owlbot.api.context import EventContext, PropagationState
|
||||
from owlbot.api.event_types import ChatEvent, Event, EventType, log_event
|
||||
from owlbot.api.events import EventHandler, EventMark, Priority
|
||||
|
||||
|
||||
class HandlerEntry(NamedTuple):
|
||||
@@ -45,7 +45,7 @@ if TYPE_CHECKING:
|
||||
from collections.abc import Awaitable, Callable
|
||||
from types import ModuleType
|
||||
|
||||
from ..api.context import ModuleContext
|
||||
from owlbot.api.context import ModuleContext
|
||||
|
||||
logger = logging.getLogger("owlbot.events")
|
||||
|
||||
@@ -344,9 +344,7 @@ class EventDispatcher:
|
||||
)
|
||||
break
|
||||
|
||||
await self._call_handler(
|
||||
handler, event, event_type, module_name, propagation
|
||||
)
|
||||
await self._call_handler(handler, event, module_name, propagation)
|
||||
else:
|
||||
logger.debug("No handlers registered for event type: %s", event_type)
|
||||
|
||||
@@ -362,14 +360,13 @@ class EventDispatcher:
|
||||
else:
|
||||
try:
|
||||
await self._command_dispatch(event)
|
||||
except Exception as e:
|
||||
logger.exception(f"Command dispatch failed: {e}")
|
||||
except Exception:
|
||||
logger.exception("Command dispatch failed.")
|
||||
|
||||
async def _call_handler(
|
||||
self,
|
||||
handler: EventHandler,
|
||||
event: Event,
|
||||
event_type: EventType,
|
||||
module_name: str,
|
||||
propagation: PropagationState,
|
||||
) -> None:
|
||||
@@ -377,7 +374,6 @@ class EventDispatcher:
|
||||
|
||||
:param handler: The handler function to call.
|
||||
:param event: The event to pass to the handler.
|
||||
:param event_type: The type of event being dispatched.
|
||||
:param module_name: The module that owns this handler.
|
||||
:param propagation: Shared propagation state for this dispatch cycle.
|
||||
"""
|
||||
@@ -400,13 +396,16 @@ class EventDispatcher:
|
||||
logger.debug("Handler '%s' completed in %.1fms.", handler_name, elapsed)
|
||||
except TimeoutError:
|
||||
logger.warning(
|
||||
f"Handler '{handler_name}' from module '{module_name}' "
|
||||
f"cancelled after {self._handler_timeout}s timeout."
|
||||
"Handler '%s' from module '%s' cancelled after %ss timeout.",
|
||||
handler_name,
|
||||
module_name,
|
||||
self._handler_timeout,
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
logger.exception(
|
||||
f"Handler '{handler_name}' from module "
|
||||
f"'{module_name}' raised exception: {e}"
|
||||
"Handler '%s' from module '%s' raised exception.",
|
||||
handler_name,
|
||||
module_name,
|
||||
)
|
||||
|
||||
|
||||
|
||||
+13
-10
@@ -30,14 +30,14 @@ import orjson
|
||||
from aiohttp import web
|
||||
from aiohttp.web import DynamicResource
|
||||
|
||||
from ..api.context import RouteContext
|
||||
from ..api.routes import RouteHandler, RouteInfo, RouteMark
|
||||
from owlbot.api.context import RouteContext
|
||||
from owlbot.api.routes import RouteHandler, RouteInfo, RouteMark
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
from types import ModuleType
|
||||
|
||||
from ..api.context import ModuleContext
|
||||
from owlbot.api.context import ModuleContext
|
||||
|
||||
logger = logging.getLogger("owlbot.web")
|
||||
|
||||
@@ -270,7 +270,7 @@ class RouteRegistry:
|
||||
"""
|
||||
method = method.upper()
|
||||
for group in self._groups:
|
||||
match_dict = group.resource._match(full_path) # no public API alternative
|
||||
match_dict = group.resource._match(full_path) # noqa: SLF001 # no public API alternative
|
||||
if match_dict is not None:
|
||||
for handler in group.handlers:
|
||||
if method in handler.methods:
|
||||
@@ -615,20 +615,23 @@ class RouteDispatcher:
|
||||
)
|
||||
# pragma: no branch — defensive against untyped handlers
|
||||
mod_logger.error( # type: ignore[unreachable]
|
||||
f"Route handler '{route_info.full_path}' returned "
|
||||
f"unsupported type: {type(result).__name__}"
|
||||
"Route handler '%s' returned unsupported type: %s",
|
||||
route_info.full_path,
|
||||
type(result).__name__,
|
||||
)
|
||||
return web.Response(status=500)
|
||||
|
||||
except TimeoutError:
|
||||
mod_logger.warning(
|
||||
f"Route handler '{route_info.full_path}' timed out "
|
||||
f"after {self._handler_timeout}s."
|
||||
"Route handler '%s' timed out after %ss.",
|
||||
route_info.full_path,
|
||||
self._handler_timeout,
|
||||
)
|
||||
return web.Response(status=500)
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
mod_logger.exception(
|
||||
f"Route handler '{route_info.full_path}' raised exception: {e}"
|
||||
"Route handler '%s' raised exception.",
|
||||
route_info.full_path,
|
||||
)
|
||||
return web.Response(status=500)
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user