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 HelpableActionMap, NumberActionMap
10 from Components.Pixmap import *
11 from Components.MenuList import MenuList
12 from Components.ConfigList import ConfigList
14 from xml.sax import make_parser
15 from xml.sax.handler import ContentHandler
17 class Wizard(Screen, HelpableScreen):
19 class parseWizard(ContentHandler):
20 def __init__(self, wizard):
21 self.isPointsElement, self.isReboundsElement = 0, 0
26 def startElement(self, name, attrs):
27 print "startElement", name
28 self.currContent = name
31 if attrs.has_key('id'):
32 id = str(attrs.get('id'))
35 if attrs.has_key('nextstep'):
36 nextstep = str(attrs.get('nextstep'))
39 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
40 elif (name == "text"):
41 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
42 elif (name == "listentry"):
43 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
44 elif (name == "config"):
45 exec "from Screens." + str(attrs.get('module')) + " import *"
46 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
47 if (attrs.has_key('args')):
49 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
50 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
51 elif (name == "code"):
52 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
55 self.codeafter = False
56 elif (name == "condition"):
58 def endElement(self, name):
62 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
64 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
65 elif name == 'condition':
66 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
68 def characters(self, ch):
69 if self.currContent == "code":
71 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
73 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
74 elif self.currContent == "condition":
75 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
77 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
78 Screen.__init__(self, session)
79 HelpableScreen.__init__(self)
84 parser = make_parser()
85 print "Reading " + self.xmlfile
86 wizardHandler = self.parseWizard(self.wizard)
87 parser.setContentHandler(wizardHandler)
88 parser.parse('/usr/share/enigma2/' + self.xmlfile)
90 self.showSteps = showSteps
91 self.showStepSlider = showStepSlider
92 self.showList = showList
93 self.showConfig = showConfig
95 self.numSteps = len(self.wizard)
98 self["text"] = Label()
101 self["config"] = ConfigList([])
104 self["step"] = Label()
106 if self.showStepSlider:
107 self["stepslider"] = Slider(1, self.numSteps)
111 self["list"] = MenuList(self.list)
113 self.onShown.append(self.updateValues)
115 self.configInstance = None
117 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
125 "1": self.keyNumberGlobal,
126 "2": self.keyNumberGlobal,
127 "3": self.keyNumberGlobal,
128 "4": self.keyNumberGlobal,
129 "5": self.keyNumberGlobal,
130 "6": self.keyNumberGlobal,
131 "7": self.keyNumberGlobal,
132 "8": self.keyNumberGlobal,
133 "9": self.keyNumberGlobal,
134 "0": self.keyNumberGlobal
138 if len(self.stepHistory) > 1:
139 self.currStep = self.stepHistory[-2]
140 self.stepHistory = self.stepHistory[:-2]
141 if self.currStep < 1:
148 def getStepWithID(self, id):
150 for x in self.wizard:
151 if self.wizard[x]["id"] == id:
156 def finished(self, **args):
158 currStep = self.currStep
160 if self.updateValues not in self.onShown:
161 self.onShown.append(self.updateValues)
164 if (len(self.wizard[currStep]["list"]) > 0):
165 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
166 self.currStep = self.getStepWithID(nextStep)
168 if (currStep == self.numSteps): # wizard finished
172 self.runCode(self.wizard[currStep]["codeafter"])
173 if self.wizard[currStep]["nextstep"] is not None:
174 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
178 print "Now: " + str(self.currStep)
183 currStep = self.currStep
186 if (self.wizard[currStep]["config"]["screen"] != None):
187 # TODO: don't die, if no run() is available
188 # there was a try/except here, but i can't see a reason
189 # for this. If there is one, please do a more specific check
190 # and/or a comment in which situation there is no run()
191 if callable(getattr(self.configInstance, "runAsync", None)):
192 self.onShown.remove(self.updateValues)
193 self.configInstance.runAsync(self.finished)
196 self.configInstance.run()
199 def keyNumberGlobal(self, number):
200 if (self.wizard[self.currStep]["config"]["screen"] != None):
201 self.configInstance.keyNumberGlobal(number)
204 if (self.wizard[self.currStep]["config"]["screen"] != None):
205 self.configInstance.keyLeft()
209 if (self.wizard[self.currStep]["config"]["screen"] != None):
210 self.configInstance.keyRight()
214 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
215 self["config"].instance.moveSelection(self["config"].instance.moveUp)
216 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
217 self["list"].instance.moveSelection(self["list"].instance.moveUp)
221 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
222 self["config"].instance.moveSelection(self["config"].instance.moveDown)
223 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
224 self["list"].instance.moveSelection(self["list"].instance.moveDown)
227 def runCode(self, code):
232 def updateValues(self):
233 print "Updating values in step " + str(self.currStep)
235 self.stepHistory.append(self.currStep)
237 if self.configInstance is not None:
238 del self.configInstance["config"]
239 self.configInstance.doClose()
240 self.configInstance = None
242 self.condition = True
243 exec (self.wizard[self.currStep]["condition"])
246 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
247 if self.showStepSlider:
248 self["stepslider"].setValue(self.currStep)
250 print "wizard text", _(self.wizard[self.currStep]["text"])
251 self["text"].setText(_(self.wizard[self.currStep]["text"]))
253 self.runCode(self.wizard[self.currStep]["code"])
256 self["list"].instance.setZPosition(1)
258 if (len(self.wizard[self.currStep]["list"]) > 0):
259 self["list"].instance.setZPosition(2)
260 for x in self.wizard[self.currStep]["list"]:
261 self.list.append((_(x[0]), None))
262 self["list"].l.setList(self.list)
263 self["list"].moveToIndex(0)
266 self["config"].instance.setZPosition(1)
267 if (self.wizard[self.currStep]["config"]["screen"] != None):
268 if self.wizard[self.currStep]["config"]["type"] == "standalone":
269 print "Type is standalone"
270 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
272 self["config"].instance.setZPosition(2)
273 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
274 if self.wizard[self.currStep]["config"]["args"] == None:
275 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
277 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
278 self["config"].l.setList(self.configInstance["config"].list)
279 self.configInstance["config"].destroy()
280 self.configInstance["config"] = self["config"]
282 self["config"].l.setList([])
283 else: # condition false
291 def registerWizard(self, wizard, precondition):
292 self.wizards.append((wizard, precondition))
294 def getWizards(self):
296 for x in self.wizards:
297 if x[1] == 1: # precondition
301 wizardManager = WizardManager()