HotReload Cog #49

Merged
cswimr merged 8 commits from hotreload into main 2025-01-25 18:51:28 -05:00
5 changed files with 95 additions and 0 deletions
Showing only changes of commit bf907ac0e1 - Show all commits

5
hotreload/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from .hotreload import HotReload
async def setup(bot):
await bot.add_cog(HotReload(bot))

70
hotreload/hotreload.py Normal file
View file

@ -0,0 +1,70 @@
from pathlib import Path
from red_commons.logging import RedTraceLogger, getLogger
from redbot.core import commands
from redbot.core.bot import Red
from redbot.core.core_commands import CoreLogic
from redbot.core.utils.chat_formatting import bold, humanize_list
from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
class HotReload(commands.Cog):
"""Automatically reload cogs in local cog paths on file change."""
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
__version__ = "1.0.0-rc1"
__documentation__ = "https://seacogs.coastalcommits.com/hotreload/"
def __init__(self, bot: Red) -> None:
super().__init__()
self.bot: Red = bot
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload")
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 ""
text = [
f"{pre_processed}{n}",
f"{bold('Cog Version:')} [{self.__version__}]({self.__git__})",
f"{bold('Author:')} {humanize_list(self.__author__)}",
f"{bold('Documentation:')} {self.__documentation__}",
]
return "\n".join(text)
async def get_paths(self) -> tuple[Path]:
"""Retrieve user defined paths."""
cog_manager = self.bot._cog_mgr
cog_paths = await cog_manager.user_defined_paths()
return (Path(path) for path in cog_paths)
async def start_observer(self) -> None:
"""Start the observer to watch for file changes."""
observer = Observer()
for path in await self.get_paths():
observer.schedule(HotReloadHandler(self.bot, path), path, recursive=True)
observer.start()
self.logger.info("Started observer.")
class HotReloadHandler(FileSystemEventHandler):
"""Handler for file changes."""
def __init__(self, bot: Red, path: Path) -> None:
self.bot: Red = bot
self.path: Path = path
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReloadHandler")
def on_modified(self, event: FileSystemEvent) -> None:
"""Handle file modification events."""
if event.is_directory:
return
self.logger.info(f"File {event.src_path} has been modified.")
# self.bot.loop.create_task(self.reload_cog())
async def reload_cog(self, cog_name: str) -> None:
"""Reload modified cog."""
core_logic = CoreLogic(bot=self.bot)
core_logic._reload(pkg_names=(cog_name,))
self.logger.info(f"Reloaded {cog_name} cog.")

17
hotreload/info.json Normal file
View file

@ -0,0 +1,17 @@
{
"author" : ["cswimr"],
"install_msg" : "Thank you for installing HotReload!",
"name" : "HotReload",
"short" : "Automatically reload cogs in local cog paths on file change.",
"description" : "Automatically reload cogs in local cog paths on file change.",
"end_user_data_statement" : "This cog does not store end user data.",
"hidden": false,
"disabled": false,
"min_bot_version": "3.5.0",
"min_python_version": [3, 10, 0],
"requirements": ["watchdog"],
"tags": [
"utility",
"development"
]
}

View file

@ -18,6 +18,7 @@ dependencies = [
"py-dactyl", "py-dactyl",
"pydantic>=2.9.2", "pydantic>=2.9.2",
"red-discordbot>=3.5.14", "red-discordbot>=3.5.14",
"watchdog>=5.0.3",
"websockets>=13.1", "websockets>=13.1",
] ]

2
uv.lock generated
View file

@ -1667,6 +1667,7 @@ dependencies = [
{ name = "py-dactyl" }, { name = "py-dactyl" },
{ name = "pydantic" }, { name = "pydantic" },
{ name = "red-discordbot" }, { name = "red-discordbot" },
{ name = "watchdog" },
{ name = "websockets" }, { name = "websockets" },
] ]
@ -1706,6 +1707,7 @@ requires-dist = [
{ name = "py-dactyl", git = "https://github.com/cswimr/pydactyl" }, { name = "py-dactyl", git = "https://github.com/cswimr/pydactyl" },
{ name = "pydantic", specifier = ">=2.9.2" }, { name = "pydantic", specifier = ">=2.9.2" },
{ name = "red-discordbot", specifier = ">=3.5.14" }, { name = "red-discordbot", specifier = ">=3.5.14" },
{ name = "watchdog", specifier = ">=5.0.3" },
{ name = "websockets", specifier = ">=13.1" }, { name = "websockets", specifier = ">=13.1" },
] ]