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": ""}
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 == "condition"):
43 def endElement(self, name):
46 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
47 elif name == 'condition':
48 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
50 def characters(self, ch):
51 if self.currContent == "code":
52 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
53 elif self.currContent == "condition":
54 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
55 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
56 Screen.__init__(self, session)
57 HelpableScreen.__init__(self)
60 parser = make_parser()
61 print "Reading " + self.xmlfile
62 wizardHandler = self.parseWizard(self.wizard)
63 parser.setContentHandler(wizardHandler)
64 parser.parse('/usr/share/enigma2/' + self.xmlfile)
66 self.showSteps = showSteps
67 self.showStepSlider = showStepSlider
68 self.showList = showList
69 self.showConfig = showConfig
71 self.numSteps = len(self.wizard)
74 self["text"] = Label()
77 self["config"] = ConfigList([])
80 self["step"] = Label()
82 if self.showStepSlider:
83 self["stepslider"] = Slider(1, self.numSteps)
87 self["list"] = MenuList(self.list)
89 self.onShown.append(self.updateValues)
91 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
99 "1": self.keyNumberGlobal,
100 "2": self.keyNumberGlobal,
101 "3": self.keyNumberGlobal,
102 "4": self.keyNumberGlobal,
103 "5": self.keyNumberGlobal,
104 "6": self.keyNumberGlobal,
105 "7": self.keyNumberGlobal,
106 "8": self.keyNumberGlobal,
107 "9": self.keyNumberGlobal,
108 "0": self.keyNumberGlobal
113 if self.currStep < 1:
123 if (self.wizard[self.currStep]["config"]["screen"] != None):
124 try: # don't die, if no run() is available
125 self.configInstance.run()
127 print "Failed to run configInstance"
130 if (len(self.wizard[self.currStep]["list"]) > 0):
131 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
132 if nextStep == "end":
133 self.currStep = self.numSteps
134 elif nextStep == "next":
137 self.currStep = int(nextStep) - 1
139 if (self.currStep == self.numSteps): # wizard finished
146 print "Now: " + str(self.currStep)
148 def keyNumberGlobal(self, number):
149 if (self.wizard[self.currStep]["config"]["screen"] != None):
150 self.configInstance.keyNumberGlobal(number)
153 if (self.wizard[self.currStep]["config"]["screen"] != None):
154 self.configInstance.keyLeft()
158 if (self.wizard[self.currStep]["config"]["screen"] != None):
159 self.configInstance.keyRight()
163 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
164 self["config"].instance.moveSelection(self["config"].instance.moveUp)
165 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
166 self["list"].instance.moveSelection(self["list"].instance.moveUp)
170 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
171 self["config"].instance.moveSelection(self["config"].instance.moveDown)
172 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
173 self["list"].instance.moveSelection(self["list"].instance.moveDown)
176 def updateValues(self):
177 print "Updating values in step " + str(self.currStep)
179 self.condition = True
180 exec (self.wizard[self.currStep]["condition"])
183 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
184 if self.showStepSlider:
185 self["stepslider"].setValue(self.currStep)
187 print _(self.wizard[self.currStep]["text"])
188 self["text"].setText(_(self.wizard[self.currStep]["text"]))
190 if self.wizard[self.currStep]["code"] != "":
191 print self.wizard[self.currStep]["code"]
192 exec(self.wizard[self.currStep]["code"])
195 self["list"].instance.setZPosition(1)
197 if (len(self.wizard[self.currStep]["list"]) > 0):
198 self["list"].instance.setZPosition(2)
199 for x in self.wizard[self.currStep]["list"]:
200 self.list.append((_(x[0]), None))
201 self["list"].l.setList(self.list)
204 self["config"].instance.setZPosition(1)
205 if (self.wizard[self.currStep]["config"]["screen"] != None):
206 if self.wizard[self.currStep]["config"]["type"] == "standalone":
207 print "Type is standalone"
208 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
210 self["config"].instance.setZPosition(2)
211 print self.wizard[self.currStep]["config"]["screen"]
212 if self.wizard[self.currStep]["config"]["args"] == None:
213 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
215 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
216 self["config"].l.setList(self.configInstance["config"].list)
217 self.configInstance["config"] = self["config"]
219 self["config"].l.setList([])
220 else: # condition false
228 def registerWizard(self, wizard, precondition):
229 self.wizards.append((wizard, precondition))
231 def getWizards(self):
233 for x in self.wizards:
234 if x[1] == 1: # precondition
238 wizardManager = WizardManager()