From 22615d7b9ac11e311c5be35f82867d608166902b Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Wed, 15 Dec 2010 20:43:14 -0600 Subject: [PATCH] rewrite reload support, making it an IrcAdmin command only lightly tested for the moment --- Module.py | 28 +++++++++++----------------- modules/IrcAdmin.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Module.py b/Module.py index 262219b..9a05376 100644 --- a/Module.py +++ b/Module.py @@ -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): """ diff --git a/modules/IrcAdmin.py b/modules/IrcAdmin.py index 8502e4c..667d33b 100644 --- a/modules/IrcAdmin.py +++ b/modules/IrcAdmin.py @@ -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: