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