rewrite reload support, making it an IrcAdmin command

only lightly tested for the moment
This commit is contained in:
Brian S. Stephan 2010-12-15 20:43:14 -06:00
parent d92d60ec7a
commit 22615d7b9a
2 changed files with 26 additions and 17 deletions

View File

@ -155,10 +155,6 @@ class Module(object):
if replypath is not None and skip_recursion_scan is False:
what = self.try_recursion(connection, event, nick, userhost, replypath, what, admin_unlocked)
# broken because ???
## try reloading
#self.reload(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def on_privmsg(self, connection, event):
@ -187,23 +183,21 @@ class Module(object):
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def reload(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
def reload(self):
"""Reload this module's code and then create a new object of it, removing the old."""
whats = what.split(' ')
if whats[0] == 'reload' and admin_unlocked:
# re-read and re-compile module from source on disk
reload(sys.modules[self.__module__])
# 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()
# 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)
# create new object, like how we did initially
obj(self.config, self.server, self.modlist)
def reply(self, connection, replypath, replystr):
"""

View File

@ -77,6 +77,8 @@ class IrcAdmin(Module):
return self.sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
elif whats[0] == 'nick' and admin_unlocked and len(whats) >= 2:
return self.sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
elif whats[0] == 'reload' and admin_unlocked and len(whats) >= 2:
return self.sub_reload_module(connection, event, nick, userhost, replypath, what, admin_unlocked)
def sub_join_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
@ -146,6 +148,19 @@ class IrcAdmin(Module):
replystr = 'changed nickname'
return self.reply(connection, replypath, replystr)
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.')
# Save the config file.
def save_config(self):
with open('dr.botzo.cfg', 'w') as cfg: