diff options
| author | 2018-09-19 12:35:34 +0100 | |
|---|---|---|
| committer | 2018-09-19 12:37:41 +0100 | |
| commit | b8aca728611d0422683347fdf3fc97b4f8651499 (patch) | |
| tree | 377d61f17b3a35849753314ec60a0f012956fb0c /ModuleManager.py | |
| parent | Send a FONT_RESET (\x0F) after stderr module names because a bug in weechat (diff) | |
| signature | ||
Support hooking functions in modules with @Utils.hook
Diffstat (limited to 'ModuleManager.py')
| -rw-r--r-- | ModuleManager.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/ModuleManager.py b/ModuleManager.py index a67cacad..29248a36 100644 --- a/ModuleManager.py +++ b/ModuleManager.py @@ -1,5 +1,7 @@ import glob, imp, inspect, os, sys, uuid +BITBOT_HOOKS_MAGIC = "__bitbot_hooks" + class ModuleManager(object): def __init__(self, bot, events, exports, directory="modules"): self.bot = bot @@ -51,10 +53,22 @@ class ModuleManager(object): raise ImportError("module '%s' has a Module attribute but it is not a class.") context = str(uuid.uuid4()) - module_object = module.Module(self.bot, self.events.new_context( - context), self.exports.new_context(context)) + context_events = self.events.new_context(context) + context_exports = self.exports.new_context(context) + module_object = module.Module(self.bot, context_events, + context_exports) + if not hasattr(module_object, "_name"): module_object._name = name.title() + for attribute_name in dir(module_object): + attribute = getattr(module_object, attribute_name) + if inspect.ismethod(attribute) and hasattr(attribute, + BITBOT_HOOKS_MAGIC): + hooks = getattr(attribute, BITBOT_HOOKS_MAGIC) + for hook in hooks: + context_events.on(hook["event"]).hook(attribute, + **hook["kwargs"]) + module_object._context = context module_object._import_name = name |
