2018-10-30 14:58:48 +00:00
|
|
|
import itertools, time, traceback, typing
|
|
|
|
from src import Logging, utils
|
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-09-09 16:08:38 +00:00
|
|
|
PRIORITY_MONITOR = 4
|
2018-07-15 22:53:59 +00:00
|
|
|
|
2018-09-09 16:08:38 +00:00
|
|
|
DEFAULT_PRIORITY = PRIORITY_MEDIUM
|
2018-09-26 17:26:10 +00:00
|
|
|
DEFAULT_EVENT_DELIMITER = "."
|
|
|
|
DEFAULT_MULTI_DELIMITER = "|"
|
2018-08-31 11:55:52 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
CALLBACK_TYPE = typing.Callable[["Event"], typing.Any]
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Event(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, name: str, **kwargs):
|
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
|
2018-10-30 14:58:48 +00:00
|
|
|
def __getitem__(self, key: str) -> typing.Any:
|
2016-03-29 11:56:58 +00:00
|
|
|
return self.kwargs[key]
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, key: str, default=None) -> typing.Any:
|
2016-03-29 11:56:58 +00:00
|
|
|
return self.kwargs.get(key, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def __contains__(self, key: str) -> bool:
|
2016-03-29 11:56:58 +00:00
|
|
|
return key in self.kwargs
|
|
|
|
def eat(self):
|
|
|
|
self.eaten = True
|
|
|
|
|
|
|
|
class EventCallback(object):
|
2019-02-14 10:36:11 +00:00
|
|
|
def __init__(self, function: CALLBACK_TYPE, priority: int, kwargs: dict,
|
|
|
|
one_shot: bool=False):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.function = function
|
2018-07-02 11:23:33 +00:00
|
|
|
self.priority = priority
|
2016-03-29 11:56:58 +00:00
|
|
|
self.kwargs = kwargs
|
2018-10-30 17:49:35 +00:00
|
|
|
self.docstring = utils.parse.docstring(function.__doc__ or "")
|
2019-02-14 10:36:11 +00:00
|
|
|
self._one_shot = one_shot
|
2018-09-30 16:29:09 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def call(self, event: Event) -> typing.Any:
|
2016-03-29 11:56:58 +00:00
|
|
|
return self.function(event)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_kwarg(self, name: str, default=None) -> typing.Any:
|
2018-09-30 16:29:09 +00:00
|
|
|
item = self.kwargs.get(name, default)
|
|
|
|
return item or self.docstring.items.get(name, default)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class EventHook(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, log: Logging.Log, name: str = None,
|
|
|
|
parent: "EventHook" = None):
|
2018-09-27 10:07:29 +00:00
|
|
|
self.log = log
|
2016-07-13 06:31:09 +00:00
|
|
|
self.name = name
|
2018-08-28 11:23:57 +00:00
|
|
|
self.parent = parent
|
2018-12-02 10:04:05 +00:00
|
|
|
self._children = {} # type: typing.Dict[str, EventHook]
|
|
|
|
self._hooks = [] # type: typing.List[EventCallback]
|
2018-10-30 17:49:35 +00:00
|
|
|
self._replayed = False
|
2018-12-02 10:04:05 +00:00
|
|
|
self._stored_events = [] # type: typing.List[typing.Dict]
|
|
|
|
self._context_hooks = {} # type: typing.Dict[str, typing.List[EventCallback]]
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def _make_event(self, kwargs: dict) -> Event:
|
2018-09-29 08:24:10 +00:00
|
|
|
return Event(self._get_path(), **kwargs)
|
2019-01-30 19:35:43 +00:00
|
|
|
def make_event(self, **kwargs):
|
|
|
|
return self._make_event(kwargs)
|
2018-08-28 13:55:08 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def _get_path(self) -> str:
|
2018-09-01 17:27:10 +00:00
|
|
|
path = []
|
2018-10-30 17:49:35 +00:00
|
|
|
parent = self # type: typing.Optional[EventHook]
|
|
|
|
while not parent == None:
|
|
|
|
cast_parent = typing.cast(EventHook, parent)
|
|
|
|
if cast_parent.name == None:
|
|
|
|
break
|
|
|
|
|
|
|
|
path.append(typing.cast(str, cast_parent.name))
|
|
|
|
parent = cast_parent.parent
|
2018-09-26 17:26:10 +00:00
|
|
|
return DEFAULT_EVENT_DELIMITER.join(path[::-1])
|
2018-08-31 11:55:52 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def new_context(self, context: str) -> "EventHookContext":
|
2018-08-31 11:55:52 +00:00
|
|
|
return EventHookContext(self, context)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def hook(self, function: CALLBACK_TYPE, priority: int = DEFAULT_PRIORITY,
|
|
|
|
replay: bool = False, **kwargs) -> EventCallback:
|
2018-09-29 14:05:50 +00:00
|
|
|
return self._hook(function, None, priority, replay, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def _context_hook(self, context: str, function: CALLBACK_TYPE,
|
|
|
|
priority: int, replay: bool, kwargs: dict) -> EventCallback:
|
2018-09-29 14:05:50 +00:00
|
|
|
return self._hook(function, context, priority, replay, kwargs)
|
2018-10-30 17:49:35 +00:00
|
|
|
def _hook(self, function: CALLBACK_TYPE, context: typing.Optional[str],
|
|
|
|
priority: int, replay: bool, kwargs: dict) -> EventCallback:
|
2018-09-27 10:07:29 +00:00
|
|
|
callback = EventCallback(function, priority, kwargs)
|
2018-08-31 11:55:52 +00:00
|
|
|
|
|
|
|
if context == None:
|
|
|
|
self._hooks.append(callback)
|
|
|
|
else:
|
2018-12-02 10:04:05 +00:00
|
|
|
context_str = typing.cast(str, context)
|
2018-08-31 11:55:52 +00:00
|
|
|
if not context in self._context_hooks:
|
2018-12-02 10:04:05 +00:00
|
|
|
self._context_hooks[context_str] = []
|
|
|
|
self._context_hooks[context_str].append(callback)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2018-10-30 17:49:35 +00:00
|
|
|
if replay and not self._replayed:
|
2018-08-28 13:55:08 +00:00
|
|
|
for kwargs in self._stored_events:
|
2018-10-01 15:33:04 +00:00
|
|
|
self._call(kwargs, True, None)
|
2018-10-30 17:49:35 +00:00
|
|
|
self._replayed = True
|
2018-09-29 14:05:50 +00:00
|
|
|
return callback
|
|
|
|
|
2018-12-02 10:04:05 +00:00
|
|
|
def unhook(self, callback: "EventCallback"):
|
2018-09-29 14:05:50 +00:00
|
|
|
if callback in self._hooks:
|
|
|
|
self._hooks.remove(callback)
|
|
|
|
|
|
|
|
empty = []
|
|
|
|
for context, hooks in self._context_hooks.items():
|
|
|
|
if callback in hooks:
|
|
|
|
hooks.remove(callback)
|
|
|
|
if not hooks:
|
|
|
|
empty.append(context)
|
|
|
|
for context in empty:
|
|
|
|
del self._context_hooks[context]
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-10-30 17:49:35 +00:00
|
|
|
def _make_multiple_hook(self, source: "EventHook",
|
|
|
|
context: typing.Optional[str],
|
|
|
|
events: typing.Iterable[str]) -> "MultipleEventHook":
|
2018-09-26 17:26:10 +00:00
|
|
|
multiple_event_hook = MultipleEventHook()
|
|
|
|
for event in events:
|
|
|
|
event_hook = source.get_child(event)
|
|
|
|
if not context == None:
|
2018-10-30 17:49:35 +00:00
|
|
|
context_hook = event_hook.new_context(typing.cast(str, context))
|
|
|
|
multiple_event_hook._add(typing.cast(EventHook, context_hook))
|
|
|
|
else:
|
|
|
|
multiple_event_hook._add(event_hook)
|
2018-09-26 17:26:10 +00:00
|
|
|
return multiple_event_hook
|
|
|
|
|
2018-10-30 17:49:35 +00:00
|
|
|
def on(self, subevent: str, *extra_subevents: str,
|
|
|
|
delimiter: str = DEFAULT_EVENT_DELIMITER) -> "EventHook":
|
2018-08-31 11:55:52 +00:00
|
|
|
return self._on(subevent, extra_subevents, None, delimiter)
|
2018-10-30 14:58:48 +00:00
|
|
|
def _context_on(self, context: str, subevent: str,
|
2018-10-30 17:49:35 +00:00
|
|
|
extra_subevents: typing.Tuple[str, ...],
|
2018-10-30 14:58:48 +00:00
|
|
|
delimiter: str = DEFAULT_EVENT_DELIMITER) -> "EventHook":
|
2018-08-31 11:55:52 +00:00
|
|
|
return self._on(subevent, extra_subevents, context, delimiter)
|
2018-10-30 17:49:35 +00:00
|
|
|
def _on(self, subevent: str, extra_subevents: typing.Tuple[str, ...],
|
|
|
|
context: typing.Optional[str], delimiter: str) -> "EventHook":
|
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:
|
2018-09-26 17:26:10 +00:00
|
|
|
if DEFAULT_MULTI_DELIMITER in event_name:
|
2018-10-30 17:49:35 +00:00
|
|
|
multiple_hook = self._make_multiple_hook(event_obj, context,
|
2018-09-26 17:26:10 +00:00
|
|
|
event_name.split(DEFAULT_MULTI_DELIMITER))
|
2018-10-30 17:49:35 +00:00
|
|
|
return typing.cast(EventHook, multiple_hook)
|
2018-09-26 17:26:10 +00:00
|
|
|
|
2018-07-08 10:18:55 +00:00
|
|
|
event_obj = event_obj.get_child(event_name)
|
2018-08-31 11:55:52 +00:00
|
|
|
if not context == None:
|
2018-10-30 17:49:35 +00:00
|
|
|
context_hook = event_obj.new_context(typing.cast(str, context))
|
|
|
|
return typing.cast(EventHook, context_hook)
|
2018-07-08 10:18:55 +00:00
|
|
|
return event_obj
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if extra_subevents:
|
2018-10-30 17:49:35 +00:00
|
|
|
multiple_hook = self._make_multiple_hook(self, context,
|
2018-09-26 17:26:10 +00:00
|
|
|
(subevent,)+extra_subevents)
|
2018-10-30 17:49:35 +00:00
|
|
|
return typing.cast(EventHook, multiple_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:
|
2018-10-30 17:49:35 +00:00
|
|
|
context_child = child.new_context(typing.cast(str, context))
|
|
|
|
child = typing.cast(EventHook, context_child)
|
2018-08-31 11:55:52 +00:00
|
|
|
return child
|
2018-07-08 10:18:55 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def call_for_result(self, default=None, **kwargs) -> typing.Any:
|
2018-10-01 15:04:06 +00:00
|
|
|
return (self.call_limited(1, **kwargs) or [default])[0]
|
2018-08-28 13:55:08 +00:00
|
|
|
def assure_call(self, **kwargs):
|
2018-10-30 17:49:35 +00:00
|
|
|
if not self._replayed:
|
2018-08-28 13:55:08 +00:00
|
|
|
self._stored_events.append(kwargs)
|
|
|
|
else:
|
2018-10-01 15:04:06 +00:00
|
|
|
self._call(kwargs, True, None)
|
2018-10-30 14:58:48 +00:00
|
|
|
def call(self, **kwargs) -> typing.List[typing.Any]:
|
2018-10-01 15:04:06 +00:00
|
|
|
return self._call(kwargs, True, None)
|
2018-10-30 14:58:48 +00:00
|
|
|
def call_limited(self, maximum: int, **kwargs) -> typing.List[typing.Any]:
|
2018-10-30 17:49:35 +00:00
|
|
|
return self._call(kwargs, True, maximum)
|
2018-10-01 15:04:06 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def call_unsafe_for_result(self, default=None, **kwargs) -> typing.Any:
|
2018-10-01 15:04:06 +00:00
|
|
|
return (self.call_unsafe_limited(1, **kwargs) or [default])[0]
|
2018-10-30 14:58:48 +00:00
|
|
|
def call_unsafe(self, **kwargs) -> typing.List[typing.Any]:
|
2018-10-01 15:33:04 +00:00
|
|
|
return self._call(kwargs, False, None)
|
2018-10-30 14:58:48 +00:00
|
|
|
def call_unsafe_limited(self, maximum: int, **kwargs
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-10-01 15:04:06 +00:00
|
|
|
return self._call(kwargs, False, maximum)
|
|
|
|
|
2018-10-30 17:49:35 +00:00
|
|
|
def _call(self, kwargs: dict, safe: bool, maximum: typing.Optional[int]
|
2018-10-30 14:58:48 +00:00
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-28 15:35:24 +00:00
|
|
|
event_path = self._get_path()
|
2018-12-05 15:18:40 +00:00
|
|
|
hooks = self.get_hooks()
|
2018-12-06 10:15:12 +00:00
|
|
|
returns = [] # type: typing.List[typing.Any]
|
2018-12-05 15:18:40 +00:00
|
|
|
|
|
|
|
if not hooks:
|
|
|
|
self.log.trace("not calling non-hooked event \"%s\" (params: %s)",
|
2018-12-05 15:23:40 +00:00
|
|
|
[event_path, kwargs])
|
2018-12-05 15:18:40 +00:00
|
|
|
return returns
|
|
|
|
|
2018-11-26 14:23:20 +00:00
|
|
|
self.log.trace("calling event: \"%s\" (params: %s)",
|
2018-12-05 15:23:40 +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)
|
2018-12-05 15:18:40 +00:00
|
|
|
for hook in hooks[:maximum]:
|
2018-09-01 17:27:10 +00:00
|
|
|
if 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:
|
2018-10-16 13:53:37 +00:00
|
|
|
if safe:
|
|
|
|
self.log.error("failed to call event \"%s\"",
|
|
|
|
[self._get_path()], exc_info=True)
|
|
|
|
else:
|
2018-10-01 15:04:06 +00:00
|
|
|
raise
|
2018-08-28 11:23:57 +00:00
|
|
|
|
2019-02-14 10:36:11 +00:00
|
|
|
if hook._one_shot:
|
|
|
|
self.unhook(hook)
|
|
|
|
|
2018-09-01 17:27:10 +00:00
|
|
|
total_milliseconds = (time.monotonic() - start) * 1000
|
2018-11-26 14:23:20 +00:00
|
|
|
self.log.trace("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
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_child(self, child_name: str) -> "EventHook":
|
2016-03-29 11:56:58 +00:00
|
|
|
child_name_lower = child_name.lower()
|
|
|
|
if not child_name_lower in self._children:
|
2018-09-27 10:07:29 +00:00
|
|
|
self._children[child_name_lower] = EventHook(self.log,
|
2018-09-27 11:16:30 +00:00
|
|
|
child_name_lower, self)
|
2016-03-29 11:56:58 +00:00
|
|
|
return self._children[child_name_lower]
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_child(self, child_name: str):
|
2018-08-28 11:24:39 +00:00
|
|
|
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
|
|
|
|
2018-08-28 11:24:39 +00:00
|
|
|
def check_purge(self):
|
2018-09-01 17:27:10 +00:00
|
|
|
if self.is_empty() and not self.parent == None:
|
2018-08-28 11:24:39 +00:00
|
|
|
self.parent.remove_child(self.name)
|
|
|
|
self.parent.check_purge()
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_context(self, context: str):
|
2018-08-31 11:55:52 +00:00
|
|
|
del self._context_hooks[context]
|
2018-10-30 14:58:48 +00:00
|
|
|
def has_context(self, context: str) -> bool:
|
2018-08-31 11:55:52 +00:00
|
|
|
return context in self._context_hooks
|
2018-10-30 14:58:48 +00:00
|
|
|
def purge_context(self, context: str):
|
2018-08-31 11:55:52 +00:00
|
|
|
if self.has_context(context):
|
|
|
|
self.remove_context(context)
|
2018-09-01 17:27:10 +00:00
|
|
|
|
2018-09-01 10:29:26 +00:00
|
|
|
for child_name in self.get_children()[:]:
|
|
|
|
child = self.get_child(child_name)
|
2018-08-31 11:55:52 +00:00
|
|
|
child.purge_context(context)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_hooks(self) -> typing.List[EventCallback]:
|
2018-09-01 17:27:10 +00:00
|
|
|
return sorted(self._hooks + sum(self._context_hooks.values(), []),
|
|
|
|
key=lambda e: e.priority)
|
2018-10-30 17:49:35 +00:00
|
|
|
def get_children(self) -> typing.List[str]:
|
2018-09-01 10:29:26 +00:00
|
|
|
return list(self._children.keys())
|
2018-10-30 14:58:48 +00:00
|
|
|
def is_empty(self) -> bool:
|
2018-10-30 17:49:35 +00:00
|
|
|
return (len(self.get_hooks())+len(self.get_children())) == 0
|
2018-10-30 14:58:48 +00:00
|
|
|
|
|
|
|
class MultipleEventHook(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._event_hooks = set([])
|
|
|
|
def _add(self, event_hook: EventHook):
|
|
|
|
self._event_hooks.add(event_hook)
|
|
|
|
|
|
|
|
def hook(self, function: CALLBACK_TYPE, **kwargs):
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
event_hook.hook(function, **kwargs)
|
|
|
|
|
|
|
|
def call_limited(self, maximum: int, **kwargs) -> typing.List[typing.Any]:
|
|
|
|
returns = []
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
returns.append(event_hook.call_limited(maximum, **kwargs))
|
|
|
|
return returns
|
|
|
|
def call(self, **kwargs) -> typing.List[typing.Any]:
|
|
|
|
returns = []
|
|
|
|
for event_hook in self._event_hooks:
|
|
|
|
returns.append(event_hook.call(**kwargs))
|
|
|
|
return returns
|
|
|
|
|
|
|
|
class EventHookContext(object):
|
|
|
|
def __init__(self, parent, context):
|
|
|
|
self._parent = parent
|
|
|
|
self.context = context
|
2019-01-30 19:37:52 +00:00
|
|
|
|
|
|
|
def make_event(self, **kwargs):
|
|
|
|
return self._parent.make_event(**kwargs)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def hook(self, function: CALLBACK_TYPE, priority: int = DEFAULT_PRIORITY,
|
|
|
|
replay: bool = False, **kwargs) -> EventCallback:
|
|
|
|
return self._parent._context_hook(self.context, function, priority,
|
|
|
|
replay, kwargs)
|
|
|
|
def unhook(self, callback: EventCallback):
|
|
|
|
self._parent.unhook(callback)
|
|
|
|
|
|
|
|
def on(self, subevent: str, *extra_subevents,
|
|
|
|
delimiter: str = DEFAULT_EVENT_DELIMITER) -> EventHook:
|
|
|
|
return self._parent._context_on(self.context, subevent,
|
|
|
|
extra_subevents, delimiter)
|
|
|
|
|
|
|
|
def call_for_result(self, default=None, **kwargs) -> typing.Any:
|
|
|
|
return self._parent.call_for_result(default, **kwargs)
|
|
|
|
def assure_call(self, **kwargs):
|
|
|
|
self._parent.assure_call(**kwargs)
|
|
|
|
def call(self, **kwargs) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.call(**kwargs)
|
|
|
|
def call_limited(self, maximum: int, **kwargs) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.call_limited(maximum, **kwargs)
|
|
|
|
|
|
|
|
def call_unsafe_for_result(self, default=None, **kwargs) -> typing.Any:
|
|
|
|
return self._parent.call_unsafe_for_result(default, **kwargs)
|
|
|
|
def call_unsafe(self, **kwargs) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.call_unsafe(**kwargs)
|
|
|
|
def call_unsafe_limited(self, maximum: int, **kwargs
|
|
|
|
) -> typing.List[typing.Any]:
|
|
|
|
return self._parent.call_unsafe_limited(maximum, **kwargs)
|
|
|
|
|
|
|
|
def get_hooks(self) -> typing.List[EventCallback]:
|
|
|
|
return self._parent.get_hooks()
|
|
|
|
def get_children(self) -> typing.List[EventHook]:
|
|
|
|
return self._parent.get_children()
|