aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ModuleManager.py22
-rw-r--r--src/utils/__init__.py43
-rw-r--r--src/utils/consts.py4
3 files changed, 43 insertions, 26 deletions
diff --git a/src/ModuleManager.py b/src/ModuleManager.py
index 45866d23..2b1de97d 100644
--- a/src/ModuleManager.py
+++ b/src/ModuleManager.py
@@ -209,23 +209,17 @@ class ModuleManager(object):
# @utils.hook() magic
for attribute_name in dir(module_object):
attribute = getattr(module_object, attribute_name)
- if inspect.ismethod(attribute):
- kwargs = self._get_magic(attribute,
- utils.consts.BITBOT_KWARG_MAGIC, [])
- kwargs = dict(list(d.items())[0] for d in kwargs)
+ if inspect.ismethod(attribute) and utils.has_magic(attribute):
+ magic = utils.get_magic(attribute)
- for hook in self._get_magic(attribute,
- utils.consts.BITBOT_HOOKS_MAGIC, []):
- new_kwargs = kwargs.copy()
- new_kwargs.update(hook["kwargs"])
-
- context_events.on(hook["event"]).hook(attribute,
- **new_kwargs)
+ for hook, kwargs in magic.get_hooks():
+ context_events.on(hook).hook(attribute, **dict(kwargs))
# @utils.export() magic
- for export in self._get_magic(module_object,
- utils.consts.BITBOT_EXPORTS_MAGIC, []):
- context_exports.add(export["setting"], export["value"])
+ if utils.has_magic(module_object):
+ magic = utils.get_magic(module_object)
+ for key, value in magic.get_exports():
+ context_exports.add(key, value)
if definition.name in self.modules:
raise ModuleNameCollisionException("Module name '%s' "
diff --git a/src/utils/__init__.py b/src/utils/__init__.py
index 027a72af..eee3fc50 100644
--- a/src/utils/__init__.py
+++ b/src/utils/__init__.py
@@ -153,25 +153,50 @@ class EventsUsageError(EventError):
def __init__(self, usage):
EventError.__init__(self, "Not enough arguments, usage: %s" % usage)
-def _set_get_append(obj: typing.Any, setting: str, item: typing.Any):
- if not hasattr(obj, setting):
- setattr(obj, setting, [])
- getattr(obj, setting).append(item)
+class BitBotMagic(object):
+ def __init__(self):
+ self._hooks: typing.List[typing.Tuple[str, dict]] = []
+ self._kwargs: typing.List[typing.Tuple[str, typing.Any]] = []
+ self._exports: typing.List[typing.Tuple[str, typing.Any]] = []
+ def add_hook(self, hook: str, kwargs: dict):
+ self._hooks.append((hook, kwargs))
+ def add_kwarg(self, key: str, value: typing.Any):
+ self._kwargs.append((key, value))
+
+ def get_hooks(self):
+ hooks: typing.List[typing.Tuple[str, typing.List[str, typing.Any]]] = []
+ for hook, kwargs in self._hooks:
+ hooks.append((hook, self._kwargs.copy()+list(kwargs.items())))
+ return hooks
+
+ def add_export(self, key: str, value: typing.Any):
+ self._exports.append((key, value))
+ def get_exports(self):
+ return self._exports.copy()
+
+def get_magic(obj: typing.Any):
+ if not has_magic(obj):
+ setattr(obj, consts.BITBOT_MAGIC, BitBotMagic())
+ return getattr(obj, consts.BITBOT_MAGIC)
+def has_magic(obj: typing.Any):
+ return hasattr(obj, consts.BITBOT_MAGIC)
+
def hook(event: str, **kwargs):
def _hook_func(func):
- _set_get_append(func, consts.BITBOT_HOOKS_MAGIC,
- {"event": event, "kwargs": kwargs})
+ magic = get_magic(func)
+ magic.add_hook(event, kwargs)
return func
return _hook_func
def export(setting: str, value: typing.Any):
def _export_func(module):
- _set_get_append(module, consts.BITBOT_EXPORTS_MAGIC,
- {"setting": setting, "value": value})
+ magic = get_magic(module)
+ magic.add_export(setting, value)
return module
return _export_func
def kwarg(key: str, value: typing.Any):
def _kwarg_func(func):
- _set_get_append(func, consts.BITBOT_KWARG_MAGIC, {key: value})
+ magic = get_magic(func)
+ magic.add_kwarg(key, value)
return func
return _kwarg_func
diff --git a/src/utils/consts.py b/src/utils/consts.py
index 0c57b4b0..9a6e973b 100644
--- a/src/utils/consts.py
+++ b/src/utils/consts.py
@@ -1,9 +1,7 @@
import typing
from . import _consts_256_color
-BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
-BITBOT_KWARG_MAGIC = "__bitbot_kwarg"
-BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
+BITBOT_MAGIC = "__bitbot"
class IRCColor(object):
def __init__(self, irc: int, ansi: int, is_256):