aboutsummaryrefslogtreecommitdiff
path: root/src/EventManager.py
blob: f10ebcd436f0a1fd4d3267cb254b9011fec2c5c6 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import itertools, time, traceback, typing
from src import Logging, utils

PRIORITY_URGENT = 0
PRIORITY_HIGH = 1
PRIORITY_MEDIUM = 2
PRIORITY_LOW = 3
PRIORITY_MONITOR = 4

DEFAULT_PRIORITY = PRIORITY_MEDIUM
DEFAULT_EVENT_DELIMITER = "."
DEFAULT_MULTI_DELIMITER = "|"

class Event(object):
    def __init__(self, name: str, kwargs):
        self.name = name
        self.kwargs = kwargs
        self.eaten = False
    def __getitem__(self, key: str) -> typing.Any:
        return self.kwargs[key]
    def get(self, key: str, default=None) -> typing.Any:
        return self.kwargs.get(key, default)
    def __contains__(self, key: str) -> bool:
        return key in self.kwargs
    def eat(self):
        self.eaten = True

CALLBACK_TYPE = typing.Callable[[Event], typing.Any]

class EventHook(object):
    def __init__(self, event_name: str, func: CALLBACK_TYPE,
            context: typing.Optional[str], priority: int,
            kwargs: typing.List[typing.Tuple[str, typing.Any]]):
        self.event_name = event_name
        self.function = func
        self.context = context
        self.priority = priority
        self.docstring = utils.parse.docstring(func.__doc__ or "")

        self.call_count = 0
        self._kwargs: typing.Dict[str, typing.Any] = {}
        self._multi_kwargs: typing.Dict[str, typing.List[typing.Any]] = {}
        for key, value in kwargs:
            if key in self._multi_kwargs:
                self._multi_kwargs[key].append(value)
            elif key in self._kwargs:
                self._multi_kwargs[key] = [self._kwargs.pop(key), value]
            else:
                self._kwargs[key] = value

    def call(self, event: Event) -> typing.Any:
        self.call_count += 1
        return self.function(event)

    def get_kwargs(self, key: str) -> typing.List[typing.Any]:
        if key in self._kwargs:
            return [self._kwargs[key]]
        elif key in self._multi_kwargs:
            return self._multi_kwargs[key].copy()
        elif key in self.docstring.var_items:
            return self.docstring.var_items[key]
        elif key in self.docstring.items:
            return [self.docstring.items[key]]
        return []
    def get_kwarg(self, key: str, default: typing.Any=None) -> typing.Any:
        return (self.get_kwargs(key) or [default])[0]

class Events(object):
    def __init__(self, root: "EventRoot", path: typing.List[str],
            context: typing.Optional[str]):
        self._root = root
        self._path = path
        self._context = context

    def new_root(self):
        return self._root._new_root()

    def new_context(self, context: str):
        return self._root._new_context(context)

    def make_event(self, **kwargs):
        return self._root._make_event(self._path, kwargs)

    def on(self, subname):
        parts = subname.split(DEFAULT_EVENT_DELIMITER)
        new_path = self._path + parts

        return Events(self._root, new_path, self._context)

    def hook(self, func: CALLBACK_TYPE, priority: int = DEFAULT_PRIORITY,
            **kwargs):
        self._hook(func, priority, list(kwargs.items()))
    def _hook(self, func: CALLBACK_TYPE, priority: int = DEFAULT_PRIORITY,
            kwargs: typing.List[typing.Tuple[str, typing.Any]] = []):
        for key, value in kwargs:
            if key == "priority":
                priority = value
                break
        self._root._hook(self._path, func, self._context, priority, kwargs)

    def call(self, **kwargs):
        return self._root._call(self._path, kwargs, True, self._context, None)
    def call_unsafe(self, **kwargs):
        return self._root._call(self._path, kwargs, False, self._context, None)

    def _call_limited(self, maximum: int, safe: bool, kwargs):
        return self._root._call(self._path, kwargs, safe, self._context,
            maximum)
    def call_limited(self, maximum: int, **kwargs):
        return self._call_limited(maximum, True, kwargs)
    def call_limited_unsafe(self, maximum: int, **kwargs):
        return self._call_limited(maximum, False, kwargs)

    def call_for_result(self, default=None, **kwargs):
        return (self._call_limited(1, True, kwargs) or [default])[0]
    def call_for_result_unsafe(self, default=None, **kwargs):
        return (self._call_limited(1, False, kwargs) or [default])[0]

    def get_children(self):
        return self._root._get_children(self._path)
    def get_hooks(self):
        return self._root._get_hooks(self._path)

    def purge_context(self, context: str):
        self._root._purge_context(context)

    def all_hooks(self):
        return self._root.all_hooks()

