some python import cleanups
[enigma2.git] / lib / python / Components / ActionMap.py
1 from enigma import eActionMap
2
3 class ActionMap:
4         def __init__(self, contexts = [ ], actions = { }, prio=0):
5                 self.actions = actions
6                 self.contexts = contexts
7                 self.prio = prio
8                 self.p = eActionMap.getInstance()
9                 self.bound = False
10                 self.exec_active = False
11                 self.enabled = True
12
13         def setEnabled(self, enabled):
14                 self.enabled = enabled
15                 self.checkBind()
16
17         def doBind(self):
18                 if not self.bound:
19                         for ctx in self.contexts:
20                                 self.p.bindAction(ctx, self.prio, self.action)
21                         self.bound = True
22
23         def doUnbind(self):
24                 if self.bound:
25                         for ctx in self.contexts:
26                                 self.p.unbindAction(ctx, self.action)
27                         self.bound = False
28
29         def checkBind(self):
30                 if self.exec_active and self.enabled:
31                         self.doBind()
32                 else:
33                         self.doUnbind()
34
35         def execBegin(self):
36                 self.exec_active = True
37                 self.checkBind()
38
39         def execEnd(self):
40                 self.exec_active = False
41                 self.checkBind()
42
43         def action(self, context, action):
44                 print " ".join(("action -> ", context, action))
45                 if self.actions.has_key(action):
46                         res = self.actions[action]()
47                         if res is not None:
48                                 return res
49                         return 1
50                 else:
51                         print "unknown action %s/%s! typo in keymap?" % (context, action)
52                         return 0
53
54         def destroy(self):
55                 pass
56
57 class NumberActionMap(ActionMap):
58         def action(self, contexts, action):
59                 numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
60                 if (action in numbers and self.actions.has_key(action)):
61                         res = self.actions[action](int(action))
62                         if res is not None:
63                                 return res
64                         return 1
65                 else:
66                         return ActionMap.action(self, contexts, action)
67
68 class HelpableActionMap(ActionMap):
69         """An Actionmap which automatically puts the actions into the helpList.
70
71         Note that you can only use ONE context here!"""
72         
73         # sorry for this complicated code.
74         # it's not more than converting a "documented" actionmap 
75         # (where the values are possibly (function, help)-tuples)
76         # into a "classic" actionmap, where values are just functions.
77         # the classic actionmap is then passed to the ActionMap constructor,
78         # the collected helpstrings (with correct context, action) is
79         # added to the screen's "helpList", which will be picked up by
80         # the "HelpableScreen".
81         def __init__(self, parent, context, actions = { }, prio=0):
82                 alist = [ ]
83                 adict = { }
84                 for (action, funchelp) in actions.iteritems():
85                         # check if this is a tuple
86                         if type(funchelp) is type(()):
87                                 alist.append((action, funchelp[1]))
88                                 adict[action] = funchelp[0]
89                         else:
90                                 adict[action] = funchelp
91
92                 ActionMap.__init__(self, [context], adict, prio)
93
94                 parent.helpList.append((self, context, alist))