-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcallback.py
More file actions
122 lines (98 loc) · 5.51 KB
/
callback.py
File metadata and controls
122 lines (98 loc) · 5.51 KB
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
## INFO ########################################################################
## ##
## plastey ##
## ======= ##
## ##
## Oculus Rift + Leap Motion + Python 3 + C + Blender + Arch Linux ##
## Version: 0.1.6.672 (20150502) ##
## File: callback.py ##
## ##
## For more information about the project, visit ##
## <http://plastey.kibu.hu>. ##
## Copyright (C) 2015 Peter Varo, Kitchen Budapest ##
## ##
## This program is free software: you can redistribute it and/or modify it ##
## under the terms of the GNU General Public License as published by the ##
## Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, but ##
## WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ##
## See the GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program, most likely a file in the root directory, ##
## called 'LICENSE'. If not, see <http://www.gnu.org/licenses>. ##
## ##
######################################################################## INFO ##
# Import python modules
from collections import OrderedDict
#------------------------------------------------------------------------------#
class CallbackManager:
# Class level constants
REFERENCE_ERROR = ('{!r} is not a valid callback '
'reference for {.__class__.__name__!r}')
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __init__(self, valid_references=None):
self._states = {}
if valid_references is None:
self._restricted = False
self._callbacks = OrderedDict()
else:
self._restricted = True
self._callbacks = OrderedDict.fromkeys(valid_references, [])
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def append_callback(self, reference, callback):
# If reference already has a list of callbacks
try:
# Store the new callback
self._callbacks[reference].append(callback)
# If reference doesn't have a list of callbacks
except KeyError:
# If this manager-object is restricted
if self._restricted:
# Tell user, he can't set such reference
raise KeyError(self.REFERENCE_ERROR.format(reference, self))
# Create list of callbacks and store the new callback
self._callbacks[reference] = [callback]
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def remove_callback(self, reference, index=-1):
self._callbacks[reference].pop(index)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def remove_callbacks(self, reference):
if self._restricted:
self._callbacks[reference] = []
else:
del self._callbacks[reference]
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def remove_all_callbacks(self):
if self._restricted:
self._callbacks = OrderedDict.fromkeys(self._callbacks.keys(), [])
else:
self._callbacks = OrderedDict()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def execute_callback(self, reference, index=-1):
self._callbacks[reference][index](self._states)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def execute_callbacks(self, reference):
states = self._states
for callback in self._callbacks[reference]:
callback(states)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def execute_all_callbacks(self):
states = self._states
for callbacks in self._callbacks.values():
for callback in callbacks:
callback(states)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def get_state(self, state):
return self._states[state]
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def set_states(self, **states):
self._states.update(states)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def del_states(self, *states):
_states = self._states
for state in states:
_states.pop(state, None)