-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
50 lines (35 loc) · 1.22 KB
/
events.py
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
#!/usr/bin/env python
# encoding: utf-8
from __future__ import unicode_literals
from __future__ import print_function
from collections import defaultdict
from itertools import chain
class Event(object):
def __init__(self, type_, **kwargs):
self.type = type_
self.props = kwargs
for name, value in kwargs.items():
setattr(self, name, value)
def __unicode__(self):
return '<Event (' + self.type + ')>'
__str__ = __repr__ = __unicode__
class EventSystem(object):
def init(self):
# So that the subclasses don't have to call this class's __init__
# method.
try:
self.handlers
except AttributeError:
self.handlers = defaultdict(list)
def emit(self, event, **kwargs):
self.init()
if not isinstance(event, Event):
event = Event(event, **kwargs)
elif kwargs:
raise ValueError('When emitting an event object, no keyword '
'arguments are accepted.')
for handler in chain(self.handlers[event.type], self.handlers['*']):
handler(event)
def listen(self, event_name, fn):
self.init()
self.handlers[event_name].append(fn)