switch to function/module magic being a single object
This commit is contained in:
parent
71152475b2
commit
77dfc76591
3 changed files with 43 additions and 26 deletions
|
@ -209,23 +209,17 @@ class ModuleManager(object):
|
||||||
# @utils.hook() magic
|
# @utils.hook() magic
|
||||||
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):
|
if inspect.ismethod(attribute) and utils.has_magic(attribute):
|
||||||
kwargs = self._get_magic(attribute,
|
magic = utils.get_magic(attribute)
|
||||||
utils.consts.BITBOT_KWARG_MAGIC, [])
|
|
||||||
kwargs = dict(list(d.items())[0] for d in kwargs)
|
|
||||||
|
|
||||||
for hook in self._get_magic(attribute,
|
for hook, kwargs in magic.get_hooks():
|
||||||
utils.consts.BITBOT_HOOKS_MAGIC, []):
|
context_events.on(hook).hook(attribute, **dict(kwargs))
|
||||||
new_kwargs = kwargs.copy()
|
|
||||||
new_kwargs.update(hook["kwargs"])
|
|
||||||
|
|
||||||
context_events.on(hook["event"]).hook(attribute,
|
|
||||||
**new_kwargs)
|
|
||||||
|
|
||||||
# @utils.export() magic
|
# @utils.export() magic
|
||||||
for export in self._get_magic(module_object,
|
if utils.has_magic(module_object):
|
||||||
utils.consts.BITBOT_EXPORTS_MAGIC, []):
|
magic = utils.get_magic(module_object)
|
||||||
context_exports.add(export["setting"], export["value"])
|
for key, value in magic.get_exports():
|
||||||
|
context_exports.add(key, value)
|
||||||
|
|
||||||
if definition.name in self.modules:
|
if definition.name in self.modules:
|
||||||
raise ModuleNameCollisionException("Module name '%s' "
|
raise ModuleNameCollisionException("Module name '%s' "
|
||||||
|
|
|
@ -153,25 +153,50 @@ class EventsUsageError(EventError):
|
||||||
def __init__(self, usage):
|
def __init__(self, usage):
|
||||||
EventError.__init__(self, "Not enough arguments, usage: %s" % usage)
|
EventError.__init__(self, "Not enough arguments, usage: %s" % usage)
|
||||||
|
|
||||||
def _set_get_append(obj: typing.Any, setting: str, item: typing.Any):
|
class BitBotMagic(object):
|
||||||
if not hasattr(obj, setting):
|
def __init__(self):
|
||||||
setattr(obj, setting, [])
|
self._hooks: typing.List[typing.Tuple[str, dict]] = []
|
||||||
getattr(obj, setting).append(item)
|
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(event: str, **kwargs):
|
||||||
def _hook_func(func):
|
def _hook_func(func):
|
||||||
_set_get_append(func, consts.BITBOT_HOOKS_MAGIC,
|
magic = get_magic(func)
|
||||||
{"event": event, "kwargs": kwargs})
|
magic.add_hook(event, kwargs)
|
||||||
return func
|
return func
|
||||||
return _hook_func
|
return _hook_func
|
||||||
def export(setting: str, value: typing.Any):
|
def export(setting: str, value: typing.Any):
|
||||||
def _export_func(module):
|
def _export_func(module):
|
||||||
_set_get_append(module, consts.BITBOT_EXPORTS_MAGIC,
|
magic = get_magic(module)
|
||||||
{"setting": setting, "value": value})
|
magic.add_export(setting, value)
|
||||||
return module
|
return module
|
||||||
return _export_func
|
return _export_func
|
||||||
def kwarg(key: str, value: typing.Any):
|
def kwarg(key: str, value: typing.Any):
|
||||||
def _kwarg_func(func):
|
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 func
|
||||||
return _kwarg_func
|
return _kwarg_func
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import typing
|
import typing
|
||||||
from . import _consts_256_color
|
from . import _consts_256_color
|
||||||
|
|
||||||
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
BITBOT_MAGIC = "__bitbot"
|
||||||
BITBOT_KWARG_MAGIC = "__bitbot_kwarg"
|
|
||||||
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
|
|
||||||
|
|
||||||
class IRCColor(object):
|
class IRCColor(object):
|
||||||
def __init__(self, irc: int, ansi: int, is_256):
|
def __init__(self, irc: int, ansi: int, is_256):
|
||||||
|
|
Loading…
Reference in a new issue