1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.Label import Label
7 from Components.Slider import Slider
8 from Components.ActionMap import HelpableActionMap, NumberActionMap
9 from Components.Pixmap import *
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
24 def startElement(self, name, attrs):
26 self.currContent = name
28 self.lastStep = int(attrs.get('number'))
29 self.wizard[self.lastStep] = {"condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": ""}
30 elif (name == "text"):
31 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
32 elif (name == "listentry"):
33 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
34 elif (name == "config"):
35 exec "from Screens." + str(attrs.get('module')) + " import *"
36 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
37 if (attrs.has_key('args')):
39 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
40 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
41 elif (name == "code"):
42 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
45 self.codeafter = False
46 elif (name == "condition"):
48 def endElement(self, name):
52 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
54 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
55 elif name == 'condition':
56 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
58 def characters(self, ch):
59 if self.currContent == "code":
61 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
63 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
64 elif self.currContent == "condition":
65 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
66 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
67 Screen.__init__(self, session)
68 HelpableScreen.__init__(self)
71 parser = make_parser()
72 print "Reading " + self.xmlfile
73 wizardHandler = self.parseWizard(self.wizard)
74 parser.setContentHandler(wizardHandler)
75 parser.parse('/usr/share/enigma2/' + self.xmlfile)
77 self.showSteps = showSteps
78 self.showStepSlider = showStepSlider
79 self.showList = showList
80 self.showConfig = showConfig
82 self.numSteps = len(self.wizard)
85 self["text"] = Label()
88 self["config"] = ConfigList([])
91 self["step"] = Label()
93 if self.showStepSlider:
94 self["stepslider"] = Slider(1, self.numSteps)
98 self["list"] = MenuList(self.list)
100 self.onShown.append(self.updateValues)
102 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
110 "1": self.keyNumberGlobal,
111 "2": self.keyNumberGlobal,
112 "3": self.keyNumberGlobal,
113 "4": self.keyNumberGlobal,
114 "5": self.keyNumberGlobal,
115 "6": self.keyNumberGlobal,
116 "7": self.keyNumberGlobal,
117 "8": self.keyNumberGlobal,
118 "9": self.keyNumberGlobal,
119 "0": self.keyNumberGlobal
124 if self.currStep < 1:
134 if (self.wizard[self.currStep]["config"]["screen"] != None):
135 try: # don't die, if no run() is available
136 self.configInstance.run()
138 print "Failed to run configInstance"
141 if (len(self.wizard[self.currStep]["list"]) > 0):
142 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
143 if nextStep == "end":
144 self.currStep = self.numSteps
145 elif nextStep == "next":
148 self.currStep = int(nextStep) - 1
150 if (self.currStep == self.numSteps): # wizard finished
154 self.runCode(self.wizard[self.currStep]["codeafter"])
158 print "Now: " + str(self.currStep)
160 def keyNumberGlobal(self, number):
161 if (self.wizard[self.currStep]["config"]["screen"] != None):
162 self.configInstance.keyNumberGlobal(number)
165 if (self.wizard[self.currStep]["config"]["screen"] != None):
166 self.configInstance.keyLeft()
170 if (self.wizard[self.currStep]["config"]["screen"] != None):
171 self.configInstance.keyRight()
175 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
176 self["config"].instance.moveSelection(self["config"].instance.moveUp)
177 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
178 self["list"].instance.moveSelection(self["list"].instance.moveUp)
182 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
183 self["config"].instance.moveSelection(self["config"].instance.moveDown)
184 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
185 self["list"].instance.moveSelection(self["list"].instance.moveDown)
188 def runCode(self, code):
193 def updateValues(self):
194 print "Updating values in step " + str(self.currStep)
196 self.condition = True
197 exec (self.wizard[self.currStep]["condition"])
200 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
201 if self.showStepSlider:
202 self["stepslider"].setValue(self.currStep)
204 print _(self.wizard[self.currStep]["text"])
205 self["text"].setText(_(self.wizard[self.currStep]["text"]))
207 self.runCode(self.wizard[self.currStep]["code"])
210 self["list"].instance.setZPosition(1)
212 if (len(self.wizard[self.currStep]["list"]) > 0):
213 self["list"].instance.setZPosition(2)
214 for x in self.wizard[self.currStep]["list"]:
215 self.list.append((_(x[0]), None))
216 self["list"].l.setList(self.list)
219 self["config"].instance.setZPosition(1)
220 if (self.wizard[self.currStep]["config"]["screen"] != None):
221 if self.wizard[self.currStep]["config"]["type"] == "standalone":
222 print "Type is standalone"
223 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
225 self["config"].instance.setZPosition(2)
226 print self.wizard[self.currStep]["config"]["screen"]
227 if self.wizard[self.currStep]["config"]["args"] == None:
228 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
230 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
231 self["config"].l.setList(self.configInstance["config"].list)
232 self.configInstance["config"] = self["config"]
234 self["config"].l.setList([])
235 else: # condition false
243 def registerWizard(self, wizard, precondition):
244 self.wizards.append((wizard, precondition))
246 def getWizards(self):
248 for x in self.wizards:
249 if x[1] == 1: # precondition
253 wizardManager = WizardManager()