9d2998ba47440387945da48ac56d7ad69ed6e504
[openblackhole/openblackhole-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 ImportError:
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 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):
85                 self.desktop = None
86                 self.delayTimer = eTimer()
87                 self.delayTimer.timeout.get().append(self.processDelay)
88                 
89                 self.currentDialog = None
90                 
91                 self.dialogStack = [ ]
92         
93         def processDelay(self):
94                 self.execEnd()
95                 
96                 callback = self.currentDialog.callback
97
98                 retval = self.currentDialog.returnValue
99
100                 if self.currentDialog.isTmp:
101                         self.currentDialog.doClose()
102 #                       dump(self.currentDialog)
103                         del self.currentDialog
104                 else:
105                         del self.currentDialog.callback
106                 
107                 self.popCurrent()
108                 if callback is not None:
109                         callback(*retval)
110
111         def execBegin(self):
112                 c = self.currentDialog
113                 c.execBegin()
114
115                 # when execBegin opened a new dialog, don't bother showing the old one.
116                 if c == self.currentDialog:
117                         c.show()
118                 
119         def execEnd(self):
120                 self.currentDialog.execEnd()
121                 self.currentDialog.hide()
122         
123         def create(self, screen, arguments):
124                 # creates an instance of 'screen' (which is a class)
125                 try:
126                         return screen(self, *arguments)
127                 except:
128                         errstr = "Screen %s(%s): %s" % (str(screen), str(arguments), sys.exc_info()[0])
129                         print errstr
130                         traceback.print_exc(file=sys.stdout)
131                         quitMainloop(5)
132                         
133         
134         def instantiateDialog(self, screen, *arguments):
135                 # create dialog
136                 
137                 try:
138                         dlg = self.create(screen, arguments)
139                 except:
140                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
141                         print '-'*60
142                         traceback.print_exc(file=sys.stdout)
143                         quitMainloop(5)
144                         print '-'*60
145                 
146                 if dlg is None:
147                         return
148
149                 # read skin data
150                 readSkin(dlg, None, dlg.skinName, self.desktop)
151
152                 # create GUI view of this dialog
153                 assert self.desktop != None
154                 
155                 z = 0
156                 for (key, value) in dlg.skinAttributes:
157                         if key == "zPosition":
158                                 z = int(value)
159
160                 dlg.instance = eWindow(self.desktop, z)
161                 applyAllAttributes(dlg.instance, self.desktop, dlg.skinAttributes)
162                 gui = GUIOutputDevice()
163                 gui.parent = dlg.instance
164                 gui.create(dlg, self.desktop)
165                 
166                 return dlg
167          
168         def pushCurrent(self):
169                 if self.currentDialog:
170                         self.dialogStack.append(self.currentDialog)
171                         self.execEnd()
172         
173         def popCurrent(self):
174                 if len(self.dialogStack):
175                         self.currentDialog = self.dialogStack.pop()
176                         self.execBegin()
177                 else:
178                         self.currentDialog = None
179
180         def execDialog(self, dialog):
181                 self.pushCurrent()
182                 self.currentDialog = dialog
183                 self.currentDialog.isTmp = False
184                 self.currentDialog.callback = None # would cause re-entrancy problems.
185                 self.execBegin()
186
187         def openWithCallback(self, callback, screen, *arguments):
188                 dlg = self.open(screen, *arguments)
189                 dlg.callback = callback
190
191         def open(self, screen, *arguments):
192                 self.pushCurrent()
193                 dlg = self.currentDialog = self.instantiateDialog(screen, *arguments)
194                 dlg.isTmp = True
195                 dlg.callback = None
196                 self.execBegin()
197                 return dlg
198
199         def keyEvent(self, code):
200                 print "code " + str(code)
201
202         def close(self, *retval):
203                 self.currentDialog.returnValue = retval
204                 self.delayTimer.start(0, 1)
205
206 from Screens.Volume import Volume
207 from Screens.Mute import Mute
208 from GlobalActions import globalActionMap
209 from Components.config import ConfigSubsection, configSequence, configElement, configsequencearg
210
211 #TODO .. move this to a own .py file
212 class VolumeControl:
213         """Volume control, handles volUp, volDown, volMute actions and display
214         a corresponding dialog"""
215         def __init__(self, session):
216                 global globalActionMap
217                 globalActionMap.actions["volumeUp"]=self.volUp
218                 globalActionMap.actions["volumeDown"]=self.volDown
219                 globalActionMap.actions["volumeMute"]=self.volMute
220
221                 config.audio = ConfigSubsection()
222                 config.audio.volume = configElement("config.audio.volume", configSequence, [100], configsequencearg.get("INTEGER", (0, 100)))
223
224                 self.volumeDialog = session.instantiateDialog(Volume)
225                 self.muteDialog = session.instantiateDialog(Mute)
226
227                 self.hideVolTimer = eTimer()
228                 self.hideVolTimer.timeout.get().append(self.volHide)
229
230                 vol = config.audio.volume.value[0]
231                 self.volumeDialog.setValue(vol)
232                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
233
234         def volSave(self):
235                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
236                 config.audio.volume.save()
237
238         def     volUp(self):
239                 if (eDVBVolumecontrol.getInstance().isMuted()):
240                         self.volMute()
241                 eDVBVolumecontrol.getInstance().volumeUp()
242                 self.volumeDialog.show()
243                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
244                 self.volSave()
245                 self.hideVolTimer.start(3000, True)
246
247         def     volDown(self):
248                 if (eDVBVolumecontrol.getInstance().isMuted()):
249                         self.volMute()
250                 eDVBVolumecontrol.getInstance().volumeDown()
251                 self.volumeDialog.show()
252                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
253                 self.volSave()
254                 self.hideVolTimer.start(3000, True)
255
256         def volHide(self):
257                 self.volumeDialog.hide()
258
259         def     volMute(self):
260                 eDVBVolumecontrol.getInstance().volumeToggleMute()
261                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
262
263                 if (eDVBVolumecontrol.getInstance().isMuted()):
264                         self.muteDialog.show()
265                 else:
266                         self.muteDialog.hide()
267
268 def runScreenTest():
269         session = Session()
270         session.desktop = getDesktop()
271         
272         session.nav = Navigation()
273         
274         screensToRun = wizardManager.getWizards()
275         for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD):
276                 screensToRun.append(p.__call__)
277         screensToRun.append(Screens.InfoBar.InfoBar)
278
279         def runNextScreen(session, screensToRun, *result):
280                 if result:
281                         quitMainloop(*result)
282         
283                 screen = screensToRun[0]
284                 
285                 if len(screensToRun):
286                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
287                 else:
288                         session.open(screen)
289         
290         runNextScreen(session, screensToRun)
291         
292         CONNECT(keyPressedSignal(), session.keyEvent)
293         
294         vol = VolumeControl(session)
295         
296         runReactor()
297         
298         configfile.save()
299         
300         session.nav.shutdown()
301         
302         return 0
303
304 import keymapparser
305 keymapparser.readKeymap()
306 import skin
307 skin.loadSkin(getDesktop())
308
309 import Components.InputDevice
310 Components.InputDevice.InitInputDevices()
311
312 import Components.AVSwitch
313 Components.AVSwitch.InitAVSwitch()
314
315 import Components.RecordingConfig
316 Components.RecordingConfig.InitRecordingConfig()
317
318 import Components.UsageConfig
319 Components.UsageConfig.InitUsageConfig()
320
321 import Components.Network
322 Components.Network.InitNetwork()
323
324 import Components.Lcd
325 Components.Lcd.InitLcd()
326
327 import Components.SetupDevices
328 Components.SetupDevices.InitSetupDevices()
329
330 import Components.RFmod
331 Components.RFmod.InitRFmod()
332
333 import Components.NimManager
334
335 # first, setup a screen
336 try:
337         runScreenTest()
338         plugins.shutdown()
339 except:
340         print 'EXCEPTION IN PYTHON STARTUP CODE:'
341         print '-'*60
342         traceback.print_exc(file=sys.stdout)
343         quitMainloop(5)
344         print '-'*60