#!/usr/bin/python

fichier = './repertoire'

def ecrireDicDansFichier( repertoire, nomFichier ):
    try:
        fd = open( nomFichier, 'w' )
    except:
        import sys
        print "Erreur ouverture", sys.exc_type, sys.exc_value

    try:
        for nom, tel in repertoire.iteritems():
            fd.write( nom + ":" + str( tel ) + "\n" )
    except:
        import sys
        print "Erreur ecriture repertoire", sys.exc_type, sys.exc_value

    fd.close()

def lireFichierRep( nomFichier ):
    rep = {}
    try:
        fd = open( nomFichier, 'r' )
    except:
        import sys
        print "Erreur ouverture", sys.exc_type, sys.exc_value

    try:
        lines = fd.readlines()
        for l in lines:
            elem = l.split( ':' )
            rep[ elem[0] ] = (elem[1].split( '\n' ))[0]
    except:
        import sys
        print "Erreur lecture repertoire", sys.exc_type, sys.exc_value

    fd.close()
    return rep

def ajouterEntree( repertoire ):
	nom = raw_input( "Nom de la personne a ajouter : " )
	numero = int( raw_input( "Numero de telephone : " ) )
	repertoire[ nom ] = numero
        global fichier
        ecrireDicDansFichier( repertoire, fichier )

def afficherPersonne( repertoire ):
	nom = raw_input( "Nom de la personne a afficher : " )
	if nom in repertoire:
		print "Numero de " + nom + " : " + str( repertoire[nom] )
	else:
		print "Aucune entree ne correspond a " + nom

def nbNumeros( repertoire ):
	nb = len( repertoire )
	print "Il y a " + str( nb ) + " entrees dans le repertoire"

def afficherTout( repertoire ):
	for k, v in repertoire.iteritems():
		print k, ":", v

def supprimerEntree( repertoire ):
	nom = raw_input( "Nom de la personne a supprimer : " )
	if nom in repertoire:
		del repertoire[nom]
	else:
		print "Aucune entree ne correspond a " + nom
        global fichier
        ecrireDicDansFichier( repertoire, fichier )

def supprimerTout( repertoire ):
	repertoire = {}
        global fichier
        ecrireDicDansFichier( repertoire, fichier )

def menuUtilisateur():
	print "Choix de l\'action a effectuer :"
	print "1: Ajouter une entree dans le repertoire"
	print "2: Afficher le numero de telephone d'une personne"
	print "3: Afficher le nombre de numeros enregistres dans le repertoire"
	print "4: Afficher le contenu de tout le repertoire"
	print "5: Supprimer du repertoire une personne et son numero"
	print "6: Effacer tout le contenu du repertoire"
	print "0: Quitter le programme"
	choix = int( raw_input( "--> " ) )
	return choix

def main():
    global fichier
    repertoire = lireFichierRep( fichier )
    fin = False
    while fin == False:
        choix = menuUtilisateur()
        if choix == 1:
            ajouterEntree( repertoire )
        elif choix == 2:
            afficherPersonne( repertoire )
        elif choix == 3:
            nbNumeros( repertoire )
        elif choix == 4:
            afficherTout( repertoire )
        elif choix == 5:
            supprimerEntree( repertoire )
        elif choix == 6:
            supprimerTout( repertoire )
        elif choix == 0:
            fin = True
        else:
            print "erreur de saisie"

if __name__ == "__main__":
	main()
