Skip to content

Commit 8ef06cd

Browse files
committed
✨ auto_reload support plugins.$files
1 parent 8948a6d commit 8ef06cd

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

arclet/entari/builtins/auto_reload.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,20 @@ async def watch(self):
9595
logger("ERROR", f"Failed to reload <blue>{self.fail[change[1]]!r}</blue>")
9696

9797
async def watch_config(self):
98-
file = EntariConfig.instance.path
99-
async for event in awatch(file.resolve().absolute().parent, recursive=False):
98+
file = EntariConfig.instance.path.resolve()
99+
extra = [file.parent.joinpath(path) for path in EntariConfig.instance.plugin_extra_files]
100+
async for event in awatch(file.absolute(), *(dir_.absolute() for dir_ in extra), recursive=False):
100101
for change in event:
101-
if change[0].name != "modified":
102-
continue
103-
if Path(change[1]).resolve().name != file.name:
104-
continue
105102
if not self.is_watch_config:
106103
continue
107-
logger("INFO", f"Detected change in {str(file)!r}, reloading config...")
104+
if not (
105+
(change[0].name == "modified" and Path(change[1]).resolve() == file)
106+
or Path(change[1]).resolve() in extra
107+
or Path(change[1]).resolve().parent in extra
108+
):
109+
print(change)
110+
continue
111+
logger("INFO", f"Detected change in {change[1]!r}, reloading config...")
108112

109113
old_basic = EntariConfig.instance.basic.copy()
110114
old_plugin = EntariConfig.instance.plugin.copy()
@@ -150,7 +154,7 @@ async def watch_config(self):
150154
if res and res.value:
151155
logger("DEBUG", f"Plugin <y>{pid!r}</y> config change handled by itself.")
152156
continue
153-
logger("INFO", f"Detected <blue>{pid!r}</blue>'s config change, reloading...")
157+
logger("INFO", f"Detected config of <blue>{pid!r}</blue> changed, reloading...")
154158
plugin_file = str(plugin.module.__file__)
155159
unload_plugin(plugin_name)
156160
if plugin := load_plugin(plugin_name, new_conf):

arclet/entari/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class EntariConfig:
2121
basic: BasicConfig = field(default_factory=dict, init=False) # type: ignore
2222
plugin: dict[str, dict] = field(default_factory=dict, init=False)
2323
prelude_plugin: list[str] = field(default_factory=list, init=False)
24+
plugin_extra_files: list[str] = field(default_factory=list, init=False)
2425
updater: Callable[[EntariConfig], None]
2526

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

4748
def reload(self):
4849
self.updater(self)
49-
plugin_files: list[str] = self.plugin.pop("$files", []) # type: ignore
50-
for file in plugin_files:
50+
self.plugin_extra_files: list[str] = self.plugin.pop("$files", []) # type: ignore
51+
for file in self.plugin_extra_files:
5152
path = Path(file)
5253
if not path.exists():
5354
raise FileNotFoundError(file)

example_plugin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,19 @@ async def show(session: Session):
7676
print([*Plugin.current()._scope.subscribers])
7777
print(Plugin.current().subplugins)
7878
print(local_data.get_temp_dir())
79+
print(plug.config)
7980

8081

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

86+
87+
@plug.use("::config_reload")
88+
async def config_reload():
89+
print("Config Reloaded")
90+
return True
91+
8592
# @scheduler.cron("* * * * *")
8693
# async def broadcast(app: Entari):
8794
# for account in app.accounts.values():

0 commit comments

Comments
 (0)