Implement @Utils.export, to denote an export on a module

This commit is contained in:
jesopo 2018-09-27 11:45:23 +01:00
parent af2b34b92b
commit f3d98d0e95
3 changed files with 24 additions and 15 deletions

View file

@ -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",

View file

@ -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

View file

@ -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()