diff --git a/modules/bot_channel.py b/modules/bot_channel.py index 2e2ff6fb..e0731d1c 100644 --- a/modules/bot_channel.py +++ b/modules/bot_channel.py @@ -1,10 +1,8 @@ -from src import Utils - -class Module(object): - def __init__(self, bot, events, exports): - exports.add("serverset", {"setting": "bot-channel", - "help": "Set main channel"}) +from src import ModuleManager, Utils +@Utils.export("serverset", {"setting": + "bot-channel", "help": "Set main channel"}) +class Module(ModuleManager.BaseModule): @Utils.hook("received.numeric.001") def do_join(self, event): event["server"].send_join(event["server"].get_setting("bot-channel", diff --git a/src/ModuleManager.py b/src/ModuleManager.py index 6f178a16..dfcfc409 100644 --- a/src/ModuleManager.py +++ b/src/ModuleManager.py @@ -1,6 +1,7 @@ import glob, imp, inspect, os, sys, uuid BITBOT_HOOKS_MAGIC = "__bitbot_hooks" +BITBOT_EXPORTS_MAGIC = "__bitbot_exports" class ModuleException(Exception): pass @@ -41,6 +42,9 @@ class ModuleManager(object): def _import_name(self, name): return "bitbot_%s" % name + def _get_magic(self, obj, magic, default): + return getattr(obj, magic) if hasattr(obj, magic) else default + def _load_module(self, name): path = self._module_path(name) @@ -89,12 +93,11 @@ class ModuleManager(object): 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, - docstring=attribute.__doc__, **hook["kwargs"]) + for hook in self._get_magic(attribute, BITBOT_HOOKS_MAGIC, []): + context_events.on(hook["event"]).hook(attribute, + docstring=attribute.__doc__, **hook["kwargs"]) + for export in self._get_magic(module_object, BITBOT_EXPORTS_MAGIC, []): + context_exports.add(export["setting"], export["value"]) module_object._context = context module_object._import_name = name diff --git a/src/Utils.py b/src/Utils.py index e78c08fc..d5ab69d2 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -277,14 +277,22 @@ def get_closest_setting(event, setting, default=None): def prevent_highlight(nickname): return nickname[0]+"\u200d"+nickname[1:] +def _set_get_append(obj, setting, item): + if not hasattr(obj, setting): + setattr(obj, setting, []) + getattr(obj, setting).append(item) def hook(event, **kwargs): def _hook_func(func): - if not hasattr(func, ModuleManager.BITBOT_HOOKS_MAGIC): - setattr(func, ModuleManager.BITBOT_HOOKS_MAGIC, []) - getattr(func, ModuleManager.BITBOT_HOOKS_MAGIC).append( + _set_get_append(func, ModuleManager.BITBOT_HOOKS_MAGIC, {"event": event, "kwargs": kwargs}) return func return _hook_func +def export(setting, value): + def _export_func(module): + _set_get_append(module, ModuleManager.BITBOT_EXPORTS_MAGIC, + {"setting": setting, "value": value}) + return module + return _export_func def strip_html(s): return bs4.BeautifulSoup(s, "lxml").get_text()