From 842e185c613f3ea11132ff4e9fe024f0101e4813 Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 9 Aug 2012 00:49:00 -0600 Subject: reworked how the modules work a bit. warmachine works, but loading,unloading and reloading modules while it's running is probably broken. --- wmd/actions/modules.py | 6 ++--- wmd/irc.py | 59 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/wmd/actions/modules.py b/wmd/actions/modules.py index 22df2f6..3431ab6 100644 --- a/wmd/actions/modules.py +++ b/wmd/actions/modules.py @@ -9,9 +9,9 @@ class ReloadModule(Action): if username in settings.ADMINS: args = obj_data.params.split(" ") if "PRIVMSG" in obj_data.command and "RELOADMODULE" in args[1].upper(): - irc.privmsg(username, "This feature hasn't been implemented yet") - return - module = args[2] + + class_name = args[2] + if not module in irc.actions: irc.privmsg(username, "Invalid Module: %s") return diff --git a/wmd/irc.py b/wmd/irc.py index b06cfec..68a5384 100644 --- a/wmd/irc.py +++ b/wmd/irc.py @@ -14,6 +14,11 @@ class IRC(object): self.nick = nick self.name = name self.port = port + + # the structure + # TODO: Refactor + # self.actions["module_name"][0]: the actual module. saved to reload if needed + # self.actions["module_name"][1]: dictionary. key = class name, value = class object self.actions = dict() self.load_actions() @@ -64,10 +69,22 @@ class IRC(object): Loads the provided action """ module_name, class_name = path.rsplit('.', 1) - try: - module = __import__(module_name, globals(), locals(), [class_name], -1) - except ImportError: - self.log("Error loading module: %s" %(path,)) + + if self.actions.has_key(module_name): + module = self.actions[module_name][0] + else: + try: + module = __import__(module_name, globals(), locals(), [class_name], -1) + except ImportError: + self.log("Error loading module: %s" %(path,)) + return + + if not self.actions.has_key(module_name): + self.actions[module_name] = [] + self.actions[module_name].insert(0, module) # Save the module so it can be reloaded later + self.actions[module_name].insert(1, dict()) + elif class_name in self.actions[module_name][1]: + self.log("Class already loaded") return try: @@ -76,7 +93,7 @@ class IRC(object): self.log("Class does not exist in module") return - self.actions[class_name] = classz() + self.actions[module_name][1][class_name] = classz() def __call__(self, *args, **kwargs): """ @@ -104,18 +121,20 @@ class IRC(object): modules_to_load = [] modules_to_unload = [] - for plugin_name in self.actions: - retval = self.actions[plugin_name].recv_msg(self, obj_data) - if type(retval) == type(dict()): - if retval.has_key('load'): - modules_to_load.append(retval['load']) - if retval.has_key('unload'): - modules_to_unload.append(retval['unload']) - elif type(retval) == type('str is str'): - self.rawsend(retval) - - for module in modules_to_load: - self.load_action(module) - for module in modules_to_unload: - if self.actions.has_key(module): - del(self.actions[module]) + for module_name in self.actions: + for class_name in self.actions[module_name][1]: + retval = self.actions[module_name][1][class_name].recv_msg(self, obj_data) + + if type(retval) == type(dict()): + if retval.has_key('load'): + modules_to_load.append(retval['load']) + if retval.has_key('unload'): + modules_to_unload.append(retval['unload']) + elif type(retval) == type('str is str'): + self.rawsend(retval) + + for module in modules_to_load: + self.load_action(module) + #for module in modules_to_unload: + # if self.actions.has_key(module): + # del(self.actions[module]) -- cgit v1.2.1