class EventRoot(object):
    def __init__(self, log: Logging.Log):
        self.log = log
        self._hooks: typing.Dict[str, typing.List[EventHook]] = {}

    def _make_event(self, path: typing.List[str], kwargs: dict):
        return Event(self._path_str(path), kwargs)

    def _new_context(self, context: str):
        return Events(self, [], context)
    def _new_root(self):
        return EventRoot(self.log).wrap()

    def wrap(self):
        return Events(self, [], None)

    def _path_str(self, path: typing.List[str]):
        path_lower = [p.lower() for p in path]
        return DEFAULT_EVENT_DELIMITER.join(path_lower)

    def _hook(self, path: typing.List[str], func: CALLBACK_TYPE,
            context: typing.Optional[str], priority: int,
            kwargs: typing.List[typing.Tuple[str, typing.Any]] = []
            ) -> EventHook:
        path_str = self._path_str(path)
        new_hook = EventHook(path_str, func, context, priority, kwargs)

        if not path_str in self._hooks:
            self._hooks[path_str] = []
        hook_array = self._hooks[path_str]

        hooked = False
        for i, other_hook in enumerate(hook_array):
            if other_hook.priority > new_hook.priority:
                hooked = True
                hook_array.insert(i, new_hook)
                break
        if not hooked:
            hook_array.append(new_hook)
        return new_hook

    def _call(self, path: typing.List[str], kwargs: dict, safe: bool,
            context: typing.Optional[str], maximum: typing.Optional[int]
            ) -> typing.List[typing.Any]:
        if not utils.is_main_thread():
            raise RuntimeError("Can't call events outside of main thread")

        returns: typing.List[typing.Any] = []
        path_str = self._path_str(path)
        if not path_str in self._hooks:
            self.log.trace("not calling non-hooked event \"%s\" (params: %s)",
                [path_str, str(kwargs)])
            return returns

        self.log.trace("calling event: \"%s\" (params: %s)",
            [path_str, str(kwargs)])
        start = time.monotonic()

        hooks = self._hooks[path_str]
        if maximum:
            hooks = hooks[:maximum]
        event = self._make_event(path, kwargs)

        for hook in hooks:
            if event.eaten:
                break

            try:
                returned = hook.call(event)
            except Exception as e:
                if safe:
                    self.log.error("failed to call event \"%s\"",
                        [path_str], exc_info=True)
                    continue
                else:
                    raise
            returns.append(returned)

        total_milliseconds = (time.monotonic() - start) * 1000
        self.log.trace("event \"%s\" called in %fms",
            [path_str, total_milliseconds])

        return returns

    def _purge_context(self, context: str):
        context_hooks: typing.Dict[str, typing.List[EventHook]] = {}
        for path in self._hooks.keys():
            for hook in self._hooks[path]:
                if hook.context == context:
                    if not path in context_hooks:
                        context_hooks[path] = []
                    context_hooks[path].append(hook)
        for path in context_hooks:
            for hook in context_hooks[path]:
                self._hooks[path].remove(hook)
                if not self._hooks[path]:
                    del self._hooks[path]

    def _get_children(self, path):
        path_prefix = "%s%s" % (self._path_str(path), DEFAULT_EVENT_DELIMITER)
        matches = []
        for key in self._hooks.keys():
            if key.startswith(path_prefix):
                matches.append(key.replace(path_prefix, "", 1))
        return matches
    def _get_hooks(self, path):
        path_str = self._path_str(path)
        if path_str in self._hooks:
            return self._hooks[path_str][:]
        return []

    def all_hooks(self):
        return self._hooks.copy()