From b8aca728611d0422683347fdf3fc97b4f8651499 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 19 Sep 2018 12:35:34 +0100 Subject: Support hooking functions in modules with @Utils.hook --- ModuleManager.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'ModuleManager.py') 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 -- cgit v1.3.1-10-gc9f91