widget has no foregroundcolor
[enigma2.git] / lib / python / Components / Ipkg.py
1 from enigma import eConsoleAppContainer
2
3 class IpkgComponent:
4         EVENT_INSTALL = 0
5         EVENT_DOWNLOAD = 1
6         EVENT_INFLATING = 2
7         EVENT_CONFIGURING = 3
8         EVENT_REMOVE = 4
9         EVENT_UPGRADE = 5
10         EVENT_LISTITEM = 9
11         EVENT_DONE = 10
12         EVENT_ERROR = 11
13         EVENT_MODIFIED = 12
14         
15         CMD_INSTALL = 0
16         CMD_LIST = 1
17         CMD_REMOVE = 2
18         CMD_UPDATE = 3
19         CMD_UPGRADE = 4
20         
21         def __init__(self, ipkg = '/usr/bin/ipkg'):
22                 self.ipkg = ipkg
23                 
24                 self.cmd = eConsoleAppContainer()
25                 self.cmd.appClosed.get().append(self.cmdFinished)
26                 self.cmd.dataAvail.get().append(self.cmdData)
27                 self.cache = None
28                 
29                 self.callbackList = []
30                 self.setCurrentCommand()
31                 
32         def setCurrentCommand(self, command = None):
33                 self.currentCommand = command
34                 
35         def runCmd(self, cmd):
36                 print "executing", self.ipkg, cmd
37                 self.cmd.execute(self.ipkg + " " + cmd)
38                 
39         def startCmd(self, cmd, args = None):
40                 if cmd == self.CMD_UPDATE:
41                         self.runCmd("update")
42                 elif cmd == self.CMD_UPGRADE:
43                         append = ""
44                         if args["test_only"]:
45                                 append = " -test"
46                         self.runCmd("upgrade" + append)
47                 elif cmd == self.CMD_LIST:
48                         self.fetchedList = []
49                         if args['installed_only']:
50                                 self.runCmd("list_installed")
51                         else:
52                                 self.runCmd("list")
53                 elif cmd == self.CMD_INSTALL:
54                         self.runCmd("install " + args['package'])
55                 self.setCurrentCommand(cmd)
56         
57         def cmdFinished(self, retval):
58                 self.callCallbacks(self.EVENT_DONE)
59         
60         def cmdData(self, data):
61                 print "data:", data
62                 if self.cache is None:
63                         self.cache = data
64                 else:
65                         self.cache += data
66
67                 if '\n' in data:
68                         splitcache = self.cache.split('\n')
69                         if self.cache[-1] == '\n':
70                                 iteration = splitcache
71                                 self.cache = None
72                         else:
73                                 iteration = splitcache[:-1]
74                                 self.cache = splitcache[-1]
75                         for mydata in iteration:
76                                 if mydata != '':
77                                         self.parseLine(mydata)
78                 
79         def parseLine(self, data):
80                 if self.currentCommand == self.CMD_LIST:
81                         item = data.split(' - ', 2)
82                         self.fetchedList.append(item)
83                         self.callCallbacks(self.EVENT_LISTITEM, item)
84                 else:
85                         if data.find('Downloading') == 0:
86                                 self.callCallbacks(self.EVENT_DOWNLOAD, data.split(' ', 5)[1].strip())
87                         elif data.find('Upgrading') == 0:
88                                 self.callCallbacks(self.EVENT_UPGRADE, data.split('    ', 1)[1].split(' ')[0])
89                         elif data.find('Installing') == 0:
90                                 self.callCallbacks(self.EVENT_INSTALL, data.split(' ', 1)[1].split(' ')[0])
91                         elif data.find('Configuring') == 0:
92                                 self.callCallbacks(self.EVENT_CONFIGURING, data.split(' ', 1)[1].split(' ')[0])
93                         elif data.find('An error occurred') == 0:
94                                 self.callCallbacks(self.EVENT_ERROR, None)
95                         elif data.find('Failed to download') == 0:
96                                 self.callCallbacks(self.EVENT_ERROR, None)
97                         elif data.find('ipkg_download: ERROR:') == 0:
98                                 self.callCallbacks(self.EVENT_ERROR, None)
99                         elif data.find('    Configuration file') == 0:
100                                 self.callCallbacks(self.EVENT_MODIFIED, data.split(' \'', 1)[1][:-1])
101
102         def callCallbacks(self, event, param = None):
103                 for callback in self.callbackList:
104                         callback(event, param)
105
106         def addCallback(self, callback):
107                 self.callbackList.append(callback)
108                 
109         def getFetchedList(self):
110                 return self.fetchedList
111         
112         def stop(self):
113                 self.cmd.kill()
114                 
115         def isRunning(self):
116                 return self.cmd.running()
117
118         def write(self, what):
119                 if what:
120                         # We except unterminated commands
121                         what += "\n"
122                         self.cmd.write(what, len(what))