@@ -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
4849def 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}
0 commit comments