diff --git a/lib/nulib/python/nulib/web/bootstrap.py b/lib/nulib/python/nulib/web/bootstrap.py index ffba723..214c9d9 100644 --- a/lib/nulib/python/nulib/web/bootstrap.py +++ b/lib/nulib/python/nulib/web/bootstrap.py @@ -43,6 +43,13 @@ def load(prefix=None, icons=True, charset="utf-8"): ]) return u"\n".join(lines) +def sa(value, kw, name, default=Undef): + """si value est indéfini, récupérer la valeur avec le nom court dans kw + """ + if value is Undef and name is not None: value = kw.pop(name, Undef) + if value is Undef: value = default + return value + ################################################################################ # Menu @@ -246,15 +253,24 @@ class Alert(object): action = None showtb = None - def __init__(self, msg=None, exc_info=Undef, type="error", closeable=False, escape=None, action=None, showtb=True, **kw): - self(msg, exc_info, type, closeable, escape, action, showtb, **kw) + def __init__(self, msg=None, exc_info=Undef, type=Undef, closeable=Undef, escape=Undef, action=None, showtb=True, **kw): + exc_info = sa(exc_info, kw, 'e') + if exc_info is Undef: exc_info = sys.exc_info() + if exc_info == (None, None, None): exc_info = None + type = sa(type, kw, 't', "error") + closeable = sa(closeable, kw, 'c', False) + escape = sa(escape, kw, 'x', None) + + self.msg = msg + self.exc_info = exc_info + self.type = ALERT_TYPE_MAP.get(type, type) + self.closeable = closeable + self.escape = escape + self.action = action + self.showtb = showtb def __call__(self, msg=Undef, exc_info=Undef, type=Undef, closeable=Undef, escape=Undef, action=Undef, showtb=Undef, **kw): - if exc_info is Undef: exc_info = kw.pop('e', Undef) - if type is Undef: type = kw.pop('t', Undef) - if closeable is Undef: closeable = kw.pop('c', Undef) - if escape is Undef: escape = kw.pop('x', Undef) - + exc_info = sa(exc_info, kw, 'e') if msg is Undef: # si on ne spécifie pas de message, alors prendre la valeur actuelle msg = self.msg @@ -263,11 +279,11 @@ class Alert(object): # si on spécifie un message, alors prendre aussi l'exception courante if exc_info is Undef: exc_info = sys.exc_info() if exc_info == (None, None, None): exc_info = None - if type is Undef: type = self.type - if closeable is Undef: closeable = self.closeable - if escape is Undef: escape = self.escape - if action is Undef: action = self.action - if showtb is Undef: showtb = self.showtb + type = sa(type, kw, 't', self.type) + closeable = sa(closeable, kw, 'c', self.closeable) + escape = sa(escape, kw, 'x', self.escape) + action = sa(action, kw, None, self.action) + showtb = sa(showtb, kw, None, self.showtb) self.msg = msg self.exc_info = exc_info @@ -279,11 +295,7 @@ class Alert(object): return self def render(self, msg=Undef, exc_info=Undef, type=Undef, closeable=Undef, escape=Undef, action=Undef, showtb=Undef, **kw): - if exc_info is Undef: exc_info = kw.pop('e', Undef) - if type is Undef: type = kw.pop('t', Undef) - if closeable is Undef: closeable = kw.pop('c', Undef) - if escape is Undef: escape = kw.pop('x', Undef) - + exc_info = sa(exc_info, kw, 'e') if msg is Undef: # si on ne spécifie pas de message, alors prendre la valeur initiale msg = self.msg @@ -291,11 +303,11 @@ class Alert(object): else: # si on spécifie un message, alors prendre aussi l'exception courante if exc_info is Undef: exc_info = sys.exc_info() - if type is Undef: type = self.type - if closeable is Undef: closeable = self.closeable - if escape is Undef: escape = self.escape - if action is Undef: action = self.action - if showtb is Undef: showtb = self.showtb + type = sa(type, kw, 't', self.type) + closeable = sa(closeable, kw, 'c', self.closeable) + escape = sa(escape, kw, 'x', self.escape) + action = sa(action, kw, None, self.action) + showtb = sa(showtb, kw, None, self.showtb) if callable(msg): # si msg est callable, par défaut ne pas mettre le résultat en