use twisted reactor when available
[vuplus_dvbapp] / mytest.py
1 from enigma import *
2 from tools import *
3
4 from Components.Language import language
5
6 import traceback
7 import Screens.InfoBar
8
9 import sys
10 import time
11
12 import ServiceReference
13
14 from Navigation import Navigation
15
16 from skin import readSkin, applyAllAttributes
17
18 from Components.config import configfile
19 from Tools.Directories import InitFallbackFiles
20 InitFallbackFiles()
21 eDVBDB.getInstance().reloadBouquets()
22
23 try:
24         from twisted.internet import e2reactor
25         e2reactor.install()
26         
27         from twisted.internet import reactor
28         
29         def runReactor():
30                 reactor.run()
31 except:
32         def runReactor():
33                 runMainloop()
34
35 # initialize autorun plugins and plugin menu entries
36 from Components.PluginComponent import plugins
37 plugins.getPluginList(runAutostartPlugins=True)
38 from Screens.Wizard import wizardManager
39 from Screens.StartWizard import *
40 from Screens.TutorialWizard import *
41 from Tools.BoundFunction import boundFunction
42
43 had = dict()
44
45 def dump(dir, p = ""):
46         if isinstance(dir, dict):
47                 for (entry, val) in dir.items():
48                         dump(val, p + "(dict)/" + entry)
49         if hasattr(dir, "__dict__"):
50                 for name, value in dir.__dict__.items():
51                         if not had.has_key(str(value)):
52                                 had[str(value)] = 1
53                                 dump(value, p + "/" + str(name))
54                         else:
55                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
56         else:
57                 print p + ":" + str(dir)
58
59 # + ":" + str(dir.__class__)
60
61 # display
62
63 class OutputDevice:
64         def create(self, screen): pass
65
66 # display: HTML
67
68 class HTMLOutputDevice(OutputDevice):
69         def create(self, comp):
70                 print comp.produceHTML()
71
72 html = HTMLOutputDevice()
73
74 class GUIOutputDevice(OutputDevice):
75         parent = None
76         def create(self, comp, desktop):
77                 comp.createGUIScreen(self.parent, desktop)
78
79 class Session:
80         def __init__(self):
81                 self.desktop = None
82                 self.delayTimer = eTimer()
83                 self.delayTimer.timeout.get().append(self.processDelay)
84                 
85                 self.currentDialog = None
86                 
87                 self.dialogStack = [ ]
88         
89         def processDelay(self):
90                 self.execEnd()
91                 
92                 callback = self.currentDialog.callback
93
94                 retval = self.currentDialog.returnValue
95
96                 if self.currentDialog.isTmp:
97                         self.currentDialog.doClose()
98 #                       dump(self.currentDialog)
99                         del self.currentDialog
100                 else:
101                         del self.currentDialog.callback
102                 
103                 self.popCurrent()
104                 if callback is not None:
105                         callback(*retval)
106
107         def execBegin(self):
108                 c = self.currentDialog
109                 c.execBegin()
110
111                 # when execBegin opened a new dialog, don't bother showing the old one.
112                 if c == self.currentDialog:
113                         c.instance.show()
114                 
115         def execEnd(self):
116                 self.currentDialog.execEnd()
117                 self.currentDialog.instance.hide()
118         
119         def create(self, screen, arguments):
120                 # creates an instance of 'screen' (which is a class)
121                 try:
122                         return screen(self, *arguments)
123                 except:
124                         errstr = "Screen %s(%s): %s" % (str(screen), str(arguments), sys.exc_info()[0])
125                         print errstr
126                         traceback.print_exc(file=sys.stdout)
127                         quitMainloop(5)
128                         
129         
130         def instantiateDialog(self, screen, *arguments):
131                 # create dialog
132                 
133                 try:
134                         dlg = self.create(screen, arguments)
135                 except:
136                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
137                         print '-'*60
138                         traceback.print_exc(file=sys.stdout)
139                         quitMainloop(5)
140                         print '-'*60
141                 
142                 if dlg is None:
143                         return
144
145                 # read skin data
146                 readSkin(dlg, None, dlg.skinName, self.desktop)
147
148                 # create GUI view of this dialog
149                 assert self.desktop != None
150                 dlg.instance = eWindow(self.desktop)
151                 applyAllAttributes(dlg.instance, self.desktop, dlg.skinAttributes)
152                 gui = GUIOutputDevice()
153                 gui.parent = dlg.instance
154                 gui.create(dlg, self.desktop)
155                 
156                 return dlg
157          
158         def pushCurrent(self):
159                 if self.currentDialog:
160                         self.dialogStack.append(self.currentDialog)
161                         self.execEnd()
162         
163         def popCurrent(self):
164                 if len(self.dialogStack):
165                         self.currentDialog = self.dialogStack.pop()
166                         self.execBegin()
167                 else:
168                         self.currentDialog = None
169
170         def execDialog(self, dialog):
171                 self.pushCurrent()
172                 self.currentDialog = dialog
173                 self.currentDialog.isTmp = False
174                 self.currentDialog.callback = None # would cause re-entrancy problems.
175                 self.execBegin()
176
177         def openWithCallback(self, callback, screen, *arguments):
178                 dlg = self.open(screen, *arguments)
179                 dlg.callback = callback
180
181         def open(self, screen, *arguments):
182                 self.pushCurrent()
183                 dlg = self.currentDialog = self.instantiateDialog(screen, *arguments)
184                 dlg.isTmp = True
185                 dlg.callback = None
186                 self.execBegin()
187                 return dlg
188
189         def keyEvent(self, code):
190                 print "code " + str(code)
191
192         def close(self, *retval):
193                 self.currentDialog.returnValue = retval
194                 self.delayTimer.start(0, 1)
195
196 def runScreenTest():
197         session = Session()
198         session.desktop = getDesktop()
199         
200         session.nav = Navigation()
201         
202         screensToRun = wizardManager.getWizards()
203         screensToRun.append(Screens.InfoBar.InfoBar)
204         
205         def runNextScreen(session, screensToRun, *result):
206                 if result:
207                         quitMainloop(result)
208
209                 screen = screensToRun[0]
210                 
211                 if len(screensToRun):
212                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
213                 else:
214                         session.open(screen)
215         
216         runNextScreen(session, screensToRun)
217
218         CONNECT(keyPressedSignal(), session.keyEvent)
219         
220         runReactor()
221         
222         configfile.save()
223         
224         session.nav.shutdown()
225         
226         return 0
227
228 import keymapparser
229 keymapparser.readKeymap()
230 import skin
231 skin.loadSkin(getDesktop())
232
233 import Components.InputDevice
234 Components.InputDevice.InitInputDevices()
235
236 import Components.AVSwitch
237 Components.AVSwitch.InitAVSwitch()
238
239 import Components.RecordingConfig
240 Components.RecordingConfig.InitRecordingConfig()
241
242 import Components.UsageConfig
243 Components.UsageConfig.InitUsageConfig()
244
245 import Components.Network
246 Components.Network.InitNetwork()
247
248 import Components.Lcd
249 Components.Lcd.InitLcd()
250
251 import Components.SetupDevices
252 Components.SetupDevices.InitSetupDevices()
253
254 import Components.RFmod
255 Components.RFmod.InitRFmod()
256
257 import Components.NimManager
258
259 # first, setup a screen
260 try:
261         runScreenTest()
262         plugins.getPluginList(runAutoendPlugins=True)
263 except:
264         print 'EXCEPTION IN PYTHON STARTUP CODE:'
265         print '-'*60
266         traceback.print_exc(file=sys.stdout)
267         quitMainloop(5)
268         print '-'*60