1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.config import config
7 from Components.Label import Label
8 from Components.Slider import Slider
9 from Components.ActionMap import NumberActionMap
10 from Components.MenuList import MenuList
11 from Components.ConfigList import ConfigList
13 from xml.sax import make_parser
14 from xml.sax.handler import ContentHandler
16 class Wizard(Screen, HelpableScreen):
18 class parseWizard(ContentHandler):
19 def __init__(self, wizard):
20 self.isPointsElement, self.isReboundsElement = 0, 0
25 def startElement(self, name, attrs):
26 print "startElement", name
27 self.currContent = name
30 if attrs.has_key('id'):
31 id = str(attrs.get('id'))
34 if attrs.has_key('nextstep'):
35 nextstep = str(attrs.get('nextstep'))
38 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
39 elif (name == "text"):
40 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
41 elif (name == "listentry"):
42 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
43 elif (name == "config"):
44 exec "from Screens." + str(attrs.get('module')) + " import *"
45 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
46 if (attrs.has_key('args')):
48 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
49 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
50 elif (name == "code"):
51 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
54 self.codeafter = False
55 elif (name == "condition"):
57 def endElement(self, name):
61 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
63 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
64 elif name == 'condition':
65 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
67 def characters(self, ch):
68 if self.currContent == "code":
70 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
72 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
73 elif self.currContent == "condition":
74 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
76 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
77 Screen.__init__(self, session)
78 HelpableScreen.__init__(self)
83 parser = make_parser()
84 print "Reading " + self.xmlfile
85 wizardHandler = self.parseWizard(self.wizard)
86 parser.setContentHandler(wizardHandler)
87 parser.parse('/usr/share/enigma2/' + self.xmlfile)
89 self.showSteps = showSteps
90 self.showStepSlider = showStepSlider
91 self.showList = showList
92 self.showConfig = showConfig
94 self.numSteps = len(self.wizard)
97 self["text"] = Label()
100 self["config"] = ConfigList([])
103 self["step"] = Label()
105 if self.showStepSlider:
106 self["stepslider"] = Slider(1, self.numSteps)
110 self["list"] = MenuList(self.list)
112 self.onShown.append(self.updateValues)
114 self.configInstance = None
116 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
124 "1": self.keyNumberGlobal,
125 "2": self.keyNumberGlobal,
126 "3": self.keyNumberGlobal,
127 "4": self.keyNumberGlobal,
128 "5": self.keyNumberGlobal,
129 "6": self.keyNumberGlobal,
130 "7": self.keyNumberGlobal,
131 "8": self.keyNumberGlobal,
132 "9": self.keyNumberGlobal,
133 "0": self.keyNumberGlobal
137 if len(self.stepHistory) > 1:
138 self.currStep = self.stepHistory[-2]
139 self.stepHistory = self.stepHistory[:-2]
140 if self.currStep < 1:
147 def getStepWithID(self, id):
149 for x in self.wizard:
150 if self.wizard[x]["id"] == id:
155 def finished(self, *args, **kwargs):
157 currStep = self.currStep
159 if self.updateValues not in self.onShown:
160 self.onShown.append(self.updateValues)
163 if (len(self.wizard[currStep]["list"]) > 0):
164 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
165 self.currStep = self.getStepWithID(nextStep)
167 if (currStep == self.numSteps): # wizard finished
171 self.runCode(self.wizard[currStep]["codeafter"])
172 if self.wizard[currStep]["nextstep"] is not None:
173 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
177 print "Now: " + str(self.currStep)
182 currStep = self.currStep
185 if (self.wizard[currStep]["config"]["screen"] != None):
186 # TODO: don't die, if no run() is available
187 # there was a try/except here, but i can't see a reason
188 # for this. If there is one, please do a more specific check
189 # and/or a comment in which situation there is no run()
190 if callable(getattr(self.configInstance, "runAsync", None)):
191 self.onShown.remove(self.updateValues)
192 self.configInstance.runAsync(self.finished)
195 self.configInstance.run()
198 def keyNumberGlobal(self, number):
199 if (self.wizard[self.currStep]["config"]["screen"] != None):
200 self.configInstance.keyNumberGlobal(number)
203 if (self.wizard[self.currStep]["config"]["screen"] != None):
204 self.configInstance.keyLeft()
208 if (self.wizard[self.currStep]["config"]["screen"] != None):
209 self.configInstance.keyRight()
213 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
214 self["config"].instance.moveSelection(self["config"].instance.moveUp)
215 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
216 self["list"].instance.moveSelection(self["list"].instance.moveUp)
220 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
221 self["config"].instance.moveSelection(self["config"].instance.moveDown)
222 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
223 self["list"].instance.moveSelection(self["list"].instance.moveDown)
226 def runCode(self, code):
231 def updateValues(self):
232 print "Updating values in step " + str(self.currStep)
234 self.stepHistory.append(self.currStep)
236 if self.configInstance is not None:
237 del self.configInstance["config"]
238 self.configInstance.doClose()
239 self.configInstance = None
241 self.condition = True
242 exec (self.wizard[self.currStep]["condition"])
245 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
246 if self.showStepSlider:
247 self["stepslider"].setValue(self.currStep)
249 print "wizard text", _(self.wizard[self.currStep]["text"])
250 self["text"].setText(_(self.wizard[self.currStep]["text"]))
252 self.runCode(self.wizard[self.currStep]["code"])
255 self["list"].instance.setZPosition(1)
257 if (len(self.wizard[self.currStep]["list"]) > 0):
258 self["list"].instance.setZPosition(2)
259 for x in self.wizard[self.currStep]["list"]:
260 self.list.append((_(x[0]), None))
261 self["list"].l.setList(self.list)
262 self["list"].moveToIndex(0)
265 self["config"].instance.setZPosition(1)
266 if (self.wizard[self.currStep]["config"]["screen"] != None):
267 if self.wizard[self.currStep]["config"]["type"] == "standalone":
268 print "Type is standalone"
269 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
271 self["config"].instance.setZPosition(2)
272 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
273 if self.wizard[self.currStep]["config"]["args"] == None:
274 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
276 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
277 self["config"].l.setList(self.configInstance["config"].list)
278 self.configInstance["config"].destroy()
279 self.configInstance["config"] = self["config"]
281 self["config"].l.setList([])
282 else: # condition false
290 def registerWizard(self, wizard, precondition):
291 self.wizards.append((wizard, precondition))
293 def getWizards(self):
295 for x in self.wizards:
296 if x[1] == 1: # precondition
300 wizardManager = WizardManager()