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