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