support @utils.export on functions, to export those functions

This commit is contained in:
jesopo 2020-02-19 17:22:37 +00:00
parent b277463fee
commit 70db97f64e
3 changed files with 17 additions and 17 deletions

View file

@ -39,10 +39,6 @@ class CoinParseException(Exception):
pass pass
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self):
self.exports.add("command-spec.coins", self._coins_spec)
self.exports.add("command-spec.coinsmany", self._coins_many_spec)
def _coin_spec_parse(self, word): def _coin_spec_parse(self, word):
try: try:
out = decimal.Decimal(word) out = decimal.Decimal(word)
@ -54,11 +50,14 @@ class Module(ModuleManager.BaseModule):
else: else:
raise utils.parse.SpecTypeError( raise utils.parse.SpecTypeError(
"Please provide a positive coin amount") "Please provide a positive coin amount")
@utils.export("command-spec.coins")
def _coins_spec(self, server, channel, user, args): def _coins_spec(self, server, channel, user, args):
if args: if args:
return self._coin_spec_parse(args[0]), 1 return self._coin_spec_parse(args[0]), 1
else: else:
raise utils.parse.SpecTypeError("No coin amount provided") raise utils.parse.SpecTypeError("No coin amount provided")
@utils.export("command-spec.coinsmany")
def _coins_many_spec(self, server, channel, user, args): def _coins_many_spec(self, server, channel, user, args):
out = [] out = []
for arg in args: for arg in args:

View file

@ -256,7 +256,13 @@ class ModuleManager(object):
module_title = (getattr(module_object, "_name", None) or module_title = (getattr(module_object, "_name", None) or
definition.name.title()) definition.name.title())
# @utils.hook() magic # per-module @export magic
if utils.decorators.has_magic(module_object):
magic = utils.decorators.get_magic(module_object)
for key, value in magic.get_exports():
context_exports.add(key, value)
# per-function @hook/@export 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) and if (inspect.ismethod(attribute) and
@ -265,12 +271,8 @@ class ModuleManager(object):
for hook, kwargs in magic.get_hooks(): for hook, kwargs in magic.get_hooks():
context_events.on(hook)._hook(attribute, kwargs=kwargs) context_events.on(hook)._hook(attribute, kwargs=kwargs)
# @utils.export() magic
if utils.decorators.has_magic(module_object):
magic = utils.decorators.get_magic(module_object)
for key, value in magic.get_exports(): for key, value in magic.get_exports():
context_exports.add(key, value) context_exports.add(key, attribute)
branch, commit = utils.git_commit(bot.directory) branch, commit = utils.git_commit(bot.directory)

View file

@ -37,12 +37,11 @@ def hook(event: str, **kwargs):
magic.add_hook(event, 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=None):
def _export_func(module): def _export(obj: typing.Any):
magic = get_magic(module) get_magic(obj).add_export(setting, value)
magic.add_export(setting, value) return obj
return module return _export
return _export_func
def _kwarg(key: str, value: typing.Any, func: typing.Any): def _kwarg(key: str, value: typing.Any, func: typing.Any):
magic = get_magic(func) magic = get_magic(func)