From e274be0864af38991c07ce9a45128057a83f52d5 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Mon, 23 Feb 2015 01:35:48 +0100 Subject: [PATCH] [gest_crans_lc] nettoyage de la fonction handle_exit_code --- gestion/gest_crans_lc.py | 61 ++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/gestion/gest_crans_lc.py b/gestion/gest_crans_lc.py index 8adb9ffd..2bc6f51a 100755 --- a/gestion/gest_crans_lc.py +++ b/gestion/gest_crans_lc.py @@ -13,33 +13,58 @@ Licence : GPLv3 ### --default-button pour choisir le bouton sélectionner par defaut ### --not-tags pour masquer les tags quand il ne servent à rien pour l'utilisateur (mais sont utilisé comme index par le programme) +import argparse import os import sys -import argparse +import subprocess + if '/usr/scripts' not in sys.path: sys.path.append('/usr/scripts') -import lc_ldap.objets as objets import lc_ldap.attributs as attributs +import lc_ldap.objets as objets from dialog import adherent, club, machine from dialog.CPS import TailCall, tailcaller from dialog.lc import main -def handle_exit_code(d, code): - """Gère les codes de retour dialog du menu principal""" - if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): - if code == d.DIALOG_CANCEL: - #msg = "Vous avez choisi Annuler dans la dernière fenêtre de dialogue.\n\n" \ - # "Voulez vous quitter le programme ?" - os.system('clear') - sys.exit(0) - else: - msg = "Vous avez appuyé sur ESC ou CTRL+C dans la dernière fenêtre de dialogue.\n\n" \ - "Voulez vous quitter le programme ?" - if d.yesno(msg, width=60) == d.DIALOG_OK: - os.system('clear') - sys.exit(0) + +def handle_dialog_exit_code(dialog, code): + """Gère les codes de retour du menu principal. + + Paramètres: + - ``dialog``: (pythondialog.Dialog) instance courante de l'objet Dialog + - ``code`` : (int) code de retour de la fenêtre Dialog + + Retourne: + - ``True`` quand dialog a quitté correctement (l'utilisateur a bien + saisi une valeur) + - ``False`` quand l'utilisateur annule la sortie du programme + (aucune valeur n'a été saisie) + + Lève les exceptions: + - :py:exc:`SystemExit` quand la sortie du programme est confirmée + + """ + + def exit_from_program(): + """ + Quitte le programme après avoir vidé la console + + Lève l'exception :py:exc:`SystemExit` + """ + subprocess.call('clear') + sys.exit(0) + + if code == dialog.DIALOG_CANCEL: + exit_from_program() + elif code == dialog.DIALOG_ESC: + msg = ( + "Vous avez appuyé sur ESC ou CTRL+C dans la dernière fenêtre" + " de dialogue.\n\nVoulez vous quitter le programme ?" + ) + if dialog.yesno(msg, width=60) == dialog.DIALOG_OK: + exit_from_program() return False else: return True @@ -143,10 +168,10 @@ class GestCrans(adherent.Dialog, club.Dialog, machine.Dialog): backtitle=self._connected_as(), choices=choices) - (code, tag) = self.handle_dialog(TailCall(handle_exit_code, self.dialog, self.dialog.DIALOG_ESC), box, tag) + (code, tag) = self.handle_dialog(TailCall(handle_dialog_exit_code, self.dialog, self.dialog.DIALOG_ESC), box, tag) self_cont = TailCall(self.menu_principal, tag=tag, proprio=proprio, machine=machine) callback = menu.get(tag, menu[''])['callback'] - if handle_exit_code(self.dialog, code) and callback: + if handle_dialog_exit_code(self.dialog, code) and callback: return TailCall(callback, cont=TailCall(self.menu_principal, tag=tag, machine=machine, proprio=proprio)) else: return self_cont