use 'in', add killAll
[enigma2.git] / lib / python / Components / Console.py
1 from enigma import eConsoleAppContainer
2 from Tools.BoundFunction import boundFunction
3
4 class Console(object):
5         def __init__(self):
6                 self.appContainers = {}
7                 self.appResults = {}
8                 self.callbacks = {}
9                 self.extra_args = {}
10
11         def ePopen(self, cmd, callback=None, extra_args=[]):
12                 name = cmd
13                 i = 0
14                 while self.appContainers.has_key(name):
15                         name = cmd +'_'+ str(i)
16                         i += 1
17                 print "[ePopen] command:", cmd
18                 self.appResults[name] = ""
19                 self.extra_args[name] = extra_args
20                 self.callbacks[name] = callback
21                 self.appContainers[name] = eConsoleAppContainer()
22                 self.appContainers[name].dataAvail.append(boundFunction(self.dataAvailCB,name))
23                 self.appContainers[name].appClosed.append(boundFunction(self.finishedCB,name))
24                 if isinstance(cmd, str): # until .execute supports a better api
25                         cmd = [cmd]
26                 retval = self.appContainers[name].execute(*cmd)
27                 if retval:
28                         self.finishedCB(name, retval)
29
30         def eBatch(self, cmds, callback, extra_args=[], debug=False):
31                 self.debug = debug
32                 cmd = cmds.pop(0)
33                 self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])
34
35         def eBatchCB(self, data, retval, _extra_args):
36                 (cmds, callback, extra_args) = _extra_args
37                 if self.debug:
38                         print '[eBatch] retval=%s, cmds left=%d, data:\n%s' % (retval, len(cmds), data)
39                 if len(cmds):
40                         cmd = cmds.pop(0)
41                         self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])
42                 else:
43                         callback(extra_args)
44
45         def dataAvailCB(self, name, data):
46                 self.appResults[name] += data
47
48         def finishedCB(self, name, retval):
49                 del self.appContainers[name].dataAvail[:]
50                 del self.appContainers[name].appClosed[:]
51                 data = self.appResults[name]
52                 extra_args = self.extra_args[name]
53                 del self.appContainers[name]
54                 del self.extra_args[name]
55                 if self.callbacks[name]:
56                         self.callbacks[name](data,retval,extra_args)
57                 del self.callbacks[name]
58
59         def kill(self,name):
60                 if name in self.appContainers:
61                         print "[Console] killing: ",self.appContainers[name]
62                         self.appContainers[name].kill()
63
64         def killAll(self):
65                 for name in self.appContainers:
66                         self.kill(name)