2018-08-28 11:23:57 +00:00
|
|
|
import time, traceback
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-07-15 22:53:59 +00:00
|
|
|
PRIORITY_URGENT = 0
|
|
|
|
PRIORITY_HIGH = 1
|
|
|
|
PRIORITY_MEDIUM = 2
|
|
|
|
PRIORITY_LOW = 3
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Event(object):
|
2016-07-13 23:42:17 +00:00
|
|
|
def __init__(self, bot, name, **kwargs):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2016-07-13 23:42:17 +00:00
|
|
|
self.name = name
|
2016-03-29 11:56:58 +00:00
|
|
|
self.kwargs = kwargs
|
|
|
|
self.eaten = False
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.kwargs[key]
|
|
|
|
def get(self, key, default=None):
|
|
|
|
return self.kwargs.get(key, default)
|
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self.kwargs
|
|
|
|
def eat(self):
|
|
|
|
self.eaten = True
|
|
|
|
|
|
|
|
class EventCallback(object):
|
2018-07-02 11:23:33 +00:00
|
|
|
def __init__(self, function, bot, priority, **kwargs):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.function = function
|
|
|
|
self.bot = bot
|
2018-07-02 11:23:33 +00:00
|
|
|
self.priority = priority
|
2016-03-29 11:56:58 +00:00
|
|
|
self.kwargs = kwargs
|
|
|
|
def call(self, event):
|
|
|
|
return self.function(event)
|
|
|
|
|
|
|
|
class MultipleEventHook(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._event_hooks = set([])
|
|
|
|
def _add(self, event_hook):
|
|
|
|
self._event_hooks.add(event_hook)
|
|
|
|
def hook(self, function, **kwargs):
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
event_hook.hook(function, **kwargs)
|
|
|
|
def call(self, max=None, **kwargs):
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
event_hook.call(max, **kwargs)
|
|
|
|
|
|
|
|
class EventHook(object):
|
2018-08-28 11:23:57 +00:00
|
|
|
def __init__(self, bot, name=None, parent=None):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2016-07-13 06:31:09 +00:00
|
|
|
self.name = name
|
2018-08-28 11:23:57 +00:00
|
|
|
self.parent = parent
|
2016-03-29 11:56:58 +00:00
|
|
|
self._children = {}
|
|
|
|
self._hooks = []
|
|
|
|
self._hook_notify = None
|
|
|
|
self._child_notify = None
|
|
|
|
self._call_notify = None
|
2017-12-26 10:32:36 +00:00
|
|
|
self._stored_events = []
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-08-28 13:55:08 +00:00
|
|
|
def _make_event(self, kwargs):
|
|
|
|
return Event(self.bot, self.name, **kwargs)
|
|
|
|
|
2018-08-28 11:23:57 +00:00
|
|
|
def _get_path(self):
|
|
|
|
path = [self.name]
|
|
|
|
parent = self.parent
|
|
|
|
while not parent == None and not parent.name == None:
|
|
|
|
path.append(parent.name)
|
|
|
|
parent = parent.parent
|
|
|
|
return ".".join(path[::-1])
|
|
|
|
|
2018-07-15 22:53:59 +00:00
|
|
|
def hook(self, function, priority=PRIORITY_LOW, replay=False, **kwargs):
|
2018-07-02 11:23:33 +00:00
|
|
|
callback = EventCallback(function, self.bot, priority, **kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
if self._hook_notify:
|
|
|
|
self._hook_notify(self, callback)
|
|
|
|
self._hooks.append(callback)
|
2018-07-02 11:23:33 +00:00
|
|
|
self._hooks.sort(key=lambda x: x.priority)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
|
|
|
if replay:
|
2018-08-28 13:55:08 +00:00
|
|
|
for kwargs in self._stored_events:
|
|
|
|
callback.call(self._make_event(kwargs))
|
2017-12-26 10:32:36 +00:00
|
|
|
self._stored_events = None
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2016-04-14 15:47:33 +00:00
|
|
|
def _unhook(self, hook):
|
|
|
|
self._hooks.remove(hook)
|
2018-07-08 10:18:55 +00:00
|
|
|
|
|
|
|
def on(self, subevent, *extra_subevents, delimiter="."):
|
|
|
|
if delimiter in subevent:
|
|
|
|
event_chain = subevent.split(delimiter)
|
|
|
|
event_obj = self
|
|
|
|
for event_name in event_chain:
|
|
|
|
event_obj = event_obj.get_child(event_name)
|
|
|
|
return event_obj
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if extra_subevents:
|
|
|
|
multiple_event_hook = MultipleEventHook()
|
|
|
|
for extra_subevent in (subevent,)+extra_subevents:
|
|
|
|
multiple_event_hook._add(self.get_child(extra_subevent))
|
|
|
|
return multiple_event_hook
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
return self.get_child(subevent)
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-07-15 14:15:52 +00:00
|
|
|
def call_for_result(self, default=None, max=None, **kwargs):
|
|
|
|
results = self.call(max=max, **kwargs)
|
|
|
|
return default if not len(results) else results[0]
|
2018-08-28 13:55:08 +00:00
|
|
|
def assure_call(self, **kwargs):
|
|
|
|
if not self._stored_events == None:
|
|
|
|
self._stored_events.append(kwargs)
|
|
|
|
else:
|
2018-08-28 14:13:56 +00:00
|
|
|
self.call(**kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
def call(self, max=None, **kwargs):
|
2018-08-28 11:23:57 +00:00
|
|
|
self.bot.log.debug("calling event: \"%s\" (params: %s)",
|
|
|
|
[self._get_path(), kwargs])
|
|
|
|
start = time.monotonic()
|
|
|
|
|
2018-08-28 13:55:08 +00:00
|
|
|
event = self._make_event(kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
if self._call_notify:
|
|
|
|
self._call_notify(self, event)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
called = 0
|
|
|
|
returns = []
|
|
|
|
for hook in self._hooks:
|
|
|
|
if max and called == max:
|
|
|
|
break
|
|
|
|
if event.eaten:
|
|
|
|
break
|
2016-07-13 23:42:17 +00:00
|
|
|
try:
|
|
|
|
returns.append(hook.call(event))
|
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
# TODO don't make this an event call. can lead to error cycles!
|
2017-01-27 21:36:54 +00:00
|
|
|
#self.bot.events.on("log").on("error").call(
|
|
|
|
# message="Failed to call event callback",
|
|
|
|
# data=traceback.format_exc())
|
2016-03-29 11:56:58 +00:00
|
|
|
called += 1
|
2018-08-28 11:23:57 +00:00
|
|
|
|
|
|
|
end = time.monotonic()
|
|
|
|
total_milliseconds = (end - start) * 1000
|
|
|
|
self.bot.log.debug("event called in %fms", [total_milliseconds])
|
|
|
|
|
2018-08-28 11:24:39 +00:00
|
|
|
self.check_purge()
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
return returns
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def get_child(self, child_name):
|
|
|
|
child_name_lower = child_name.lower()
|
|
|
|
if not child_name_lower in self._children:
|
2016-07-13 06:31:09 +00:00
|
|
|
self._children[child_name_lower] = EventHook(self.bot,
|
2018-08-28 11:23:57 +00:00
|
|
|
child_name, self)
|
2016-03-29 11:56:58 +00:00
|
|
|
if self._child_notify:
|
|
|
|
self._child_notify(self, self._children[
|
|
|
|
child_name_lower])
|
|
|
|
return self._children[child_name_lower]
|
2018-08-28 11:24:39 +00:00
|
|
|
|
|
|
|
def remove_child(self, child_name):
|
|
|
|
child_name_lower = child_name.lower()
|
|
|
|
if child_name_lower in self._children:
|
|
|
|
del self._children[child_name_lower]
|
|
|
|
def check_purge(self):
|
|
|
|
if len(self._hooks) == 0 and len(self._children
|
|
|
|
) == 0 and not self.parent == None:
|
|
|
|
self.parent.remove_child(self.name)
|
|
|
|
self.parent.check_purge()
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def get_hooks(self):
|
|
|
|
return self._hooks
|
|
|
|
def get_children(self):
|
|
|
|
return self._children.keys()
|
|
|
|
def set_hook_notify(self, handler):
|
|
|
|
self._hook_notify = handler
|
|
|
|
def set_child_notify(self, handler):
|
|
|
|
self._child_notify = handler
|
|
|
|
def set_call_notify(self, handler):
|
|
|
|
self._call_notify = handler
|