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
25 def startElement(self, name, attrs):
27 self.currContent = name
30 if attrs.has_key('id'):
31 id = str(attrs.get('id'))
34 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": ""}
35 elif (name == "text"):
36 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
37 elif (name == "listentry"):
38 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
39 elif (name == "config"):
40 exec "from Screens." + str(attrs.get('module')) + " import *"
41 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
42 if (attrs.has_key('args')):
44 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
45 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
46 elif (name == "code"):
47 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
50 self.codeafter = False
51 elif (name == "condition"):
53 def endElement(self, name):
57 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
59 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
60 elif name == 'condition':
61 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
63 def characters(self, ch):
64 if self.currContent == "code":
66 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
68 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
69 elif self.currContent == "condition":
70 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
71 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
72 Screen.__init__(self, session)
73 HelpableScreen.__init__(self)
76 parser = make_parser()
77 print "Reading " + self.xmlfile
78 wizardHandler = self.parseWizard(self.wizard)
79 parser.setContentHandler(wizardHandler)
80 parser.parse('/usr/share/enigma2/' + self.xmlfile)
82 self.showSteps = showSteps
83 self.showStepSlider = showStepSlider
84 self.showList = showList
85 self.showConfig = showConfig
87 self.numSteps = len(self.wizard)
90 self["text"] = Label()
93 self["config"] = ConfigList([])
96 self["step"] = Label()
98 if self.showStepSlider:
99 self["stepslider"] = Slider(1, self.numSteps)
103 self["list"] = MenuList(self.list)
105 self.onShown.append(self.updateValues)
107 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
115 "1": self.keyNumberGlobal,
116 "2": self.keyNumberGlobal,
117 "3": self.keyNumberGlobal,
118 "4": self.keyNumberGlobal,
119 "5": self.keyNumberGlobal,
120 "6": self.keyNumberGlobal,
121 "7": self.keyNumberGlobal,
122 "8": self.keyNumberGlobal,
123 "9": self.keyNumberGlobal,
124 "0": self.keyNumberGlobal
129 if self.currStep < 1:
136 def getStepWithID(self, id):
139 for x in self.wizard:
140 if self.wizard[x]["id"] == id:
148 if (self.wizard[self.currStep]["config"]["screen"] != None):
149 try: # don't die, if no run() is available
150 self.configInstance.run()
152 print "Failed to run configInstance"
155 if (len(self.wizard[self.currStep]["list"]) > 0):
156 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
157 self.currStep = self.getStepWithID(nextStep)
159 if (self.currStep == self.numSteps): # wizard finished
163 self.runCode(self.wizard[self.currStep]["codeafter"])
167 print "Now: " + str(self.currStep)
169 def keyNumberGlobal(self, number):
170 if (self.wizard[self.currStep]["config"]["screen"] != None):
171 self.configInstance.keyNumberGlobal(number)
174 if (self.wizard[self.currStep]["config"]["screen"] != None):
175 self.configInstance.keyLeft()
179 if (self.wizard[self.currStep]["config"]["screen"] != None):
180 self.configInstance.keyRight()
184 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
185 self["config"].instance.moveSelection(self["config"].instance.moveUp)
186 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
187 self["list"].instance.moveSelection(self["list"].instance.moveUp)
191 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
192 self["config"].instance.moveSelection(self["config"].instance.moveDown)
193 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
194 self["list"].instance.moveSelection(self["list"].instance.moveDown)
197 def runCode(self, code):
202 def updateValues(self):
203 print "Updating values in step " + str(self.currStep)
205 self.condition = True
206 exec (self.wizard[self.currStep]["condition"])
209 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
210 if self.showStepSlider:
211 self["stepslider"].setValue(self.currStep)
213 print _(self.wizard[self.currStep]["text"])
214 self["text"].setText(_(self.wizard[self.currStep]["text"]))
216 self.runCode(self.wizard[self.currStep]["code"])
219 self["list"].instance.setZPosition(1)
221 if (len(self.wizard[self.currStep]["list"]) > 0):
222 self["list"].instance.setZPosition(2)
223 for x in self.wizard[self.currStep]["list"]:
224 self.list.append((_(x[0]), None))
225 self["list"].l.setList(self.list)
228 self["config"].instance.setZPosition(1)
229 if (self.wizard[self.currStep]["config"]["screen"] != None):
230 if self.wizard[self.currStep]["config"]["type"] == "standalone":
231 print "Type is standalone"
232 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
234 self["config"].instance.setZPosition(2)
235 print self.wizard[self.currStep]["config"]["screen"]
236 if self.wizard[self.currStep]["config"]["args"] == None:
237 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
239 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
240 self["config"].l.setList(self.configInstance["config"].list)
241 self.configInstance["config"] = self["config"]
243 self["config"].l.setList([])
244 else: # condition false
252 def registerWizard(self, wizard, precondition):
253 self.wizards.append((wizard, precondition))
255 def getWizards(self):
257 for x in self.wizards:
258 if x[1] == 1: # precondition
262 wizardManager = WizardManager()