Implement @Utils.export, to denote an export on a module
This commit is contained in:
parent
af2b34b92b
commit
f3d98d0e95
3 changed files with 24 additions and 15 deletions
|
@ -1,10 +1,8 @@
|
||||||
from src import Utils
|
from src import ModuleManager, Utils
|
||||||
|
|
||||||
class Module(object):
|
|
||||||
def __init__(self, bot, events, exports):
|
|
||||||
exports.add("serverset", {"setting": "bot-channel",
|
|
||||||
"help": "Set main channel"})
|
|
||||||
|
|
||||||
|
@Utils.export("serverset", {"setting":
|
||||||
|
"bot-channel", "help": "Set main channel"})
|
||||||
|
class Module(ModuleManager.BaseModule):
|
||||||
@Utils.hook("received.numeric.001")
|
@Utils.hook("received.numeric.001")
|
||||||
def do_join(self, event):
|
def do_join(self, event):
|
||||||
event["server"].send_join(event["server"].get_setting("bot-channel",
|
event["server"].send_join(event["server"].get_setting("bot-channel",
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import glob, imp, inspect, os, sys, uuid
|
import glob, imp, inspect, os, sys, uuid
|
||||||
|
|
||||||
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
||||||
|
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
|
||||||
|
|
||||||
class ModuleException(Exception):
|
class ModuleException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -41,6 +42,9 @@ class ModuleManager(object):
|
||||||
def _import_name(self, name):
|
def _import_name(self, name):
|
||||||
return "bitbot_%s" % 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):
|
def _load_module(self, name):
|
||||||
path = self._module_path(name)
|
path = self._module_path(name)
|
||||||
|
|
||||||
|
@ -89,12 +93,11 @@ class ModuleManager(object):
|
||||||
module_object._name = name.title()
|
module_object._name = name.title()
|
||||||
for attribute_name in dir(module_object):
|
for attribute_name in dir(module_object):
|
||||||
attribute = getattr(module_object, attribute_name)
|
attribute = getattr(module_object, attribute_name)
|
||||||
if inspect.ismethod(attribute) and hasattr(attribute,
|
for hook in self._get_magic(attribute, BITBOT_HOOKS_MAGIC, []):
|
||||||
BITBOT_HOOKS_MAGIC):
|
context_events.on(hook["event"]).hook(attribute,
|
||||||
hooks = getattr(attribute, BITBOT_HOOKS_MAGIC)
|
docstring=attribute.__doc__, **hook["kwargs"])
|
||||||
for hook in hooks:
|
for export in self._get_magic(module_object, BITBOT_EXPORTS_MAGIC, []):
|
||||||
context_events.on(hook["event"]).hook(attribute,
|
context_exports.add(export["setting"], export["value"])
|
||||||
docstring=attribute.__doc__, **hook["kwargs"])
|
|
||||||
|
|
||||||
module_object._context = context
|
module_object._context = context
|
||||||
module_object._import_name = name
|
module_object._import_name = name
|
||||||
|
|
14
src/Utils.py
14
src/Utils.py
|
@ -277,14 +277,22 @@ def get_closest_setting(event, setting, default=None):
|
||||||
def prevent_highlight(nickname):
|
def prevent_highlight(nickname):
|
||||||
return nickname[0]+"\u200d"+nickname[1:]
|
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(event, **kwargs):
|
||||||
def _hook_func(func):
|
def _hook_func(func):
|
||||||
if not hasattr(func, ModuleManager.BITBOT_HOOKS_MAGIC):
|
_set_get_append(func, ModuleManager.BITBOT_HOOKS_MAGIC,
|
||||||
setattr(func, ModuleManager.BITBOT_HOOKS_MAGIC, [])
|
|
||||||
getattr(func, ModuleManager.BITBOT_HOOKS_MAGIC).append(
|
|
||||||
{"event": event, "kwargs": kwargs})
|
{"event": event, "kwargs": kwargs})
|
||||||
return func
|
return func
|
||||||
return _hook_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):
|
def strip_html(s):
|
||||||
return bs4.BeautifulSoup(s, "lxml").get_text()
|
return bs4.BeautifulSoup(s, "lxml").get_text()
|
||||||
|
|
Loading…
Reference in a new issue