WirelessLan: small skinfix for WLAN Status Screen. refs #725
[enigma2.git] / mytest.py
1 import eConsoleImpl
2 import eBaseImpl
3 import enigma
4 enigma.eTimer = eBaseImpl.eTimer
5 enigma.eSocketNotifier = eBaseImpl.eSocketNotifier
6 enigma.eConsoleAppContainer = eConsoleImpl.eConsoleAppContainer
7
8 from Tools.Profile import profile, profile_final
9
10 profile("PYTHON_START")
11
12 from enigma import runMainloop, eDVBDB, eTimer, quitMainloop, \
13         getDesktop, ePythonConfigQuery, eAVSwitch, eServiceEvent, \
14         eEPGCache
15 from tools import *
16
17 profile("LANGUAGE")
18
19 from Components.Language import language
20
21 def setEPGLanguage():
22         print "language set to", language.getLanguage()
23         eServiceEvent.setEPGLanguage(language.getLanguage())
24
25 language.addCallback(setEPGLanguage)
26
27 from traceback import print_exc
28 profile("LOAD:InfoBar")
29 import Screens.InfoBar
30 from Screens.SimpleSummary import SimpleSummary
31
32 from sys import stdout, exc_info
33
34 profile("Bouquets")
35 eDVBDB.getInstance().reloadBouquets()
36
37 profile("ParentalControl")
38 from Components.ParentalControl import InitParentalControl
39 InitParentalControl()
40
41 profile("LOAD:Navigation")
42 from Navigation import Navigation
43
44 profile("LOAD:skin")
45 from skin import readSkin
46
47 profile("LOAD:Tools")
48 from Tools.Directories import InitFallbackFiles, resolveFilename, SCOPE_PLUGINS, SCOPE_CURRENT_SKIN
49 from Components.config import config, configfile, ConfigText, ConfigYesNo, ConfigInteger, NoSave
50 InitFallbackFiles()
51
52 profile("config.misc")
53
54 config.misc.radiopic = ConfigText(default = resolveFilename(SCOPE_CURRENT_SKIN, "radio.mvi"))
55 config.misc.isNextRecordTimerAfterEventActionAuto = ConfigYesNo(default=False)
56 config.misc.useTransponderTime = ConfigYesNo(default=True)
57 config.misc.startCounter = ConfigInteger(default=0) # number of e2 starts...
58 config.misc.standbyCounter = NoSave(ConfigInteger(default=0)) # number of standby
59 config.misc.epgcache_filename = ConfigText(default = "/hdd/epg.dat")
60
61 def setEPGCachePath(configElement):
62         eEPGCache.getInstance().setCacheFile(configElement.value)
63
64
65 #demo code for use of standby enter leave callbacks
66 #def leaveStandby():
67 #       print "!!!!!!!!!!!!!!!!!leave standby"
68
69 #def standbyCountChanged(configElement):
70 #       print "!!!!!!!!!!!!!!!!!enter standby num", configElement.value
71 #       from Screens.Standby import inStandby
72 #       inStandby.onClose.append(leaveStandby)
73
74 #config.misc.standbyCounter.addNotifier(standbyCountChanged, initial_call = False)
75 ####################################################
76
77 def useTransponderTimeChanged(configElement):
78         enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(configElement.value)
79 config.misc.useTransponderTime.addNotifier(useTransponderTimeChanged)
80
81 profile("Twisted")
82 try:
83         import twisted.python.runtime
84         twisted.python.runtime.platform.supportsThreads = lambda: False
85
86         import e2reactor
87         e2reactor.install()
88
89         from twisted.internet import reactor
90
91         def runReactor():
92                 reactor.run(installSignalHandlers=False)
93 except ImportError:
94         print "twisted not available"
95         def runReactor():
96                 runMainloop()
97
98 profile("LOAD:Plugin")
99
100 # initialize autorun plugins and plugin menu entries
101 from Components.PluginComponent import plugins
102
103 profile("LOAD:Wizard")
104 from Screens.Wizard import wizardManager
105 from Screens.DefaultWizard import *
106 from Screens.StartWizard import *
107 from Screens.TutorialWizard import *
108 import Screens.Rc
109 from Tools.BoundFunction import boundFunction
110 from Plugins.Plugin import PluginDescriptor
111
112 profile("misc")
113 had = dict()
114
115 def dump(dir, p = ""):
116         if isinstance(dir, dict):
117                 for (entry, val) in dir.items():
118                         dump(val, p + "(dict)/" + entry)
119         if hasattr(dir, "__dict__"):
120                 for name, value in dir.__dict__.items():
121                         if not had.has_key(str(value)):
122                                 had[str(value)] = 1
123                                 dump(value, p + "/" + str(name))
124                         else:
125                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
126         else:
127                 print p + ":" + str(dir)
128
129 # + ":" + str(dir.__class__)
130
131 # display
132
133 profile("LOAD:ScreenGlobals")
134 from Screens.Globals import Globals
135 from Screens.SessionGlobals import SessionGlobals
136 from Screens.Screen import Screen
137
138 profile("Screen")
139 Screen.global_screen = Globals()
140
141 # Session.open:
142 # * push current active dialog ('current_dialog') onto stack
143 # * call execEnd for this dialog
144 #   * clear in_exec flag
145 #   * hide screen
146 # * instantiate new dialog into 'current_dialog'
147 #   * create screens, components
148 #   * read, apply skin
149 #   * create GUI for screen
150 # * call execBegin for new dialog
151 #   * set in_exec
152 #   * show gui screen
153 #   * call components' / screen's onExecBegin
154 # ... screen is active, until it calls 'close'...
155 # Session.close:
156 # * assert in_exec
157 # * save return value
158 # * start deferred close handler ('onClose')
159 # * execEnd
160 #   * clear in_exec
161 #   * hide screen
162 # .. a moment later:
163 # Session.doClose:
164 # * destroy screen
165
166 class Session:
167         def __init__(self, desktop = None, summary_desktop = None, navigation = None):
168                 self.desktop = desktop
169                 self.summary_desktop = summary_desktop
170                 self.nav = navigation
171                 self.delay_timer = eTimer()
172                 self.delay_timer.callback.append(self.processDelay)
173
174                 self.current_dialog = None
175
176                 self.dialog_stack = [ ]
177                 self.summary_stack = [ ]
178                 self.summary = None
179
180                 self.in_exec = False
181
182                 self.screen = SessionGlobals(self)
183
184                 for p in plugins.getPlugins(PluginDescriptor.WHERE_SESSIONSTART):
185                         p(reason=0, session=self)
186
187         def processDelay(self):
188                 callback = self.current_dialog.callback
189
190                 retval = self.current_dialog.returnValue
191
192                 if self.current_dialog.isTmp:
193                         self.current_dialog.doClose()
194 #                       dump(self.current_dialog)
195                         del self.current_dialog
196                 else:
197                         del self.current_dialog.callback
198
199                 self.popCurrent()
200                 if callback is not None:
201                         callback(*retval)
202
203         def execBegin(self, first=True, do_show = True):
204                 assert not self.in_exec 
205                 self.in_exec = True
206                 c = self.current_dialog
207
208                 # when this is an execbegin after a execend of a "higher" dialog,
209                 # popSummary already did the right thing.
210                 if first:
211                         self.pushSummary()
212                         summary = c.createSummary() or SimpleSummary
213                         self.summary = self.instantiateSummaryDialog(summary, c)
214                         self.summary.show()
215                         c.addSummary(self.summary)
216
217                 c.saveKeyboardMode()
218                 c.execBegin()
219
220                 # when execBegin opened a new dialog, don't bother showing the old one.
221                 if c == self.current_dialog and do_show:
222                         c.show()
223
224         def execEnd(self, last=True):
225                 assert self.in_exec
226                 self.in_exec = False
227
228                 self.current_dialog.execEnd()
229                 self.current_dialog.restoreKeyboardMode()
230                 self.current_dialog.hide()
231
232                 if last:
233                         self.current_dialog.removeSummary(self.summary)
234                         self.popSummary()
235
236         def create(self, screen, arguments, **kwargs):
237                 # creates an instance of 'screen' (which is a class)
238                 try:
239                         return screen(self, *arguments, **kwargs)
240                 except:
241                         errstr = "Screen %s(%s, %s): %s" % (str(screen), str(arguments), str(kwargs), exc_info()[0])
242                         print errstr
243                         print_exc(file=stdout)
244                         quitMainloop(5)
245
246         def instantiateDialog(self, screen, *arguments, **kwargs):
247                 return self.doInstantiateDialog(screen, arguments, kwargs, self.desktop)
248
249         def deleteDialog(self, screen):
250                 screen.hide()
251                 screen.doClose()
252
253         def instantiateSummaryDialog(self, screen, *arguments, **kwargs):
254                 return self.doInstantiateDialog(screen, arguments, kwargs, self.summary_desktop)
255
256         def doInstantiateDialog(self, screen, arguments, kwargs, desktop):
257                 # create dialog
258
259                 try:
260                         dlg = self.create(screen, arguments, **kwargs)
261                 except:
262                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
263                         print '-'*60
264                         print_exc(file=stdout)
265                         quitMainloop(5)
266                         print '-'*60
267
268                 if dlg is None:
269                         return
270
271                 # read skin data
272                 readSkin(dlg, None, dlg.skinName, desktop)
273
274                 # create GUI view of this dialog
275                 assert desktop is not None
276
277                 dlg.setDesktop(desktop)
278                 dlg.applySkin()
279
280                 return dlg
281
282         def pushCurrent(self):
283                 if self.current_dialog is not None:
284                         self.dialog_stack.append((self.current_dialog, self.current_dialog.shown))
285                         self.execEnd(last=False)
286
287         def popCurrent(self):
288                 if self.dialog_stack:
289                         (self.current_dialog, do_show) = self.dialog_stack.pop()
290                         self.execBegin(first=False, do_show=do_show)
291                 else:
292                         self.current_dialog = None
293
294         def execDialog(self, dialog):
295                 self.pushCurrent()
296                 self.current_dialog = dialog
297                 self.current_dialog.isTmp = False
298                 self.current_dialog.callback = None # would cause re-entrancy problems.
299                 self.execBegin()
300
301         def openWithCallback(self, callback, screen, *arguments, **kwargs):
302                 dlg = self.open(screen, *arguments, **kwargs)
303                 dlg.callback = callback
304                 return dlg
305
306         def open(self, screen, *arguments, **kwargs):
307                 if self.dialog_stack and not self.in_exec:
308                         raise RuntimeError("modal open are allowed only from a screen which is modal!")
309                         # ...unless it's the very first screen.
310
311                 self.pushCurrent()
312                 dlg = self.current_dialog = self.instantiateDialog(screen, *arguments, **kwargs)
313                 dlg.isTmp = True
314                 dlg.callback = None
315                 self.execBegin()
316                 return dlg
317
318         def close(self, screen, *retval):
319                 if not self.in_exec:
320                         print "close after exec!"
321                         return
322
323                 # be sure that the close is for the right dialog!
324                 # if it's not, you probably closed after another dialog
325                 # was opened. this can happen if you open a dialog
326                 # onExecBegin, and forget to do this only once.
327                 # after close of the top dialog, the underlying will
328                 # gain focus again (for a short time), thus triggering
329                 # the onExec, which opens the dialog again, closing the loop.
330                 assert screen == self.current_dialog
331
332                 self.current_dialog.returnValue = retval
333                 self.delay_timer.start(0, 1)
334                 self.execEnd()
335
336         def pushSummary(self):
337                 if self.summary is not None:
338                         self.summary.hide()
339                 self.summary_stack.append(self.summary)
340                 self.summary = None
341
342         def popSummary(self):
343                 if self.summary is not None:
344                         self.summary.doClose()
345                 self.summary = self.summary_stack.pop()
346                 if self.summary is not None:
347                         self.summary.show()
348
349 profile("Standby,PowerKey")
350 import Screens.Standby
351 from Screens.Menu import MainMenu, mdom
352 from GlobalActions import globalActionMap
353
354 class PowerKey:
355         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
356
357         def __init__(self, session):
358                 self.session = session
359                 globalActionMap.actions["power_down"]=self.powerdown
360                 globalActionMap.actions["power_up"]=self.powerup
361                 globalActionMap.actions["power_long"]=self.powerlong
362                 globalActionMap.actions["deepstandby"]=self.shutdown # frontpanel long power button press
363                 self.standbyblocked = 1
364
365         def MenuClosed(self, *val):
366                 self.session.infobar = None
367
368         def shutdown(self):
369                 print "PowerOff - Now!"
370                 if not Screens.Standby.inTryQuitMainloop and self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND:
371                         self.session.open(Screens.Standby.TryQuitMainloop, 1)
372
373         def powerlong(self):
374                 if Screens.Standby.inTryQuitMainloop or (self.session.current_dialog and not self.session.current_dialog.ALLOW_SUSPEND):
375                         return
376                 self.doAction(action = config.usage.on_long_powerpress.value)
377
378         def doAction(self, action):
379                 self.standbyblocked = 1
380                 if action == "shutdown":
381                         self.shutdown()
382                 elif action == "show_menu":
383                         print "Show shutdown Menu"
384                         root = mdom.getroot()
385                         for x in root.findall("menu"):
386                                 y = x.find("id")
387                                 if y is not None:
388                                         id = y.get("val")
389                                         if id and id == "shutdown":
390                                                 self.session.infobar = self
391                                                 menu_screen = self.session.openWithCallback(self.MenuClosed, MainMenu, x)
392                                                 menu_screen.setTitle(_("Standby / Restart"))
393                                                 return
394                 elif action == "standby":
395                         self.standby()
396
397         def powerdown(self):
398                 self.standbyblocked = 0
399
400         def powerup(self):
401                 if self.standbyblocked == 0:
402                         self.doAction(action = config.usage.on_short_powerpress.value)
403
404         def standby(self):
405                 if not Screens.Standby.inStandby and self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND and self.session.in_exec:
406                         self.session.open(Screens.Standby.Standby)
407
408 profile("Scart")
409 from Screens.Scart import Scart
410
411 class AutoScartControl:
412         def __init__(self, session):
413                 self.force = False
414                 self.current_vcr_sb = eAVSwitch.getInstance().getVCRSlowBlanking()
415                 if self.current_vcr_sb and config.av.vcrswitch.value:
416                         self.scartDialog = session.instantiateDialog(Scart, True)
417                 else:
418                         self.scartDialog = session.instantiateDialog(Scart, False)
419                 config.av.vcrswitch.addNotifier(self.recheckVCRSb)
420                 eAVSwitch.getInstance().vcr_sb_notifier.get().append(self.VCRSbChanged)
421
422         def recheckVCRSb(self, configElement):
423                 self.VCRSbChanged(self.current_vcr_sb)
424
425         def VCRSbChanged(self, value):
426                 #print "vcr sb changed to", value
427                 self.current_vcr_sb = value
428                 if config.av.vcrswitch.value or value > 2:
429                         if value:
430                                 self.scartDialog.showMessageBox()
431                         else:
432                                 self.scartDialog.switchToTV()
433
434 profile("Load:CI")
435 from enigma import eDVBCIInterfaces
436 from Screens.Ci import CiHandler
437
438 profile("Load:VolumeControl")
439 from Components.VolumeControl import VolumeControl
440
441 def runScreenTest():
442         config.misc.startCounter.value += 1
443
444         profile("readPluginList")
445         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
446
447         profile("Init:Session")
448         nav = Navigation(config.misc.isNextRecordTimerAfterEventActionAuto.value)
449         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = nav)
450
451         CiHandler.setSession(session)
452
453         screensToRun = [ p.__call__ for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD) ]
454
455         profile("wizards")
456         screensToRun += wizardManager.getWizards()
457
458         screensToRun.append((100, Screens.InfoBar.InfoBar))
459
460         screensToRun.sort()
461
462         ePythonConfigQuery.setQueryFunc(configfile.getResolvedKey)
463
464 #       eDVBCIInterfaces.getInstance().setDescrambleRules(0 # Slot Number
465 #               ,(      ["1:0:1:24:4:85:C00000:0:0:0:"], #service_list
466 #                       ["PREMIERE"], #provider_list,
467 #                       [] #caid_list
468 #               ));
469
470         def runNextScreen(session, screensToRun, *result):
471                 if result:
472                         quitMainloop(*result)
473                         return
474
475                 screen = screensToRun[0][1]
476                 args = screensToRun[0][2:]
477
478                 if screensToRun:
479                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen, *args)
480                 else:
481                         session.open(screen, *args)
482
483         config.misc.epgcache_filename.addNotifier(setEPGCachePath)
484
485         runNextScreen(session, screensToRun)
486
487         profile("Init:VolumeControl")
488         vol = VolumeControl(session)
489         profile("Init:PowerKey")
490         power = PowerKey(session)
491
492         # we need session.scart to access it from within menu.xml
493         session.scart = AutoScartControl(session)
494
495         profile("RunReactor")
496         profile_final()
497         runReactor()
498
499         config.misc.startCounter.save()
500
501         profile("wakeup")
502         from time import time, strftime, localtime
503         from Tools.DreamboxHardware import setFPWakeuptime, getFPWakeuptime, setRTCtime
504         #get currentTime
505         nowTime = time()
506         wakeupList = [
507                 x for x in ((session.nav.RecordTimer.getNextRecordingTime(), 0, session.nav.RecordTimer.isNextRecordAfterEventActionAuto()),
508                                         (session.nav.RecordTimer.getNextZapTime(), 1),
509                                         (plugins.getNextWakeupTime(), 2))
510                 if x[0] != -1
511         ]
512         wakeupList.sort()
513         recordTimerWakeupAuto = False
514         if wakeupList:
515                 from time import strftime
516                 startTime = wakeupList[0]
517                 if (startTime[0] - nowTime) < 270: # no time to switch box back on
518                         wptime = nowTime + 30  # so switch back on in 30 seconds
519                 else:
520                         wptime = startTime[0] - 240
521                 if not config.misc.useTransponderTime.value:
522                         print "dvb time sync disabled... so set RTC now to current linux time!", strftime("%Y/%m/%d %H:%M", localtime(nowTime))
523                         setRTCtime(nowTime)
524                 print "set wakeup time to", strftime("%Y/%m/%d %H:%M", localtime(wptime))
525                 setFPWakeuptime(wptime)
526                 recordTimerWakeupAuto = startTime[1] == 0 and startTime[2]
527         config.misc.isNextRecordTimerAfterEventActionAuto.value = recordTimerWakeupAuto
528         config.misc.isNextRecordTimerAfterEventActionAuto.save()
529
530         profile("stopService")
531         session.nav.stopService()
532         profile("nav shutdown")
533         session.nav.shutdown()
534
535         profile("configfile.save")
536         configfile.save()
537
538         return 0
539
540 profile("Init:skin")
541 import skin
542 skin.loadSkinData(getDesktop(0))
543
544 profile("InputDevice")
545 import Components.InputDevice
546 Components.InputDevice.InitInputDevices()
547
548 profile("AVSwitch")
549 import Components.AVSwitch
550 Components.AVSwitch.InitAVSwitch()
551
552 profile("RecordingConfig")
553 import Components.RecordingConfig
554 Components.RecordingConfig.InitRecordingConfig()
555
556 profile("UsageConfig")
557 import Components.UsageConfig
558 Components.UsageConfig.InitUsageConfig()
559
560 profile("keymapparser")
561 import keymapparser
562 keymapparser.readKeymap(config.usage.keymap.value)
563
564 profile("Network")
565 import Components.Network
566 Components.Network.InitNetwork()
567
568 profile("LCD")
569 import Components.Lcd
570 Components.Lcd.InitLcd()
571
572 profile("SetupDevices")
573 import Components.SetupDevices
574 Components.SetupDevices.InitSetupDevices()
575
576 profile("RFMod")
577 import Components.RFmod
578 Components.RFmod.InitRFmod()
579
580 profile("Init:CI")
581 import Screens.Ci
582 Screens.Ci.InitCiConfig()
583
584 #from enigma import dump_malloc_stats
585 #t = eTimer()
586 #t.callback.append(dump_malloc_stats)
587 #t.start(1000)
588
589 # first, setup a screen
590 try:
591         runScreenTest()
592
593         plugins.shutdown()
594
595         from Components.ParentalControl import parentalControl
596         parentalControl.save()
597 except:
598         print 'EXCEPTION IN PYTHON STARTUP CODE:'
599         print '-'*60
600         print_exc(file=stdout)
601         quitMainloop(5)
602         print '-'*60