[lib/dialogwizard] on traite les exceptions
Ignore-this: 9a90bd32263c02b9e7f2909b2f69e301 darcs-hash:20090929132138-bd074-50fd73412bff90c7295edd03353a5752eb3b3db7.gz
This commit is contained in:
parent
de68b18b75
commit
277b1c4bd5
2 changed files with 20 additions and 9 deletions
|
@ -6,13 +6,17 @@
|
||||||
# Author: Antoine Durand-Gasselin <adg@crans.org>
|
# Author: Antoine Durand-Gasselin <adg@crans.org>
|
||||||
#
|
#
|
||||||
|
|
||||||
from wizard import Step, PreviousStep, EndScenario
|
from wizard import Step, PreviousStep, EndScenario, TryAgain
|
||||||
from itertools import izip
|
from itertools import izip
|
||||||
import dialog, time
|
import dialog, time
|
||||||
|
|
||||||
def really_quit(dico):
|
def really_quit(dico):
|
||||||
raise EndScenario("game over!", data = dico)
|
raise EndScenario("game over!", data = dico)
|
||||||
|
|
||||||
|
def error_handler(exc):
|
||||||
|
dialog.Dialog().msgbox('Erreur:', exc.__repr__())
|
||||||
|
raise TryAgain()
|
||||||
|
|
||||||
class DialogStepGenerator():
|
class DialogStepGenerator():
|
||||||
u"""This class defines a step, that will prompt the user for various
|
u"""This class defines a step, that will prompt the user for various
|
||||||
fields."""
|
fields."""
|
||||||
|
@ -34,7 +38,7 @@ class DialogStepGenerator():
|
||||||
for field in sform:
|
for field in sform:
|
||||||
item = default.get(field['var'], dico.get(field['var'], ''))
|
item = default.get(field['var'], dico.get(field['var'], ''))
|
||||||
field['item'] = item
|
field['item'] = item
|
||||||
rc, res = self.d.form(enonce, fields = sform, title=title, **kw)
|
rc, res = self.d.form(enonce % dico, fields = sform, title=title % dico, **kw)
|
||||||
self._check_rc(rc)
|
self._check_rc(rc)
|
||||||
for field, val in izip (sform, res):
|
for field, val in izip (sform, res):
|
||||||
dico[field['var']] = val
|
dico[field['var']] = val
|
||||||
|
@ -51,7 +55,7 @@ class DialogStepGenerator():
|
||||||
if c.get('show_cond', lambda x : True)(dico):
|
if c.get('show_cond', lambda x : True)(dico):
|
||||||
schoix.append((c['label'], c['item']))
|
schoix.append((c['label'], c['item']))
|
||||||
print schoix
|
print schoix
|
||||||
rc, res = self.d.menu(enonce, choices = schoix, title = title, **kw)
|
rc, res = self.d.menu(enonce % dico, choices = schoix, title = title % dico, **kw)
|
||||||
self._check_rc(rc)
|
self._check_rc(rc)
|
||||||
dico[var] = res
|
dico[var] = res
|
||||||
return dico
|
return dico
|
||||||
|
@ -59,13 +63,13 @@ class DialogStepGenerator():
|
||||||
|
|
||||||
def input_step(self, title, enonce, var, **kw):
|
def input_step(self, title, enonce, var, **kw):
|
||||||
def fn(dico, default):
|
def fn(dico, default):
|
||||||
rc, res = self.d.inputbox(enonce, title = title, init = dico.get(var, ''), **kw)
|
rc, res = self.d.inputbox(enonce % dico, title = title % dico, init = dico.get(var, ''), **kw)
|
||||||
self._check_rc(rc)
|
self._check_rc(rc)
|
||||||
dico[var] = res
|
dico[var] = res
|
||||||
return dico
|
return dico
|
||||||
return Step(fn)
|
return Step(fn)
|
||||||
|
|
||||||
def checklist_step(self, title, enonce, liste):
|
def checklist_step(self, title, enonce, liste, **kw):
|
||||||
def fn(dico, default):
|
def fn(dico, default):
|
||||||
sliste = []
|
sliste = []
|
||||||
for c in liste:
|
for c in liste:
|
||||||
|
@ -78,7 +82,7 @@ class DialogStepGenerator():
|
||||||
if dico.get(c['var'], False): checked = 'on'
|
if dico.get(c['var'], False): checked = 'on'
|
||||||
else: checked = 'off'
|
else: checked = 'off'
|
||||||
sliste.append((c['var'], c['item'], checked))
|
sliste.append((c['var'], c['item'], checked))
|
||||||
rc, res = self.d.checklist(enonce, title = title, choices = sliste, **kw)
|
rc, res = self.d.checklist(enonce % dico, title = title % dico, choices = sliste, **kw)
|
||||||
self._check_rc(rc)
|
self._check_rc(rc)
|
||||||
for tag, item, status in liste:
|
for tag, item, status in liste:
|
||||||
if tag in res:
|
if tag in res:
|
||||||
|
|
|
@ -33,8 +33,9 @@ class Step:
|
||||||
class Scenario:
|
class Scenario:
|
||||||
u"""This class allows us to define scenarios."""
|
u"""This class allows us to define scenarios."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self)
|
||||||
u"""empty scenario"""
|
u"""empty scenario, with an error handler (that takes an exception as
|
||||||
|
argument)"""
|
||||||
self.steps = None
|
self.steps = None
|
||||||
|
|
||||||
def nest(self, step):
|
def nest(self, step):
|
||||||
|
@ -87,6 +88,8 @@ def step_scenario(step):
|
||||||
def unit_scenario():
|
def unit_scenario():
|
||||||
return ( Scenario())
|
return ( Scenario())
|
||||||
|
|
||||||
|
def _relance(exc): raise exc
|
||||||
|
|
||||||
class Running:
|
class Running:
|
||||||
u"""To run scenarios"""
|
u"""To run scenarios"""
|
||||||
|
|
||||||
|
@ -94,11 +97,12 @@ class Running:
|
||||||
steps = None
|
steps = None
|
||||||
stack = None
|
stack = None
|
||||||
|
|
||||||
def __init__(self, scenario, env = {}):
|
def __init__(self, scenario, env = {}, handle = _relance):
|
||||||
if not isinstance(scenario, Scenario):
|
if not isinstance(scenario, Scenario):
|
||||||
raise TypeError("Can only run Scenarios")
|
raise TypeError("Can only run Scenarios")
|
||||||
accu = scenario.steps
|
accu = scenario.steps
|
||||||
self.env = env
|
self.env = env
|
||||||
|
self.handle = handle
|
||||||
|
|
||||||
# To avoid brain spots on the walls, we shall reverse the list
|
# To avoid brain spots on the walls, we shall reverse the list
|
||||||
# of steps.
|
# of steps.
|
||||||
|
@ -157,6 +161,9 @@ class Running:
|
||||||
# We can update defaults
|
# We can update defaults
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
except Exception, e:
|
||||||
|
self.handle(e)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Should not be called
|
# Should not be called
|
||||||
raise "invalid step"
|
raise "invalid step"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue