diff --git a/lib/dialogwizard/dialogwizard.py b/lib/dialogwizard/dialogwizard.py index ef146c33..41e2cc77 100755 --- a/lib/dialogwizard/dialogwizard.py +++ b/lib/dialogwizard/dialogwizard.py @@ -6,13 +6,17 @@ # Author: Antoine Durand-Gasselin # -from wizard import Step, PreviousStep, EndScenario +from wizard import Step, PreviousStep, EndScenario, TryAgain from itertools import izip import dialog, time def really_quit(dico): raise EndScenario("game over!", data = dico) +def error_handler(exc): + dialog.Dialog().msgbox('Erreur:', exc.__repr__()) + raise TryAgain() + class DialogStepGenerator(): u"""This class defines a step, that will prompt the user for various fields.""" @@ -34,7 +38,7 @@ class DialogStepGenerator(): for field in sform: item = default.get(field['var'], dico.get(field['var'], '')) 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) for field, val in izip (sform, res): dico[field['var']] = val @@ -51,7 +55,7 @@ class DialogStepGenerator(): if c.get('show_cond', lambda x : True)(dico): schoix.append((c['label'], c['item'])) 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) dico[var] = res return dico @@ -59,13 +63,13 @@ class DialogStepGenerator(): def input_step(self, title, enonce, var, **kw): 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) dico[var] = res return dico return Step(fn) - def checklist_step(self, title, enonce, liste): + def checklist_step(self, title, enonce, liste, **kw): def fn(dico, default): sliste = [] for c in liste: @@ -78,7 +82,7 @@ class DialogStepGenerator(): if dico.get(c['var'], False): checked = 'on' else: checked = 'off' 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) for tag, item, status in liste: if tag in res: diff --git a/lib/dialogwizard/wizard.py b/lib/dialogwizard/wizard.py index 26e5e19a..bbc486f3 100755 --- a/lib/dialogwizard/wizard.py +++ b/lib/dialogwizard/wizard.py @@ -33,8 +33,9 @@ class Step: class Scenario: u"""This class allows us to define scenarios.""" - def __init__(self): - u"""empty scenario""" + def __init__(self) + u"""empty scenario, with an error handler (that takes an exception as + argument)""" self.steps = None def nest(self, step): @@ -87,6 +88,8 @@ def step_scenario(step): def unit_scenario(): return ( Scenario()) +def _relance(exc): raise exc + class Running: u"""To run scenarios""" @@ -94,11 +97,12 @@ class Running: steps = None stack = None - def __init__(self, scenario, env = {}): + def __init__(self, scenario, env = {}, handle = _relance): if not isinstance(scenario, Scenario): raise TypeError("Can only run Scenarios") accu = scenario.steps self.env = env + self.handle = handle # To avoid brain spots on the walls, we shall reverse the list # of steps. @@ -157,6 +161,9 @@ class Running: # We can update defaults pass + except Exception, e: + self.handle(e) + else: # Should not be called raise "invalid step"