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 if attrs.has_key('nextstep'):
35 nextstep = str(attrs.get('nextstep'))
38 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
39 elif (name == "text"):
40 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
41 elif (name == "listentry"):
42 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
43 elif (name == "config"):
44 exec "from Screens." + str(attrs.get('module')) + " import *"
45 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
46 if (attrs.has_key('args')):
48 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
49 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
50 elif (name == "code"):
51 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
54 self.codeafter = False
55 elif (name == "condition"):
57 def endElement(self, name):
61 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
63 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
64 elif name == 'condition':
65 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
67 def characters(self, ch):
68 if self.currContent == "code":
70 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
72 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
73 elif self.currContent == "condition":
74 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
75 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
76 Screen.__init__(self, session)
77 HelpableScreen.__init__(self)
82 parser = make_parser()
83 print "Reading " + self.xmlfile
84 wizardHandler = self.parseWizard(self.wizard)
85 parser.setContentHandler(wizardHandler)
86 parser.parse('/usr/share/enigma2/' + self.xmlfile)
88 self.showSteps = showSteps
89 self.showStepSlider = showStepSlider
90 self.showList = showList
91 self.showConfig = showConfig
93 self.numSteps = len(self.wizard)
96 self["text"] = Label()
99 self["config"] = ConfigList([])
102 self["step"] = Label()
104 if self.showStepSlider:
105 self["stepslider"] = Slider(1, self.numSteps)
109 self["list"] = MenuList(self.list)
111 self.onShown.append(self.updateValues)
113 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
121 "1": self.keyNumberGlobal,
122 "2": self.keyNumberGlobal,
123 "3": self.keyNumberGlobal,
124 "4": self.keyNumberGlobal,
125 "5": self.keyNumberGlobal,
126 "6": self.keyNumberGlobal,
127 "7": self.keyNumberGlobal,
128 "8": self.keyNumberGlobal,
129 "9": self.keyNumberGlobal,
130 "0": self.keyNumberGlobal
134 if len(self.stepHistory) > 1:
135 self.currStep = self.stepHistory[-2]
136 self.stepHistory = self.stepHistory[:-2]
137 if self.currStep < 1:
144 def getStepWithID(self, id):
146 for x in self.wizard:
147 if self.wizard[x]["id"] == id:
154 currStep = self.currStep
156 if (self.wizard[currStep]["config"]["screen"] != None):
157 try: # don't die, if no run() is available
158 self.configInstance.run()
160 print "Failed to run configInstance"
163 if (len(self.wizard[currStep]["list"]) > 0):
164 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
165 self.currStep = self.getStepWithID(nextStep)
167 if (currStep == self.numSteps): # wizard finished
171 self.runCode(self.wizard[currStep]["codeafter"])
172 if self.wizard[currStep]["nextstep"] is not None:
173 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
177 print "Now: " + str(self.currStep)
179 def keyNumberGlobal(self, number):
180 if (self.wizard[self.currStep]["config"]["screen"] != None):
181 self.configInstance.keyNumberGlobal(number)
184 if (self.wizard[self.currStep]["config"]["screen"] != None):
185 self.configInstance.keyLeft()
189 if (self.wizard[self.currStep]["config"]["screen"] != None):
190 self.configInstance.keyRight()
194 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
195 self["config"].instance.moveSelection(self["config"].instance.moveUp)
196 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
197 self["list"].instance.moveSelection(self["list"].instance.moveUp)
201 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
202 self["config"].instance.moveSelection(self["config"].instance.moveDown)
203 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
204 self["list"].instance.moveSelection(self["list"].instance.moveDown)
207 def runCode(self, code):
212 def updateValues(self):
213 print "Updating values in step " + str(self.currStep)
215 self.stepHistory.append(self.currStep)
217 self.condition = True
218 exec (self.wizard[self.currStep]["condition"])
221 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
222 if self.showStepSlider:
223 self["stepslider"].setValue(self.currStep)
225 print "wizard text", _(self.wizard[self.currStep]["text"])
226 self["text"].setText(_(self.wizard[self.currStep]["text"]))
228 self.runCode(self.wizard[self.currStep]["code"])
231 self["list"].instance.setZPosition(1)
233 if (len(self.wizard[self.currStep]["list"]) > 0):
234 self["list"].instance.setZPosition(2)
235 for x in self.wizard[self.currStep]["list"]:
236 self.list.append((_(x[0]), None))
237 self["list"].l.setList(self.list)
240 self["config"].instance.setZPosition(1)
241 if (self.wizard[self.currStep]["config"]["screen"] != None):
242 if self.wizard[self.currStep]["config"]["type"] == "standalone":
243 print "Type is standalone"
244 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
246 self["config"].instance.setZPosition(2)
247 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
248 if self.wizard[self.currStep]["config"]["args"] == None:
249 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
251 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
252 self["config"].l.setList(self.configInstance["config"].list)
253 self.configInstance["config"] = self["config"]
255 self["config"].l.setList([])
256 else: # condition false
264 def registerWizard(self, wizard, precondition):
265 self.wizards.append((wizard, precondition))
267 def getWizards(self):
269 for x in self.wizards:
270 if x[1] == 1: # precondition
274 wizardManager = WizardManager()