-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
69 lines (49 loc) · 1.48 KB
/
actions.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
# actions.py
#
# managing the actions
#
import messagequeue
#
actionMap = {} # map of (action, led:bool)-tuples
# list of all action classes available in config
allActions = []
# Action prefix, aka bank selection
_prefix = None
class ActionList:
def __init__(self, *args):
self.actions = args
def __repr__(self):
return "ActionList(%s)" % ", ".join(map(str,self.actions))
def run(self):
for a in self.actions:
a.run()
class SetPrefix:
def __init__(self, prefix):
self.prefix = prefix
def __repr__(self):
return "SetPrefix(%s)" % self.prefix
def run(self):
global _prefix
_prefix = self.prefix
def execute(rawActionKey):
actionKey = rawActionKey
if _prefix != None:
actionKey = "%s:%s" % (_prefix, rawActionKey)
if actionKey in actionMap.keys():
action, led = actionMap[actionKey]
print "Performing action", action
if led:
messagequeue.leds.send(actionKey)
action.run()
else:
print "No action registered for '%s'" % actionKey
def setAction(actionKey, action, led=False):
if actionKey in actionMap.keys():
print "Warning: action for '%s' registered more than once!" % actionKey
actionMap[actionKey] = (action, led)
def setLedAction(actionKey, action):
setAction(actionKey, action, True)
def registerActionClass(aClass):
allActions.append(aClass)
registerActionClass(ActionList)
registerActionClass(SetPrefix)