2018-08-31 11:55:52 +00:00
|
|
|
import itertools, 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
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
DEFAULT_PRIORITY = PRIORITY_LOW
|
|
|
|
DEFAULT_DELIMITER = "."
|
|
|
|
|
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-08-31 11:55:52 +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)
|
2018-08-28 15:04:35 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def hook(self, function, **kwargs):
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
event_hook.hook(function, **kwargs)
|
2018-08-28 15:04:35 +00:00
|
|
|
|
|
|
|
def call_limited(self, maximum, **kwargs):
|
|
|
|
returns = []
|
2016-03-29 11:56:58 +00:00
|
|
|
for event_hook in self._event_hooks:
|
2018-08-28 15:04:35 +00:00
|
|
|
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
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
class EventHookContext(object):
|
|
|
|
def __init__(self, parent, context):
|
|
|
|
self._parent = parent
|
|
|
|
self.context = context
|
|
|
|
def hook(self, function, priority=DEFAULT_PRIORITY, **kwargs):
|
|
|
|
self._parent._context_hook(self.context, function, priority, kwargs)
|
|
|
|
def on(self, subevent, *extra_subevents, delimiter=DEFAULT_DELIMITER):
|
|
|
|
return self._parent._context_on(self.context, subevent,
|
|
|
|
extra_subevents, delimiter)
|
|
|
|
def call_for_result(self, default=None, **kwargs):
|
|
|
|
return self._parent.call_for_result(default, **kwargs)
|
|
|
|
def assure_call(self, **kwargs):
|
|
|
|
self._parent.assure_call(**kwargs)
|
|
|
|
def call(self, **kwargs):
|
|
|
|
return self._parent.call(**kwargs)
|
|
|
|
def call_limited(self, maximum, **kwargs):
|
|
|
|
return self._parent.call_limited(maximum, **kwargs)
|
|
|
|
def get_hooks(self):
|
|
|
|
return self._parent.get_hooks()
|
|
|
|
def get_children(self):
|
|
|
|
return self._parent.get_children()
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
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 = []
|
2017-12-26 10:32:36 +00:00
|
|
|
self._stored_events = []
|
2018-08-31 11:55:52 +00:00
|
|
|
self._context_hooks = {}
|
|
|
|
self._current_context = None
|
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
|
2018-08-31 11:55:52 +00:00
|
|
|
return DEFAULT_DELIMITER.join(path[::-1])
|
|
|
|
|
|
|
|
def new_context(self, context):
|
|
|
|
return EventHookContext(self, context)
|
|
|
|
|
|
|
|
def hook(self, function, priority=DEFAULT_PRIORITY, replay=False,
|
|
|
|
**kwargs):
|
|
|
|
self._hook(function, None, priority, replay, kwargs)
|
|
|
|
def _context_hook(self, context, function, priority, kwargs):
|
|
|
|
self._hook(function, context, priority, False, kwargs)
|
|
|
|
def _hook(self, function, context, priority, replay, kwargs):
|
|
|
|
callback = EventCallback(function, self.bot, priority, kwargs)
|
|
|
|
|
|
|
|
if context == None:
|
|
|
|
self._hooks.append(callback)
|
|
|
|
hooks = self._hooks
|
|
|
|
else:
|
|
|
|
if not context in self._context_hooks:
|
|
|
|
self._context_hooks[context] = []
|
|
|
|
self._context_hooks[context].append(callback)
|
|
|
|
hooks = self._context_hooks[context]
|
|
|
|
hooks.sort(key=lambda x: x.priority)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2018-08-28 15:04:35 +00:00
|
|
|
if replay and not self._stored_events == None:
|
2018-08-28 13:55:08 +00:00
|
|
|
for kwargs in self._stored_events:
|
2018-08-28 15:04:35 +00:00
|
|
|
self._call(kwargs)
|
2017-12-26 10:32:36 +00:00
|
|
|
self._stored_events = None
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
def on(self, subevent, *extra_subevents, delimiter=DEFAULT_DELIMITER):
|
|
|
|
return self._on(subevent, extra_subevents, None, delimiter)
|
|
|
|
def _context_on(self, context, subevent, extra_subevents,
|
|
|
|
delimiter=DEFAULT_DELIMITER):
|
|
|
|
return self._on(subevent, extra_subevents, context, delimiter)
|
|
|
|
def _on(self, subevent, extra_subevents, context, delimiter):
|
2018-07-08 10:18:55 +00:00
|
|
|
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)
|
2018-08-31 11:55:52 +00:00
|
|
|
if not context == None:
|
|
|
|
event_obj = event_obj.new_context(context)
|
2018-07-08 10:18:55 +00:00
|
|
|
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:
|
2018-08-31 11:55:52 +00:00
|
|
|
child = self.get_child(extra_subevent)
|
|
|
|
if not context == None:
|
|
|
|
child = child.new_context(context)
|
|
|
|
multiple_event_hook._add(child)
|
2016-03-29 11:56:58 +00:00
|
|
|
return multiple_event_hook
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
child = self.get_child(subevent)
|
|
|
|
if not context == None:
|
|
|
|
child = child.new_context(context)
|
|
|
|
return child
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
def call_for_result(self, default=None, **kwargs):
|
|
|
|
results = self.call_limited(0, **kwargs)
|
2018-07-15 14:15:52 +00:00
|
|
|
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 15:04:35 +00:00
|
|
|
self._call(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):
|
2018-08-28 15:35:24 +00:00
|
|
|
event_path = self._get_path()
|
2018-08-28 11:23:57 +00:00
|
|
|
self.bot.log.debug("calling event: \"%s\" (params: %s)",
|
2018-08-28 15:35:24 +00:00
|
|
|
[event_path, kwargs])
|
2018-08-28 11:23:57 +00:00
|
|
|
start = time.monotonic()
|
|
|
|
|
2018-08-28 13:55:08 +00:00
|
|
|
event = self._make_event(kwargs)
|
2016-03-29 11:56:58 +00:00
|
|
|
called = 0
|
|
|
|
returns = []
|
2018-08-31 11:55:52 +00:00
|
|
|
for hook in self.get_hooks():
|
|
|
|
if (maximum and called == maximum) or event.eaten:
|
2016-03-29 11:56:58 +00:00
|
|
|
break
|
2016-07-13 23:42:17 +00:00
|
|
|
try:
|
|
|
|
returns.append(hook.call(event))
|
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
2018-08-30 13:32:59 +00:00
|
|
|
self.bot.log.error("failed to call event \"%s", [
|
|
|
|
event_path], exc_info=True)
|
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
|
2018-08-28 15:35:24 +00:00
|
|
|
self.bot.log.debug("event \"%s\" called in %fms", [
|
|
|
|
event_path, total_milliseconds])
|
2018-08-28 11:23:57 +00:00
|
|
|
|
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-29 14:51:51 +00:00
|
|
|
child_name_lower, self)
|
2016-03-29 11:56:58 +00:00
|
|
|
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]
|
2018-08-31 11:55:52 +00:00
|
|
|
def get_children(self):
|
|
|
|
return self._children.keys()
|
|
|
|
|
2018-08-28 11:24:39 +00:00
|
|
|
def check_purge(self):
|
2018-08-31 12:14:31 +00:00
|
|
|
if len(self.get_hooks()) == 0 and len(self._children
|
2018-08-28 11:24:39 +00:00
|
|
|
) == 0 and not self.parent == None:
|
|
|
|
self.parent.remove_child(self.name)
|
|
|
|
self.parent.check_purge()
|
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
def remove_context(self, context):
|
|
|
|
del self._context_hooks[context]
|
|
|
|
def has_context(self, context):
|
|
|
|
return context in self._context_hooks
|
|
|
|
def purge_context(self, context):
|
|
|
|
if self.has_context(context):
|
|
|
|
self.remove_context(context)
|
|
|
|
for child in self.get_children():
|
|
|
|
child.purge_context(context)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def get_hooks(self):
|
2018-08-31 11:55:52 +00:00
|
|
|
return self._hooks + list(itertools.chain.from_iterable(
|
|
|
|
self._context_hooks.values()))
|