diff options
| author | 2018-08-31 12:55:52 +0100 | |
|---|---|---|
| committer | 2018-08-31 12:55:52 +0100 | |
| commit | 9874f79b498e1f8ae5ebec9a240963e908b645b0 (patch) | |
| tree | d5d887ac82f3e50f0ea295953981363c1c5e7c5a /ModuleManager.py | |
| parent | Merge pull request #7 from dngfx/master (diff) | |
| signature | ||
Give modules event objects with "context"s, to facilitate purging all the event
hooks for a module
Diffstat (limited to 'ModuleManager.py')
| -rw-r--r-- | ModuleManager.py | 40 |
1 files changed, 13 insertions, 27 deletions
diff --git a/ModuleManager.py b/ModuleManager.py index e601ce9e..812edb71 100644 --- a/ModuleManager.py +++ b/ModuleManager.py @@ -1,8 +1,9 @@ -import gc, glob, imp, inspect, os, sys +import gc, glob, imp, inspect, os, sys, uuid class ModuleManager(object): - def __init__(self, bot, directory="modules"): + def __init__(self, bot, events, directory="modules"): self.bot = bot + self.events = events self.directory = directory self.modules = {} self.waiting_requirement = {} @@ -39,17 +40,22 @@ class ModuleManager(object): return None else: break - import_name = "bitbot_%s" % name - module = imp.load_source(import_name, filename) + module = imp.load_source(name, filename) + if not hasattr(module, "Module"): raise ImportError("module '%s' doesn't have a Module class.") if not inspect.isclass(module.Module): raise ImportError("module '%s' has a Module attribute but it is not a class.") - module_object = module.Module(self.bot) + + event_context = uuid.uuid4() + module_object = module.Module(self.bot, self.events.new_context( + event_context)) if not hasattr(module_object, "_name"): module_object._name = name.title() + module_object._event_context = event_context module_object._is_unloaded = False - module_object._import_name = import_name + module_object._import_name = name + assert not module_object._name in self.modules, ( "module name '%s' attempted to be used twice.") return module_object @@ -62,7 +68,7 @@ class ModuleManager(object): sys.stderr.write("module '%s' not loaded: Could not resolve import.\n" % filename) return if module: - self.modules[module._name] = module + self.modules[module._import_name] = module if name in self.waiting_requirement: for filename in self.waiting_requirement: self.load_module(filename) @@ -74,23 +80,3 @@ class ModuleManager(object): for filename in self.list_modules(): if whitelist == None or filename in whitelist: self.load_module(filename) - - def unload_module(self, module): - # this is such a bad idea - module._is_unloaded = True - self.unhook_check(self.bot.events) - if hasattr(module, "_cleanup"): - module._cleanup() - del sys.modules[module._import_name] - del self.modules[module._name] - del module - gc.collect() - - def unhook_check(self, event): - for hook in event.get_hooks(): - if hasattr(hook.function, "__self__") and hasattr( - hook.function.__self__, "_is_unloaded" - ) and hook.function.__self__._is_unloaded: - event._unhook(hook) - for child in event.get_children(): - self.unhook_check(event.get_child(child)) |
