feat(repo): make all cogs pylance-typechecking compliant
at `basic` level, does not include Aurora as it's being rewritten in the `aurora/v3` branch
This commit is contained in:
parent
ea0b7937f8
commit
2a5b924409
11 changed files with 184 additions and 139 deletions
|
@ -2,18 +2,18 @@ import py_compile
|
|||
from asyncio import run_coroutine_threadsafe
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import TYPE_CHECKING, List, Sequence, Tuple
|
||||
from typing import Generator, List, Sequence
|
||||
|
||||
import discord
|
||||
from red_commons.logging import RedTraceLogger, getLogger
|
||||
from redbot.core import Config, checks, commands
|
||||
from redbot.core.bot import Red
|
||||
from redbot.core.core_commands import CoreLogic
|
||||
from redbot.core.utils.chat_formatting import bold, box, humanize_list
|
||||
from typing_extensions import override
|
||||
from watchdog.events import FileSystemEvent, FileSystemMovedEvent, RegexMatchingEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from watchdog.observers import ObserverType
|
||||
from watchdog.observers.api import BaseObserver
|
||||
|
||||
|
||||
class HotReload(commands.Cog):
|
||||
|
@ -21,24 +21,26 @@ class HotReload(commands.Cog):
|
|||
|
||||
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
|
||||
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
|
||||
__version__ = "1.4.0"
|
||||
__version__ = "1.4.1"
|
||||
__documentation__ = "https://seacogs.coastalcommits.com/hotreload/"
|
||||
|
||||
def __init__(self, bot: Red) -> None:
|
||||
super().__init__()
|
||||
self.bot: Red = bot
|
||||
self.config = Config.get_conf(self, identifier=294518358420750336, force_registration=True)
|
||||
self.config: Config = Config.get_conf(cog_instance=self, identifier=294518358420750336, force_registration=True)
|
||||
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload")
|
||||
self.observers: List[ObserverType] = []
|
||||
self.observers: List[BaseObserver] = []
|
||||
self.config.register_global(notify_channel=None, compile_before_reload=False)
|
||||
watchdog_loggers = [getLogger(name="watchdog.observers.inotify_buffer")]
|
||||
for watchdog_logger in watchdog_loggers:
|
||||
watchdog_logger.setLevel("INFO") # SHUT UP!!!!
|
||||
|
||||
@override
|
||||
async def cog_load(self) -> None:
|
||||
"""Start the observer when the cog is loaded."""
|
||||
self.bot.loop.create_task(self.start_observer())
|
||||
_ = self.bot.loop.create_task(self.start_observer())
|
||||
|
||||
@override
|
||||
async def cog_unload(self) -> None:
|
||||
"""Stop the observer when the cog is unloaded."""
|
||||
for observer in self.observers:
|
||||
|
@ -46,6 +48,7 @@ class HotReload(commands.Cog):
|
|||
observer.join()
|
||||
self.logger.info("Stopped observer. No longer watching for file changes.")
|
||||
|
||||
@override
|
||||
def format_help_for_context(self, ctx: commands.Context) -> str:
|
||||
pre_processed = super().format_help_for_context(ctx) or ""
|
||||
n = "\n" if "\n\n" not in pre_processed else ""
|
||||
|
@ -57,7 +60,7 @@ class HotReload(commands.Cog):
|
|||
]
|
||||
return "\n".join(text)
|
||||
|
||||
async def get_paths(self) -> Tuple[Path]:
|
||||
async def get_paths(self) -> Generator[Path]:
|
||||
"""Retrieve user defined paths."""
|
||||
cog_manager = self.bot._cog_mgr # noqa: SLF001 # We have to use this private method because there is no public API to get user defined paths
|
||||
cog_paths = await cog_manager.user_defined_paths()
|
||||
|
@ -79,7 +82,7 @@ class HotReload(commands.Cog):
|
|||
self.logger.warning("Path %s does not exist. Skipping.", path)
|
||||
continue
|
||||
self.logger.debug("Adding observer schedule for path %s.", path)
|
||||
observer.schedule(event_handler=HotReloadHandler(cog=self, path=path), path=path, recursive=True)
|
||||
observer.schedule(event_handler=HotReloadHandler(cog=self, path=path), path=str(path), recursive=True)
|
||||
observer.start()
|
||||
self.logger.info("Started observer. Watching for file changes.")
|
||||
is_first = False
|
||||
|
@ -91,24 +94,24 @@ class HotReload(commands.Cog):
|
|||
pass
|
||||
|
||||
@hotreload_group.command(name="notifychannel")
|
||||
async def hotreload_notifychannel(self, ctx: commands.Context, channel: commands.TextChannelConverter) -> None:
|
||||
async def hotreload_notifychannel(self, ctx: commands.Context, channel: discord.TextChannel) -> None:
|
||||
"""Set the channel to send notifications to."""
|
||||
await self.config.notify_channel.set(channel.id)
|
||||
await ctx.send(f"Notifications will be sent to {channel.mention}.")
|
||||
|
||||
@hotreload_group.command(name="compile")
|
||||
@hotreload_group.command(name="compile") # type: ignore
|
||||
async def hotreload_compile(self, ctx: commands.Context, compile_before_reload: bool) -> None:
|
||||
"""Set whether to compile modified files before reloading."""
|
||||
await self.config.compile_before_reload.set(compile_before_reload)
|
||||
await ctx.send(f"I {'will' if compile_before_reload else 'will not'} compile modified files before hotreloading cogs.")
|
||||
|
||||
@hotreload_group.command(name="list")
|
||||
@hotreload_group.command(name="list") # type: ignore
|
||||
async def hotreload_list(self, ctx: commands.Context) -> None:
|
||||
"""List the currently active observers."""
|
||||
if not self.observers:
|
||||
await ctx.send("No observers are currently active.")
|
||||
return
|
||||
await ctx.send(f"Currently active observers (If there are more than one of these, report an issue): {box(humanize_list(self.observers, style='unit'))}")
|
||||
await ctx.send(f"Currently active observers (If there are more than one of these, report an issue): {box(humanize_list([str(o) for o in self.observers], style='unit'))}")
|
||||
|
||||
|
||||
class HotReloadHandler(RegexMatchingEventHandler):
|
||||
|
@ -129,13 +132,13 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
if event.event_type not in allowed_events:
|
||||
return
|
||||
|
||||
relative_src_path = Path(event.src_path).relative_to(self.path)
|
||||
relative_src_path = Path(str(event.src_path)).relative_to(self.path)
|
||||
src_package_name = relative_src_path.parts[0]
|
||||
cogs_to_reload = [src_package_name]
|
||||
|
||||
if isinstance(event, FileSystemMovedEvent):
|
||||
dest = f" to {event.dest_path}"
|
||||
relative_dest_path = Path(event.dest_path).relative_to(self.path)
|
||||
relative_dest_path = Path(str(event.dest_path)).relative_to(self.path)
|
||||
dest_package_name = relative_dest_path.parts[0]
|
||||
if dest_package_name != src_package_name:
|
||||
cogs_to_reload.append(dest_package_name)
|
||||
|
@ -147,7 +150,7 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
run_coroutine_threadsafe(
|
||||
coro=self.reload_cogs(
|
||||
cog_names=cogs_to_reload,
|
||||
paths=[Path(p) for p in (event.src_path, getattr(event, "dest_path", None)) if p],
|
||||
paths=[Path(str(p)) for p in (event.src_path, getattr(event, "dest_path", None)) if p],
|
||||
),
|
||||
loop=self.cog.bot.loop,
|
||||
)
|
||||
|
@ -163,7 +166,7 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
self.logger.info("Reloaded cogs: %s", humanize_list(cog_names, style="unit"))
|
||||
|
||||
channel = self.cog.bot.get_channel(await self.cog.config.notify_channel())
|
||||
if channel:
|
||||
if channel and isinstance(channel, discord.TextChannel):
|
||||
await channel.send(f"Reloaded cogs: {humanize_list(cog_names, style='unit')}")
|
||||
|
||||
def compile_modified_files(self, cog_names: Sequence[str], paths: Sequence[Path]) -> bool:
|
||||
|
@ -176,7 +179,7 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
try:
|
||||
with NamedTemporaryFile() as temp_file:
|
||||
self.logger.debug("Attempting to compile %s", path)
|
||||
py_compile.compile(file=path, cfile=temp_file.name, doraise=True)
|
||||
py_compile.compile(file=str(path), cfile=temp_file.name, doraise=True)
|
||||
self.logger.debug("Successfully compiled %s", path)
|
||||
|
||||
except py_compile.PyCompileError as e:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue