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):
26 print "startElement", name
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):
138 for x in self.wizard:
139 if self.wizard[x]["id"] == id:
147 if (self.wizard[self.currStep]["config"]["screen"] != None):
148 try: # don't die, if no run() is available
149 self.configInstance.run()
151 print "Failed to run configInstance"
154 if (len(self.wizard[self.currStep]["list"]) > 0):
155 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
156 self.currStep = self.getStepWithID(nextStep)
158 if (self.currStep == self.numSteps): # wizard finished
162 self.runCode(self.wizard[self.currStep]["codeafter"])
166 print "Now: " + str(self.currStep)
168 def keyNumberGlobal(self, number):
169 if (self.wizard[self.currStep]["config"]["screen"] != None):
170 self.configInstance.keyNumberGlobal(number)
173 if (self.wizard[self.currStep]["config"]["screen"] != None):
174 self.configInstance.keyLeft()
178 if (self.wizard[self.currStep]["config"]["screen"] != None):
179 self.configInstance.keyRight()
183 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
184 self["config"].instance.moveSelection(self["config"].instance.moveUp)
185 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
186 self["list"].instance.moveSelection(self["list"].instance.moveUp)
190 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
191 self["config"].instance.moveSelection(self["config"].instance.moveDown)
192 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
193 self["list"].instance.moveSelection(self["list"].instance.moveDown)
196 def runCode(self, code):
201 def updateValues(self):
202 print "Updating values in step " + str(self.currStep)
204 self.condition = True
205 exec (self.wizard[self.currStep]["condition"])
208 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
209 if self.showStepSlider:
210 self["stepslider"].setValue(self.currStep)
212 print "wizard text", _(self.wizard[self.currStep]["text"])
213 self["text"].setText(_(self.wizard[self.currStep]["text"]))
215 self.runCode(self.wizard[self.currStep]["code"])
218 self["list"].instance.setZPosition(1)
220 if (len(self.wizard[self.currStep]["list"]) > 0):
221 self["list"].instance.setZPosition(2)
222 for x in self.wizard[self.currStep]["list"]:
223 self.list.append((_(x[0]), None))
224 self["list"].l.setList(self.list)
227 self["config"].instance.setZPosition(1)
228 if (self.wizard[self.currStep]["config"]["screen"] != None):
229 if self.wizard[self.currStep]["config"]["type"] == "standalone":
230 print "Type is standalone"
231 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
233 self["config"].instance.setZPosition(2)
234 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
235 if self.wizard[self.currStep]["config"]["args"] == None:
236 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
238 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
239 self["config"].l.setList(self.configInstance["config"].list)
240 self.configInstance["config"] = self["config"]
242 self["config"].l.setList([])
243 else: # condition false
251 def registerWizard(self, wizard, precondition):
252 self.wizards.append((wizard, precondition))
254 def getWizards(self):
256 for x in self.wizards:
257 if x[1] == 1: # precondition
261 wizardManager = WizardManager()