Skip to content

Commit 568deab

Browse files
authored
Merge pull request CloudBotIRC#131 from linuxdaemon/gonzobot+on_stop_hooks
Finally added on_stop hooks
2 parents 5d9634c + a8b000d commit 568deab

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

cloudbot/hook.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ def _periodic_hook(func):
316316
return lambda func: _periodic_hook(func)
317317

318318

319-
320319
def on_start(param=None, **kwargs):
321320
"""External on_start decorator. Can be used directly as a decorator, or with args to return a decorator
322321
:type param: function | None
@@ -339,3 +338,22 @@ def _on_start_hook(func):
339338

340339
# this is temporary, to ease transition
341340
onload = on_start
341+
342+
343+
def on_stop(param=None, **kwargs):
344+
"""External on_stop decorator. Can be used directly as a decorator, or with args to return a decorator
345+
:type param: function | None
346+
"""
347+
def _on_stop_hook(func):
348+
hook = _get_hook(func, "on_stop")
349+
if hook is None:
350+
hook = _Hook(func, "on_stop")
351+
_add_hook(func, hook)
352+
hook._add_hook(kwargs)
353+
return func
354+
if callable(param):
355+
return _on_stop_hook(param)
356+
else:
357+
return lambda func: _on_stop_hook(func)
358+
359+
on_unload = on_stop

cloudbot/plugin.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def find_hooks(parent, module):
1818
"""
1919
:type parent: Plugin
2020
:type module: object
21-
:rtype: (list[CommandHook], list[RegexHook], list[RawHook], list[SieveHook], List[EventHook], list[OnStartHook])
21+
:rtype: (list[CommandHook], list[RegexHook], list[RawHook], list[SieveHook], List[EventHook], List[PeriodicHook], list[OnStartHook], List[OnStopHook])
2222
"""
2323
# set the loaded flag
2424
module._cloudbot_loaded = True
@@ -29,8 +29,9 @@ def find_hooks(parent, module):
2929
event = []
3030
periodic = []
3131
on_start = []
32+
on_stop = []
3233
type_lists = {"command": command, "regex": regex, "irc_raw": raw, "sieve": sieve, "event": event,
33-
"periodic": periodic, "on_start": on_start}
34+
"periodic": periodic, "on_start": on_start, "on_stop": on_stop}
3435
for name, func in module.__dict__.items():
3536
if hasattr(func, "_cloudbot_hook"):
3637
# if it has cloudbot hook
@@ -42,7 +43,7 @@ def find_hooks(parent, module):
4243
# delete the hook to free memory
4344
del func._cloudbot_hook
4445

45-
return command, regex, raw, sieve, event, periodic, on_start
46+
return command, regex, raw, sieve, event, periodic, on_start, on_stop
4647

4748

4849
def find_tables(code):
@@ -293,6 +294,11 @@ def unload_plugin(self, path):
293294
for sieve_hook in plugin.sieves:
294295
self.sieves.remove(sieve_hook)
295296

297+
# Run on_stop hooks
298+
for on_stop_hook in plugin.run_on_stop:
299+
event = Event(bot=self.bot, hook=on_stop_hook)
300+
yield from self.launch(on_stop_hook, event)
301+
296302
# unregister databases
297303
plugin.unregister_tables(self.bot)
298304

@@ -439,7 +445,7 @@ def launch(self, hook, event):
439445
:rtype: bool
440446
"""
441447

442-
if hook.type not in ("on_start", "periodic"): # we don't need sieves on on_start hooks.
448+
if hook.type not in ("on_start", "on_stop", "periodic"): # we don't need sieves on on_start hooks.
443449
for sieve in self.bot.plugin_manager.sieves:
444450
event = yield from self._sieve(sieve, event, hook)
445451
if event is None:
@@ -515,7 +521,7 @@ def __init__(self, filepath, filename, title, code):
515521
self.file_path = filepath
516522
self.file_name = filename
517523
self.title = title
518-
self.commands, self.regexes, self.raw_hooks, self.sieves, self.events, self.periodic, self.run_on_start = find_hooks(self, code)
524+
self.commands, self.regexes, self.raw_hooks, self.sieves, self.events, self.periodic, self.run_on_start, self.run_on_stop = find_hooks(self, code)
519525
# we need to find tables for each plugin so that they can be unloaded from the global metadata when the
520526
# plugin is reloaded
521527
self.tables = find_tables(code)
@@ -759,12 +765,24 @@ def __str__(self):
759765
return "on_start {} from {}".format(self.function_name, self.plugin.file_name)
760766

761767

768+
class OnStopHook(Hook):
769+
def __init__(self, plugin, on_stop_hook):
770+
super().__init__("on_stop", plugin, on_stop_hook)
771+
772+
def __repr__(self):
773+
return "On_stop[{}]".format(Hook.__repr__(self))
774+
775+
def __str__(self):
776+
return "on_stop {} from {}".format(self.function_name, self.plugin.file_name)
777+
778+
762779
_hook_name_to_plugin = {
763780
"command": CommandHook,
764781
"regex": RegexHook,
765782
"irc_raw": RawHook,
766783
"sieve": SieveHook,
767784
"event": EventHook,
768785
"periodic": PeriodicHook,
769-
"on_start": OnStartHook
786+
"on_start": OnStartHook,
787+
"on_stop": OnStopHook,
770788
}

plugins/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def console_log(bot, event):
246246
bot.logger.info(text)
247247

248248

249-
# TODO: @hook.onstop() for when unloaded
249+
@hook.on_stop
250250
@hook.command("flushlog", permissions=["botcontrol"])
251251
def flush_log():
252252
for name, stream in stream_cache.values():

0 commit comments

Comments
 (0)