Skip to content

Commit

Permalink
✨ auto_reload support plugins.$files
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 29, 2024
1 parent 8948a6d commit 8ef06cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
20 changes: 12 additions & 8 deletions arclet/entari/builtins/auto_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,20 @@ async def watch(self):
logger("ERROR", f"Failed to reload <blue>{self.fail[change[1]]!r}</blue>")

async def watch_config(self):
file = EntariConfig.instance.path
async for event in awatch(file.resolve().absolute().parent, recursive=False):
file = EntariConfig.instance.path.resolve()
extra = [file.parent.joinpath(path) for path in EntariConfig.instance.plugin_extra_files]
async for event in awatch(file.absolute(), *(dir_.absolute() for dir_ in extra), recursive=False):
for change in event:
if change[0].name != "modified":
continue
if Path(change[1]).resolve().name != file.name:
continue
if not self.is_watch_config:
continue
logger("INFO", f"Detected change in {str(file)!r}, reloading config...")
if not (
(change[0].name == "modified" and Path(change[1]).resolve() == file)
or Path(change[1]).resolve() in extra
or Path(change[1]).resolve().parent in extra
):
print(change)
continue
logger("INFO", f"Detected change in {change[1]!r}, reloading config...")

old_basic = EntariConfig.instance.basic.copy()
old_plugin = EntariConfig.instance.plugin.copy()
Expand Down Expand Up @@ -150,7 +154,7 @@ async def watch_config(self):
if res and res.value:
logger("DEBUG", f"Plugin <y>{pid!r}</y> config change handled by itself.")
continue
logger("INFO", f"Detected <blue>{pid!r}</blue>'s config change, reloading...")
logger("INFO", f"Detected config of <blue>{pid!r}</blue> changed, reloading...")
plugin_file = str(plugin.module.__file__)
unload_plugin(plugin_name)
if plugin := load_plugin(plugin_name, new_conf):
Expand Down
5 changes: 3 additions & 2 deletions arclet/entari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EntariConfig:
basic: BasicConfig = field(default_factory=dict, init=False) # type: ignore
plugin: dict[str, dict] = field(default_factory=dict, init=False)
prelude_plugin: list[str] = field(default_factory=list, init=False)
plugin_extra_files: list[str] = field(default_factory=list, init=False)
updater: Callable[[EntariConfig], None]

instance: ClassVar[EntariConfig]
Expand All @@ -46,8 +47,8 @@ def _load_plugin(path: Path):

def reload(self):
self.updater(self)
plugin_files: list[str] = self.plugin.pop("$files", []) # type: ignore
for file in plugin_files:
self.plugin_extra_files: list[str] = self.plugin.pop("$files", []) # type: ignore
for file in self.plugin_extra_files:
path = Path(file)
if not path.exists():
raise FileNotFoundError(file)
Expand Down
7 changes: 7 additions & 0 deletions example_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ async def show(session: Session):
print([*Plugin.current()._scope.subscribers])
print(Plugin.current().subplugins)
print(local_data.get_temp_dir())
print(plug.config)


@plug.use("::before_send")
async def send_hook(message: MessageChain):
return message + "喵"


@plug.use("::config_reload")
async def config_reload():
print("Config Reloaded")
return True

# @scheduler.cron("* * * * *")
# async def broadcast(app: Entari):
# for account in app.accounts.values():
Expand Down

0 comments on commit 8ef06cd

Please sign in to comment.