document return codes
[enigma2.git] / mytest.py
1 from Tools import RedirectOutput
2 from enigma import *
3 from tools import *
4
5 from Components.Language import language
6
7 import traceback
8 import Screens.InfoBar
9
10 import sys
11 import time
12
13 import ServiceReference
14
15 from Navigation import Navigation
16
17 from skin import readSkin, applyAllAttributes
18
19 from Components.config import configfile
20 from Tools.Directories import InitFallbackFiles
21 InitFallbackFiles()
22 eDVBDB.getInstance().reloadBouquets()
23
24 try:
25         import e2reactor
26         e2reactor.install()
27         
28         from twisted.internet import reactor
29         
30         def runReactor():
31                 reactor.run()
32 except:
33         print "twisted not available"
34         def runReactor():
35                 runMainloop()
36
37 # initialize autorun plugins and plugin menu entries
38 from Components.PluginComponent import plugins
39 plugins.readPluginList()
40
41 from Screens.Wizard import wizardManager
42 from Screens.StartWizard import *
43 from Screens.TutorialWizard import *
44 from Tools.BoundFunction import boundFunction
45
46 had = dict()
47
48 def dump(dir, p = ""):
49         if isinstance(dir, dict):
50                 for (entry, val) in dir.items():
51                         dump(val, p + "(dict)/" + entry)
52         if hasattr(dir, "__dict__"):
53                 for name, value in dir.__dict__.items():
54                         if not had.has_key(str(value)):
55                                 had[str(value)] = 1
56                                 dump(value, p + "/" + str(name))
57                         else:
58                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
59         else:
60                 print p + ":" + str(dir)
61
62 # + ":" + str(dir.__class__)
63
64 # display
65
66 class OutputDevice:
67         def create(self, screen): pass
68
69 # display: HTML
70
71 class HTMLOutputDevice(OutputDevice):
72         def create(self, comp):
73                 print comp.produceHTML()
74
75 html = HTMLOutputDevice()
76
77 class GUIOutputDevice(OutputDevice):
78         parent = None
79         def create(self, comp, desktop):
80                 comp.createGUIScreen(self.parent, desktop)
81
82 class Session:
83         def __init__(self):
84                 self.desktop = None
85                 self.delayTimer = eTimer()
86                 self.delayTimer.timeout.get().append(self.processDelay)
87                 
88                 self.currentDialog = None
89                 
90                 self.dialogStack = [ ]
91         
92         def processDelay(self):
93                 self.execEnd()
94                 
95                 callback = self.currentDialog.callback
96
97                 retval = self.currentDialog.returnValue
98
99                 if self.currentDialog.isTmp:
100                         self.currentDialog.doClose()
101 #                       dump(self.currentDialog)
102                         del self.currentDialog
103                 else:
104                         del self.currentDialog.callback
105                 
106                 self.popCurrent()
107                 if callback is not None:
108                         callback(*retval)
109
110         def execBegin(self):
111                 c = self.currentDialog
112                 c.execBegin()
113
114                 # when execBegin opened a new dialog, don't bother showing the old one.
115                 if c == self.currentDialog:
116                         c.show()
117                 
118         def execEnd(self):
119                 self.currentDialog.execEnd()
120                 self.currentDialog.hide()
121         
122         def create(self, screen, arguments):
123                 # creates an instance of 'screen' (which is a class)
124                 try:
125                         return screen(self, *arguments)
126                 except:
127                         errstr = "Screen %s(%s): %s" % (str(screen), str(arguments), sys.exc_info()[0])
128                         print errstr
129                         traceback.print_exc(file=sys.stdout)
130                         quitMainloop(5)
131                         
132         
133         def instantiateDialog(self, screen, *arguments):
134                 # create dialog
135                 
136                 try:
137                         dlg = self.create(screen, arguments)
138                 except:
139                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
140                         print '-'*60
141                         traceback.print_exc(file=sys.stdout)
142                         quitMainloop(5)
143                         print '-'*60
144                 
145                 if dlg is None:
146                         return
147
148                 # read skin data
149                 readSkin(dlg, None, dlg.skinName, self.desktop)
150
151                 # create GUI view of this dialog
152                 assert self.desktop != None
153                 
154                 z = 0
155                 for (key, value) in dlg.skinAttributes:
156                         if key == "zPosition":
157                                 z = int(value)
158
159                 dlg.instance = eWindow(self.desktop, z)
160                 applyAllAttributes(dlg.instance, self.desktop, dlg.skinAttributes)
161                 gui = GUIOutputDevice()
162                 gui.parent = dlg.instance
163                 gui.create(dlg, self.desktop)
164                 
165                 return dlg
166          
167         def pushCurrent(self):
168                 if self.currentDialog:
169                         self.dialogStack.append(self.currentDialog)
170                         self.execEnd()
171         
172         def popCurrent(self):
173                 if len(self.dialogStack):
174                         self.currentDialog = self.dialogStack.pop()
175                         self.execBegin()
176                 else:
177                         self.currentDialog = None
178
179         def execDialog(self, dialog):
180                 self.pushCurrent()
181                 self.currentDialog = dialog
182                 self.currentDialog.isTmp = False
183                 self.currentDialog.callback = None # would cause re-entrancy problems.
184                 self.execBegin()
185
186         def openWithCallback(self, callback, screen, *arguments):
187                 dlg = self.open(screen, *arguments)
188                 dlg.callback = callback
189
190         def open(self, screen, *arguments):
191                 self.pushCurrent()
192                 dlg = self.currentDialog = self.instantiateDialog(screen, *arguments)
193                 dlg.isTmp = True
194                 dlg.callback = None
195                 self.execBegin()
196                 return dlg
197
198         def keyEvent(self, code):
199                 print "code " + str(code)
200
201         def close(self, *retval):
202                 self.currentDialog.returnValue = retval
203                 self.delayTimer.start(0, 1)
204
205 from Screens.Volume import Volume
206 from Screens.Mute import Mute
207 from GlobalActions import globalActionMap
208 from Components.config import ConfigSubsection, configSequence, configElement, configsequencearg
209
210 #TODO .. move this to a own .py file
211 class VolumeControl:
212         """Volume control, handles volUp, volDown, volMute actions and display
213         a corresponding dialog"""
214         def __init__(self, session):
215                 global globalActionMap
216                 globalActionMap.actions["volumeUp"]=self.volUp
217                 globalActionMap.actions["volumeDown"]=self.volDown
218                 globalActionMap.actions["volumeMute"]=self.volMute
219
220                 config.audio = ConfigSubsection()
221                 config.audio.volume = configElement("config.audio.volume", configSequence, [100], configsequencearg.get("INTEGER", (0, 100)))
222
223                 self.volumeDialog = session.instantiateDialog(Volume)
224                 self.muteDialog = session.instantiateDialog(Mute)
225
226                 self.hideVolTimer = eTimer()
227                 self.hideVolTimer.timeout.get().append(self.volHide)
228
229                 vol = config.audio.volume.value[0]
230                 self.volumeDialog.setValue(vol)
231                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
232
233         def volSave(self):
234                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
235                 config.audio.volume.save()
236
237         def     volUp(self):
238                 if (eDVBVolumecontrol.getInstance().isMuted()):
239                         self.volMute()
240                 eDVBVolumecontrol.getInstance().volumeUp()
241                 self.volumeDialog.show()
242                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
243                 self.volSave()
244                 self.hideVolTimer.start(3000, True)
245
246         def     volDown(self):
247                 if (eDVBVolumecontrol.getInstance().isMuted()):
248                         self.volMute()
249                 eDVBVolumecontrol.getInstance().volumeDown()
250                 self.volumeDialog.show()
251                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
252                 self.volSave()
253                 self.hideVolTimer.start(3000, True)
254
255         def volHide(self):
256                 self.volumeDialog.hide()
257
258         def     volMute(self):
259                 eDVBVolumecontrol.getInstance().volumeToggleMute()
260                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
261
262                 if (eDVBVolumecontrol.getInstance().isMuted()):
263                         self.muteDialog.show()
264                 else:
265                         self.muteDialog.hide()
266
267 def runScreenTest():
268         session = Session()
269         session.desktop = getDesktop()
270         
271         session.nav = Navigation()
272         
273         screensToRun = wizardManager.getWizards()
274         screensToRun.append(Screens.InfoBar.InfoBar)
275
276         def runNextScreen(session, screensToRun, *result):
277                 if result:
278                         quitMainloop(result)
279         
280                 screen = screensToRun[0]
281                 
282                 if len(screensToRun):
283                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
284                 else:
285                         session.open(screen)
286         
287         runNextScreen(session, screensToRun)
288         
289         CONNECT(keyPressedSignal(), session.keyEvent)
290         
291         vol = VolumeControl(session)
292         
293         runReactor()
294         
295         configfile.save()
296         
297         session.nav.shutdown()
298         
299         return 0
300
301 import keymapparser
302 keymapparser.readKeymap()
303 import skin
304 skin.loadSkin(getDesktop())
305
306 import Components.InputDevice
307 Components.InputDevice.InitInputDevices()
308
309 import Components.AVSwitch
310 Components.AVSwitch.InitAVSwitch()
311
312 import Components.RecordingConfig
313 Components.RecordingConfig.InitRecordingConfig()
314
315 import Components.UsageConfig
316 Components.UsageConfig.InitUsageConfig()
317
318 import Components.Network
319 Components.Network.InitNetwork()
320
321 import Components.Lcd
322 Components.Lcd.InitLcd()
323
324 import Components.SetupDevices
325 Components.SetupDevices.InitSetupDevices()
326
327 import Components.RFmod
328 Components.RFmod.InitRFmod()
329
330 import Components.NimManager
331
332 # first, setup a screen
333 try:
334         runScreenTest()
335         plugins.shutdown()
336 except:
337         print 'EXCEPTION IN PYTHON STARTUP CODE:'
338         print '-'*60
339         traceback.print_exc(file=sys.stdout)
340         quitMainloop(5)
341         print '-'*60