aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorGravatar jesopo2019-07-26 11:58:06 +0100
committerGravatar jesopo2019-07-26 11:58:06 +0100
commit77dfc765910ccdebb2816cb65705e67016f80a1f (patch)
treeab94d5cbdc3cd2552a5825cf752d6e3a2b656e59 /src/utils
parent8ball.py -> eightball.py (diff)
signature
switch to function/module magic being a single object
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/__init__.py43
-rw-r--r--src/utils/consts.py4
2 files changed, 35 insertions, 12 deletions
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):