ressuscite.py: ajout a un autre adherent, depuis un fichier, clarification
Je vais encore rajouter la recherche par conditions sur la machine ressuscitee mais apres je ne vois plus trop quoi rajouter ... darcs-hash:20080924133419-ddb99-27051bb18d13dba998052dff2fe1dadcd0ecedc7.gz
This commit is contained in:
parent
6ddb784b1b
commit
289e14df60
1 changed files with 225 additions and 117 deletions
|
@ -39,12 +39,15 @@ def dialog(arg):
|
||||||
return affich_tools.dialog(u'Ressusciter une machine', arg)
|
return affich_tools.dialog(u'Ressusciter une machine', arg)
|
||||||
|
|
||||||
def menu_principal():
|
def menu_principal():
|
||||||
|
"""Menu principal de l'interface."""
|
||||||
|
choix = ''
|
||||||
while 1:
|
while 1:
|
||||||
arg = u'--title "Ressusciter" '
|
arg = u'--title "Ressusciter" '
|
||||||
arg += u'--help-button --item-help --cancel-label "Quitter" '
|
arg += u'--item-help --cancel-label "Quitter" '
|
||||||
arg += u'--default-item "" '
|
arg += u'--default-item "%s" ' % choix
|
||||||
arg += u'--menu "Que souhaitez vous faire ?" 0 55 13 '
|
arg += u'--menu "Que souhaitez vous faire ?" 0 55 13 '
|
||||||
arg += u'"A" "Ressusciter à partir d\'un adhérent" "" '
|
arg += u'"A" "Ressusciter à partir d\'un adhérent" "Rechercher les anciennes machines d\'un adhérent" '
|
||||||
|
arg += u'"F" "Ressusciter à partir d\'un fichier" "Récupérer depuis un fichier du cimetière" '
|
||||||
annul, result = dialog(arg)
|
annul, result = dialog(arg)
|
||||||
if annul: break
|
if annul: break
|
||||||
|
|
||||||
|
@ -52,139 +55,244 @@ def menu_principal():
|
||||||
if choix == 'A':
|
if choix == 'A':
|
||||||
# Ressusciter à partir d'un adhérent
|
# Ressusciter à partir d'un adhérent
|
||||||
# On sélectionne l'adhérent
|
# On sélectionne l'adhérent
|
||||||
adh = select(db, u'Adhérent auquel rechercher les anciennes machines a', mde='ro')
|
adh = select(db, u'Adhérent duquel rechercher les anciennes machines a', mde='ro')
|
||||||
if not adh: continue
|
if not adh: continue
|
||||||
|
|
||||||
# On sélectionne le type de machine
|
# Est-ce que c'est bien une machine de l'adhérent ?
|
||||||
arg = u'--title "Recherche de machine" '
|
def condition(machine, date):
|
||||||
arg += u'--menu "Type de machine ?" 0 0 0 '
|
return (int(machine.dn.split(',', 3)[1].split('=', 2)[1])
|
||||||
arg += u'"Fixe" "Machine fixe" '
|
== int(adh.id()) and str(machine.proprio) == ("%s %s"
|
||||||
arg += u'"Wifi" "Machine wireless" '
|
% (adh.prenom(), adh.nom())))
|
||||||
arg += u'"Tous" "Rechercher les deux types" '
|
|
||||||
annul, result = dialog(arg)
|
|
||||||
if annul: continue
|
|
||||||
choix = result[0]
|
|
||||||
|
|
||||||
# On fait la liste des machines
|
choixmachine = choixrecherche(condition)
|
||||||
cimetiere = []
|
|
||||||
if choix == 'Fixe' or choix == 'Tous':
|
|
||||||
cimetiere = map(lambda s: "/home/cimetiere/MachineFixe/" + s,
|
|
||||||
os.listdir("/home/cimetiere/MachineFixe"))
|
|
||||||
if choix == 'Wifi' or choix == 'Tous':
|
|
||||||
cimetiere += map(lambda s: "/home/cimetiere/MachineWifi/" + s,
|
|
||||||
os.listdir("/home/cimetiere/MachineWifi"))
|
|
||||||
|
|
||||||
valid = []
|
if choixmachine != None:
|
||||||
for fichier in cimetiere:
|
(machine, date) = choixmachine
|
||||||
|
adh = choixadherent(machine, adh)
|
||||||
|
if not adh: continue
|
||||||
|
ressuscite(adh, machine)
|
||||||
|
elif choix == 'F':
|
||||||
|
# Ressusciter à partir d'un fichier
|
||||||
|
fichier = None
|
||||||
|
while 1:
|
||||||
|
arg = u'--title "Sélection du fichier" '
|
||||||
|
arg += u'--inputbox "Vous pouvez aussi exécuter le script sur le fichier :\nressuscite [le fichier]\n\nFichier : " 0 0 "%s"' % "/home/cimetiere/Machine"
|
||||||
|
annul, result = dialog(arg)
|
||||||
|
if annul: break
|
||||||
try:
|
try:
|
||||||
machine = cPickle.load(open(fichier, 'r'))
|
fichier = open(result[0], 'r')
|
||||||
# Est-ce que c'est bien une machine de l'adhérent ?
|
break
|
||||||
if (int(machine.dn.split(',', 3)[1].split('=', 2)[1])
|
|
||||||
== int(adh.id())
|
|
||||||
and str(machine.proprio) == ("%s %s"
|
|
||||||
% (adh.prenom(), adh.nom()))):
|
|
||||||
valid.append((machine, fichier[28:44]))
|
|
||||||
except:
|
except:
|
||||||
pass
|
arg = u'--title "Sélection du fichier" '
|
||||||
|
arg += u'--msgbox "Fichier invalide\n\n\n" 0 0'
|
||||||
|
dialog(arg)
|
||||||
|
continue
|
||||||
|
if fichier != None:
|
||||||
|
machine = cPickle.load(fichier)
|
||||||
|
adh = choixadherent(machine)
|
||||||
|
if not adh: continue
|
||||||
|
ressuscite(adh, machine)
|
||||||
|
|
||||||
# Pas de machines !
|
continue
|
||||||
if not valid:
|
|
||||||
arg = u'--title "Recherche" '
|
|
||||||
arg += u'--msgbox "Aucun résultat.\n\n\n" 0 0'
|
def choixrecherche(condition=None):
|
||||||
dialog(arg)
|
"""Demande les types de machines à chercher, et effectue la recherche avec
|
||||||
|
la condition."""
|
||||||
|
# On sélectionne le type de machine
|
||||||
|
arg = u'--title "Recherche de machine" '
|
||||||
|
arg += u'--menu "Type de machine ?" 0 0 0 '
|
||||||
|
arg += u'"Fixe" "Machine fixe" '
|
||||||
|
arg += u'"Wifi" "Machine wireless" '
|
||||||
|
arg += u'"Tous" "Rechercher les deux types" '
|
||||||
|
annul, result = dialog(arg)
|
||||||
|
if annul: return None
|
||||||
|
choix = result[0]
|
||||||
|
|
||||||
|
# On cherche les machines
|
||||||
|
machines = recherche(choix == 'Fixe' or choix == 'Tous',
|
||||||
|
choix == 'Wifi' or choix == 'Tous',
|
||||||
|
condition)
|
||||||
|
|
||||||
|
# Pas de résultat
|
||||||
|
if not machines:
|
||||||
|
arg = u'--title "Recherche" '
|
||||||
|
arg += u'--msgbox "Aucun résultat.\n\n\n" 0 0'
|
||||||
|
dialog(arg)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return choixmachine(machines)
|
||||||
|
|
||||||
|
|
||||||
|
def recherche(fixe=False, wifi=False, condition=None):
|
||||||
|
"""Recherche des machines du type demandé, et satisfaisant une condition :
|
||||||
|
la fonction condition doit être de la forme
|
||||||
|
condition(machine, date)
|
||||||
|
et retourner True ou False selon si l'on veut avoir la machine dans la
|
||||||
|
liste ou non."""
|
||||||
|
# Liste des fichiers à lire
|
||||||
|
cimetiere = []
|
||||||
|
if fixe: cimetiere = map(lambda s: "/home/cimetiere/MachineFixe/" + s,
|
||||||
|
os.listdir("/home/cimetiere/MachineFixe"))
|
||||||
|
if wifi: cimetiere += map(lambda s: "/home/cimetiere/MachineWifi/" + s,
|
||||||
|
os.listdir("/home/cimetiere/MachineWifi"))
|
||||||
|
|
||||||
|
# On regarde chacun des fichiers
|
||||||
|
valid = []
|
||||||
|
for fichier in cimetiere:
|
||||||
|
try:
|
||||||
|
machine = cPickle.load(open(fichier, 'r'))
|
||||||
|
date = fichier[28:44]
|
||||||
|
if condition == None or condition(machine, date):
|
||||||
|
valid.append((machine, date))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return valid
|
||||||
|
|
||||||
|
|
||||||
|
def choixadherent(machine, oldadh=None):
|
||||||
|
"""Permet de choisir l'adhérent à qui rattacher la machine."""
|
||||||
|
if oldadh == None:
|
||||||
|
oldadhl = db.search(machine.dn.split(',', 2)[1])['adherent']
|
||||||
|
else:
|
||||||
|
oldadhl = [oldadh]
|
||||||
|
|
||||||
|
arg = u'--title "Adhérent auquel rattacher la machine" '
|
||||||
|
arg += u'--menu "Le propriétaire de la machine est %s.' % machine.proprio
|
||||||
|
|
||||||
|
# Est-ce que l'adhérent existe encore ?
|
||||||
|
if (len(oldadhl) > 0 and str(machine.proprio) == "%s %s"
|
||||||
|
% (oldadhl[0].prenom(), oldadhl[0].nom())):
|
||||||
|
arg += u'\nCet adhérent existe encore dans la base." 0 0 0 '
|
||||||
|
arg += u'"Garder" "Ajouter à cet adhérent" '
|
||||||
|
arg += u'"Autre" "Ajouter à un autre adhérent" '
|
||||||
|
else:
|
||||||
|
arg += u'\nCet adhérent n\'existe plus dans la base." 0 0 0 '
|
||||||
|
arg += u'"Autre" "Ajouter à un autre adhérent" '
|
||||||
|
annul, result = dialog(arg)
|
||||||
|
if not annul:
|
||||||
|
choix = result[0]
|
||||||
|
if choix == "Garder":
|
||||||
|
adh = oldadhl[0]
|
||||||
|
else:
|
||||||
|
adh = select(db, u'Adhérent auquel ajouter la machine a', mde='ro')
|
||||||
|
else:
|
||||||
|
adh = None
|
||||||
|
return adh
|
||||||
|
|
||||||
|
|
||||||
|
def choixmachine(valid):
|
||||||
|
"""Choisit une machine dans une liste."""
|
||||||
|
# Pas de machines !
|
||||||
|
if len(valid) == 1:
|
||||||
|
# Une seule réponse
|
||||||
|
choix = valid[0]
|
||||||
|
else:
|
||||||
|
# Il faut choisir
|
||||||
|
while 1:
|
||||||
|
os.system('clear')
|
||||||
|
choix = None
|
||||||
|
print "Plusieurs réponses correspondant à votre requête ont été trouvées :"
|
||||||
|
|
||||||
|
data = []
|
||||||
|
i = 1
|
||||||
|
for (machine, date) in valid:
|
||||||
|
data.append([i, date, str(machine.__class__)[-4:], machine.nom().split('.', 1)[0], machine.mac()])
|
||||||
|
i += 1
|
||||||
|
print affich_tools.tableau(data,
|
||||||
|
titre=[u'id', u'Date destruction', u'Type', u'Nom de machine', u'Adresse MAC'],
|
||||||
|
largeur=[5, 16, 4, '*', 17],
|
||||||
|
alignement=['d', 'c', 'c', 'c', 'c'])
|
||||||
|
|
||||||
|
i = affich_tools.prompt(u'Votre choix ? (0 pour annuler) id =')
|
||||||
|
try:
|
||||||
|
i = int(i)
|
||||||
|
except:
|
||||||
|
print 'Choix invalide'
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(valid) == 1:
|
if i == 0: break
|
||||||
# Une seule réponse
|
if i > len(valid):
|
||||||
choix = valid[0]
|
print 'Choix invalide'
|
||||||
else:
|
|
||||||
# Il faut choisir
|
|
||||||
while 1:
|
|
||||||
os.system('clear')
|
|
||||||
choix = None
|
|
||||||
print "Plusieurs réponses correspondant à votre requête ont été trouvées :"
|
|
||||||
|
|
||||||
data = []
|
|
||||||
for (machine, date) in valid:
|
|
||||||
data.append([machine.id(), str(machine.__class__)[-4:], machine.nom().split('.', 1)[0], date])
|
|
||||||
print affich_tools.tableau(data,
|
|
||||||
titre=[u'mid', u'Type', u'Nom de machine', u'Date destruction'],
|
|
||||||
largeur=[5, 4, '*', 16],
|
|
||||||
alignement=['d', 'c', 'c', 'c'])
|
|
||||||
|
|
||||||
i = affich_tools.prompt(u'Votre choix ? (0 pour annuler) mid =')
|
|
||||||
if i == '0': break
|
|
||||||
for (m, d) in valid:
|
|
||||||
if m.id() == i:
|
|
||||||
choix = (m, d)
|
|
||||||
break
|
|
||||||
if not choix:
|
|
||||||
# Redemande le choix
|
|
||||||
print 'Choix invalide'
|
|
||||||
continue
|
|
||||||
if choix: break
|
|
||||||
|
|
||||||
if not choix:
|
|
||||||
# Retour à l'interface de recherche
|
|
||||||
continue
|
continue
|
||||||
|
choix = valid[i-1]
|
||||||
|
if choix: break
|
||||||
|
|
||||||
(oldmachine, date) = choix
|
if not choix:
|
||||||
if str(machine.__class__)[-4:] == 'Fixe':
|
# Retour à l'interface de recherche
|
||||||
machine = MachineFixe(adh)
|
return None
|
||||||
elif str(machine.__class__)[-4:] == 'Wifi':
|
return choix
|
||||||
machine = MachineWifi(adh)
|
|
||||||
else:
|
|
||||||
# On va quand même créer une machine ...
|
|
||||||
machine = MachineFixe(adh)
|
|
||||||
|
|
||||||
err = ""
|
|
||||||
|
|
||||||
# On remet le nom
|
def ressuscite(adh, oldmachine):
|
||||||
try: machine.nom(str(oldmachine.nom()))
|
"""Ressuscite une instance de machine."""
|
||||||
except ValueError, c: err += c.args[0] + '\n'
|
if str(oldmachine.__class__)[-4:] == 'Fixe':
|
||||||
except EnvironmentError, c: err += c.args[0] + '\n'
|
machine = MachineFixe(adh)
|
||||||
|
elif str(oldmachine.__class__)[-4:] == 'Wifi':
|
||||||
|
machine = MachineWifi(adh)
|
||||||
|
else:
|
||||||
|
# On va quand même créer une machine ...
|
||||||
|
machine = MachineFixe(adh)
|
||||||
|
|
||||||
# On remet la MAC
|
err = ""
|
||||||
try: machine.mac(str(oldmachine.mac()))
|
|
||||||
except ValueError, c:
|
|
||||||
if len(c.args)>1 and c.args[1] == 1 and isadm:
|
|
||||||
# Mac en double
|
|
||||||
arg = u'--title "Adresse MAC" '
|
|
||||||
arg += u'--yesno "L\'adresse MAC existe déjà, continuer ? \n" 0 0'
|
|
||||||
no, res = dialog(arg)
|
|
||||||
if not no:
|
|
||||||
try: machine.mac(str(oldmachine.mac()), 1)
|
|
||||||
except ValueError, c: err += c.args[0] + '\n'
|
|
||||||
except EnvironmentError, c: err += c.args[0] + '\n'
|
|
||||||
elif len(c.args)>1 and c.args[1] == 3 and isadm:
|
|
||||||
# Mac douteuse
|
|
||||||
arg = u'--title "Adresse MAC" '
|
|
||||||
arg += u'--yesno "L\'adresse MAC ne correspond à aucun constructeur, continuer ? \n" 0 0'
|
|
||||||
no, res = dialog(arg)
|
|
||||||
if not no:
|
|
||||||
try: machine.mac(str(oldmachine.mac()), 1)
|
|
||||||
except ValueError, c: err += c.args[0] + '\n'
|
|
||||||
except EnvironmentError, c: err += c.args[0] + '\n'
|
|
||||||
else:
|
|
||||||
err += c.args[0] + '\n'
|
|
||||||
except EnvironmentError, c: err += c.args[0] + '\n'
|
|
||||||
|
|
||||||
# On met une IP automatique
|
# On remet le nom
|
||||||
try: machine.ip('<automatique>')
|
try: machine.nom(str(oldmachine.nom()))
|
||||||
except ValueError, c: err += c.args[0] + '\n'
|
except ValueError, c: err += c.args[0] + '\n'
|
||||||
except EnvironmentError, c: err += c.args[0] + '\n'
|
except EnvironmentError, c: err += c.args[0] + '\n'
|
||||||
except RuntimeError, c: err += c.args[0] + '\n'
|
|
||||||
|
|
||||||
# Des erreurs ?
|
# On remet la MAC
|
||||||
if err:
|
try: machine.mac(str(oldmachine.mac()))
|
||||||
arg = u'--title "Paramètres machine" '
|
except ValueError, c:
|
||||||
arg += u'--msgbox "%s\n\n" 0 0' % err
|
if len(c.args)>1 and c.args[1] == 1 and isadm:
|
||||||
dialog(arg)
|
# Mac en double
|
||||||
set_machine(machine)
|
arg = u'--title "Adresse MAC" '
|
||||||
|
arg += u'--yesno "L\'adresse MAC existe déjà, continuer ? \n" 0 0'
|
||||||
|
no, res = dialog(arg)
|
||||||
|
if not no:
|
||||||
|
try: machine.mac(str(oldmachine.mac()), 1)
|
||||||
|
except ValueError, c: err += c.args[0] + '\n'
|
||||||
|
except EnvironmentError, c: err += c.args[0] + '\n'
|
||||||
|
elif len(c.args)>1 and c.args[1] == 3 and isadm:
|
||||||
|
# Mac douteuse
|
||||||
|
arg = u'--title "Adresse MAC" '
|
||||||
|
arg += u'--yesno "L\'adresse MAC ne correspond à aucun constructeur, continuer ? \n" 0 0'
|
||||||
|
no, res = dialog(arg)
|
||||||
|
if not no:
|
||||||
|
try: machine.mac(str(oldmachine.mac()), 1)
|
||||||
|
except ValueError, c: err += c.args[0] + '\n'
|
||||||
|
except EnvironmentError, c: err += c.args[0] + '\n'
|
||||||
|
else:
|
||||||
|
err += c.args[0] + '\n'
|
||||||
|
except EnvironmentError, c: err += c.args[0] + '\n'
|
||||||
|
|
||||||
continue
|
# On met une IP automatique
|
||||||
|
try: machine.ip('<automatique>')
|
||||||
|
except ValueError, c: err += c.args[0] + '\n'
|
||||||
|
except EnvironmentError, c: err += c.args[0] + '\n'
|
||||||
|
except RuntimeError, c: err += c.args[0] + '\n'
|
||||||
|
|
||||||
|
# Des erreurs ?
|
||||||
|
if err:
|
||||||
|
arg = u'--title "Paramètres machine" '
|
||||||
|
arg += u'--msgbox "%s\n\n" 0 0' % err
|
||||||
|
dialog(arg)
|
||||||
|
set_machine(machine)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
menu_principal()
|
menu_principal()
|
||||||
|
os.system("clear")
|
||||||
|
else:
|
||||||
|
for path in sys.argv[1:]:
|
||||||
|
try:
|
||||||
|
fichier = open(path, 'r')
|
||||||
|
machine = cPickle.load(fichier)
|
||||||
|
adh = choixadherent(machine)
|
||||||
|
if adh:
|
||||||
|
ressuscite(adh, machine)
|
||||||
|
except:
|
||||||
|
print "Fichier invalide : %s" % fichier
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue