-
Notifications
You must be signed in to change notification settings - Fork 81
/
callbacks.py
87 lines (66 loc) · 3.81 KB
/
callbacks.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
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
# -*- coding: utf-8 -*-
from errors import UnknownCallbackError
from constants import *
class CallBacks(dict):
"""
Define the callbacks that can be registered by the application.
Multiple functions can be assigned to a callback using "add_callback"
The call is done from within the pymumble loop thread, it's important to
keep processing short to avoid delays on audio transmission
"""
def __init__(self):
self.update(
{PYMUMBLE_CLBK_CONNECTED: None, # Connection succeeded
PYMUMBLE_CLBK_CHANNELCREATED: None, # send the created channel object as parameter
PYMUMBLE_CLBK_CHANNELUPDATED: None, # send the updated channel object and a dict with all the modified fields as parameter
PYMUMBLE_CLBK_CHANNELREMOVED: None, # send the removed channel object as parameter
PYMUMBLE_CLBK_USERCREATED: None, # send the added user object as parameter
PYMUMBLE_CLBK_USERUPDATED: None, # send the updated user object and a dict with all the modified fields as parameter
PYMUMBLE_CLBK_USERREMOVED: None, # send the removed user object and the mumble message as parameter
PYMUMBLE_CLBK_SOUNDRECEIVED: None, # send the user object that received the sound and the SoundChunk object itself
PYMUMBLE_CLBK_TEXTMESSAGERECEIVED: None, # Send the received message
} )
def set_callback(self, callback, dest):
"""Define the function to call for a specific callback. Suppress any axisting callback function"""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
self[callback] = [dest]
def add_callback(self, callback, dest):
"""Add the function to call for a specific callback."""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
if self[callback] is None:
self[callback] = list()
self[callback].append(dest)
def get_callback(self, callback):
"""Get the functions assigned to a callback as a list. Return None if no callback defined"""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
return self[callback]
def remove_callback(self, callback, dest):
"""Remove a specific function from a specific callback. Function object must be the one added before."""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
if self[callback] is None or dest not in self[callback]:
raise UnknownCallbackError("Function not registered for callback \"%s\"." % callback)
self[callback].remove(dest)
if len(self[callback]) == 0:
self[callback] = None
def reset_callback(self, callback):
"""remove functions for a defined callback"""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
self[callback] = None
def call_callback(self, callback, *pos_parameters):
"""Call all the registered function for a specific callback."""
if callback not in self:
raise UnknownCallbackError("Callback \"%s\" does not exists." % callback)
if self[callback]:
for func in self[callback]:
func(*pos_parameters)
def __call__(self, callback, *pos_parameters):
"""shortcut to be able to call the dict element as a function"""
self.call_callback(callback, *pos_parameters)
def get_callbacks_list(self):
"""Get a list of all callbacks"""
return self.keys()