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
|
'''This module manages events and evet handlers'''
event_handlers = []
def register_handler(handler):
'''Register an event handler'''
if isinstance(handler, Handler):
event_handlers.append(handler)
else:
raise Exception('Object must be a subclass of libs.events.Handler')
def unregister_handler(handler):
'''Unregister an event handler'''
event_handlers.remove(handler)
def broadcast(event_type, *args):
'''Broadcast and event to all handlers in `handlers`'''
for handler in event_handlers:
getattr(handler, event_type)(*args)
class Handler(object):
'''Base handler class. Provides default methods to handle events. This must
be used by all modules and is done to ensure compatibility.'''
def got_message(self, message):
'''Called when a message is recieved'''
pass
def got_connect(self, connection):
'''Called when we get or establish a connection'''
pass
def web_ui_request(self, path, connection):
'''The user made a request to our localhost web address.
`path` is the file path requested by the user.
`connection` is what we will use to send data back'''
pass
|