allow using complex shell commandlines (including | etc.) for job tasks (handle with...
[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                 retval = self.appContainers[name].execute(cmd)
25                 if retval:
26                         self.finishedCB(name, retval)
27
28         def eBatch(self, cmds, callback, extra_args=[], debug=False):
29                 self.debug = debug
30                 cmd = cmds.pop(0)
31                 self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])
32
33         def eBatchCB(self, data, retval, _extra_args):
34                 (cmds, callback, extra_args) = _extra_args
35                 if self.debug:
36                         print '[eBatch] retval=%s, cmds left=%d, data:\n%s' % (retval, len(cmds), data)
37                 if len(cmds):
38                         cmd = cmds.pop(0)
39                         self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])
40                 else:
41                         callback(extra_args)
42
43         def dataAvailCB(self, name, data):
44                 self.appResults[name] += data
45
46         def finishedCB(self, name, retval):
47                 del self.appContainers[name].dataAvail[:]
48                 del self.appContainers[name].appClosed[:]
49                 data = self.appResults[name]
50                 extra_args = self.extra_args[name]
51                 del self.appContainers[name]
52                 del self.extra_args[name]
53                 if self.callbacks[name]:
54                         self.callbacks[name](data,retval,extra_args)
55                 del self.callbacks[name]