small but significant unload/reload changes.

IrcAdmin:
* have sub_unload_module() call the module's shutdown() before deleting stuff
* sub_unload_module() needs to delete the sys.modules entry too
* slightly more verbose output in sub_unload_module()
* sub_reload_module() now does a self.sub_unload_module() + self.sub_load_module()

Module:
* remove reload() --- modules must now safely handle shutdown
This commit is contained in:
Brian S. Stephan 2011-01-06 19:54:16 -06:00
parent 99b474c85b
commit ea0fd9d509
2 changed files with 25 additions and 34 deletions

View File

@ -205,23 +205,6 @@ class Module(object):
except Exception as e:
print('EXCEPTION: ' + str(e))
def reload(self):
"""Reload this module's code and then create a new object of it, removing the old."""
# re-read and re-compile module from source on disk
reload(sys.modules[self.__module__])
# find the class declaration for the current module
for name, obj in inspect.getmembers(sys.modules[self.__module__]):
if inspect.isclass(obj) and str(obj).find(self.__module__) > 0:
# remove existing object from in-memory stuff
self.modlist.remove(self)
self.unregister_handlers()
# create new object, like how we did initially
obj(self.config, self.server, self.modlist)
self.register_handlers(self.server)
def reply(self, connection, replypath, replystr):
"""
Reply over IRC to replypath or return a string with the reply.

View File

@ -188,39 +188,47 @@ class IrcAdmin(Module):
except ImportError:
return self.reply(connection, replypath, 'Module ' + modname + ' not found.')
def sub_reload_module(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""
Attempt to reload a module, by seeing if it's loaded and, if so, calling its reload()
"""
whats = what.split(' ')
modname = whats[1]
for module in self.modlist:
if modname == module.__class__.__name__:
module.reload()
return self.reply(connection, replypath, modname + ' reloaded.')
def sub_unload_module(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""
Attempt to unload and del a module if it's loaded.
"""
whats = what.split(' ')
modname = whats[1]
modstr = 'modules.'+modname
for module in self.modlist:
if modname == module.__class__.__name__:
# do anything the module needs to do to clean up
module.shutdown()
# remove module references
self.modlist.remove(module)
module.unregister_handlers()
# del it
del module
return self.reply(connection, replypath, modname + ' unloaded.')
del(sys.modules[modstr])
del(module)
return self.reply(connection, replypath, 'Module ' + modname + ' unloaded.')
# guess it was never loaded
return self.reply(connection, replypath, modname + ' is not loaded.')
return self.reply(connection, replypath, 'Module ' + modname + ' is not loaded.')
def sub_reload_module(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""
Attempt to reload a module, by removing it from memory and then re-initializing it.
"""
whats = what.split(' ')
modname = whats[1]
ret = self.sub_unload_module(connection, event, nick, userhost, None, what, admin_unlocked)
if ret == 'Module ' + modname + ' unloaded.':
ret = self.sub_load_module(connection, event, nick, userhost, None, what, admin_unlocked)
if ret == 'Module ' + modname + ' loaded.':
return self.reply(connection, replypath, 'Module ' + modname + ' reloaded.')
return self.reply(connection, replypath, 'Module ' + modname + ' reload failed. Check the console.')
# Save the config file.
def save_config(self):