Added .call_limited() to EventHook to avoid having a kwarg outside of **kwargs

in .call()
This commit is contained in:
jesopo 2018-08-28 16:04:35 +01:00
parent 4922ee8f4a
commit 5602b95d70

View file

@ -34,12 +34,21 @@ class MultipleEventHook(object):
self._event_hooks = set([]) self._event_hooks = set([])
def _add(self, event_hook): def _add(self, event_hook):
self._event_hooks.add(event_hook) self._event_hooks.add(event_hook)
def hook(self, function, **kwargs): def hook(self, function, **kwargs):
for event_hook in self._event_hooks: for event_hook in self._event_hooks:
event_hook.hook(function, **kwargs) event_hook.hook(function, **kwargs)
def call(self, max=None, **kwargs):
def call_limited(self, maximum, **kwargs):
returns = []
for event_hook in self._event_hooks: for event_hook in self._event_hooks:
event_hook.call(max, **kwargs) returns.append(event_hook.call_limited(maximum, **kwargs))
return returns
def call(self, **kwargs):
returns = []
for event_hook in self._event_hooks:
returns.append(event_hook.call(**kwargs))
return returns
class EventHook(object): class EventHook(object):
def __init__(self, bot, name=None, parent=None): def __init__(self, bot, name=None, parent=None):
@ -71,9 +80,9 @@ class EventHook(object):
self._hooks.append(callback) self._hooks.append(callback)
self._hooks.sort(key=lambda x: x.priority) self._hooks.sort(key=lambda x: x.priority)
if replay: if replay and not self._stored_events == None:
for kwargs in self._stored_events: for kwargs in self._stored_events:
callback.call(self._make_event(kwargs)) self._call(kwargs)
self._stored_events = None self._stored_events = None
def _unhook(self, hook): def _unhook(self, hook):
@ -102,8 +111,12 @@ class EventHook(object):
if not self._stored_events == None: if not self._stored_events == None:
self._stored_events.append(kwargs) self._stored_events.append(kwargs)
else: else:
self.call(**kwargs) self._call(kwargs)
def call(self, max=None, **kwargs): def call(self, **kwargs):
return self._call(kwargs)
def call_limited(self, maximum, **kwargs):
return self._call(kwargs, maximum=maximum)
def _call(self, kwargs, maximum=None):
self.bot.log.debug("calling event: \"%s\" (params: %s)", self.bot.log.debug("calling event: \"%s\" (params: %s)",
[self._get_path(), kwargs]) [self._get_path(), kwargs])
start = time.monotonic() start = time.monotonic()
@ -115,7 +128,7 @@ class EventHook(object):
called = 0 called = 0
returns = [] returns = []
for hook in self._hooks: for hook in self._hooks:
if max and called == max: if maximum and called == maximum:
break break
if event.eaten: if event.eaten:
break break