diff options
| author | 2019-11-15 13:39:24 +0000 | |
|---|---|---|
| committer | 2019-11-15 13:39:24 +0000 | |
| commit | bfcf40edd71c5bdd436ab8b535c880fad8781f16 (patch) | |
| tree | f26b390a444203f03a03be84b475cb83bf907bb0 /src/utils/decorators.py | |
| parent | only try to shlex when we know we've found a command hook (diff) | |
| signature | ||
split some stuff out of utils/__init__.py
Diffstat (limited to 'src/utils/decorators.py')
| -rw-r--r-- | src/utils/decorators.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/utils/decorators.py b/src/utils/decorators.py new file mode 100644 index 00000000..f6897e17 --- /dev/null +++ b/src/utils/decorators.py @@ -0,0 +1,50 @@ +import typing +from . import consts + +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[Tuple[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): + 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): + 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): + magic = get_magic(func) + magic.add_kwarg(key, value) + return func + return _kwarg_func + |
