aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Components/Console.py
blob: c5fa5f9810456c3ec051e4146c3edeb3e3f46ca8 (plain)
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
from enigma import eConsoleAppContainer
from Tools.BoundFunction import boundFunction

class Console(object):
	def __init__(self):
		self.appContainers = {}
		self.appResults = {}
		self.callbacks = {}
		self.extra_args = {}

	def ePopen(self, cmd, callback, extra_args=[]):
		name = cmd
		i = 0
		while self.appContainers.has_key(name):
			name = cmd +'_'+ str(i)
			i += 1
		print "[ePopen] command:", cmd
		self.appResults[name] = ""
		self.extra_args[name] = extra_args
		self.callbacks[name] = callback
		self.appContainers[name] = eConsoleAppContainer()
		self.appContainers[name].dataAvail.append(boundFunction(self.dataAvailCB,name))
		self.appContainers[name].appClosed.append(boundFunction(self.finishedCB,name))
		retval = self.appContainers[name].execute(cmd)
		if retval:
			self.finishedCB(name, retval)

	def eBatch(self, cmds, callback, extra_args=[], debug=False):
		self.debug = debug
		cmd = cmds.pop(0)
		self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])

	def eBatchCB(self, data, retval, _extra_args):
		(cmds, callback, extra_args) = _extra_args
		if self.debug:
			print '[eBatch] retval=%s, cmds left=%d, data:\n%s' % (retval, len(cmds), data)
		if len(cmds):
			cmd = cmds.pop(0)
			self.ePopen(cmd, self.eBatchCB, [cmds, callback, extra_args])
		else:
			callback(extra_args)

	def dataAvailCB(self, name, data):
		self.appResults[name] += data

	def finishedCB(self, name, retval):
		del self.appContainers[name].dataAvail[:]
		del self.appContainers[name].appClosed[:]
		data = self.appResults[name]
		extra_args = self.extra_args[name]
		del self.appContainers[name]
		del self.extra_args[name]
		self.callbacks[name](data,retval,extra_args)
		del self.callbacks[name]