# -*- coding: utf-8 -*-
"""
Created on Sat Jan 21 14:37:10 2017

@author: PC Alexandre.Ribault
"""


from Tkinter import *
from tkFileDialog import *
import csv


""" Emplacement à modifier pour tester sur votre machine"""

filename='ListeDesFormations.csv'


"""
------------------------------------------------------------------------------------
|                                                                                  | 
|                                                                                  | 
|                           Definition des methodes                                |
|                                                                                  | 
|                                                                                  | 
------------------------------------------------------------------------------------
"""
def csvcount(filename):
    i=0
    Tableur= open(filename,"rb")
    for row in Tableur:
        i+=1
    Tableur.close()
    return i

def txtcount(filename):
    i=0
    FichierTexte= open(filename,"rb")
    while 1:
        data=FichierTexte.readline()
        if not data:
            break
    
        else :
            i+=1
    FichierTexte.close()
    return i
    
def trie(string,filenameSource,filenameDestination):
    FichierSource= open(filenameSource,"rb")
    FichierDestination= open(filenameDestination,"wb")
    while 1:
        data=FichierSource.readline()
        if not data:
            break  
        else :
            if data.find(string)>0:
                FichierDestination.write(data)
              
    FichierSource.close()
    FichierDestination.close()
    Copie(filenameDestination,filenameSource)
    FichierDestination= open(filenameDestination,"rb")
    contenu= FichierDestination.read()
    print contenu
    FichierDestination.close()
       
def triebarre(string,filenameSource,filenameDestination):
    FichierSource= open(filenameSource,"rb")
    FichierDestination= open(filenameDestination,"wb")
    while 1:
        data=FichierSource.readline()
        if not data:
            break  
        else :
            if data.find(string)<0:
                FichierDestination.write(data)
              
    FichierSource.close()
    FichierDestination.close()
    Copie(filenameDestination,filenameSource)
    FichierDestination= open(filenameDestination,"rb")
    contenu= FichierDestination.read()
    print contenu
    FichierDestination.close()
    
def NbOccurence(string,filenameSource):
    i=0
    FichierSource= open(filenameSource,"rb")
    while 1:
        data=FichierSource.readline()
        if not data:
            break  
        else :
            if data.find(string)>0:
                i=i+1
    FichierSource.close()
    return i

    
def Copie(filenameSource,filenameDestination):
    FichierSource= open(filenameSource,"rb")
    FichierDestination= open(filenameDestination,"wb")
    while 1:
        data=FichierSource.readline()
        if not data:
            break
        else :
            FichierDestination.write(data)

    FichierSource.close()
    FichierDestination.close()

def conversion(filenameSource,filenameDestination):
    FichierSource= open(filenameSource,"rb")
    FichierDestination= open(filenameDestination,"wb")
    TableauStr =csvcount(filenameSource) *[0]   
    iTStr=0                                         
    Tableau= csv.reader(FichierSource,delimiter=";")   
    for row in FichierSource:
        FichierDestination.write(row)
    FichierSource.close()
    FichierDestination.close()
    

def filtre (filenameSource):
    FichierSource=open(filenameSource, "rb")
    contenu=FichierSource.read() #Lit tout le fichier d'un coup
    index = { ';' :' '}
    FichierSource.close()
    
    for cle in index:
        contenu=contenu.replace(cle, index[cle])
    FichierSource=open(filenameSource, "wb")
    FichierSource.write(contenu)
    FichierSource.close()

def movey(*args):
    if args[0] == "scroll":
        can.yview_scroll(args[1],"unit")
        
def movex(*args):
    if args[0] == "scroll":
        can.xview_scroll(args[1],"unit")

def lancerrecherche():
    conversion("ListeDesFormations.csv","Formation.txt")
    filtre ("Formation.txt")
    print EtatCheck11.get() 
    if  ((EtatCheck11.get()==1)&(EtatCheck12.get()==1)):
        print " mot "+ entree1.get() +" non prit en compte"
    else:
        if (entree1.get()!=''):
            if (EtatCheck11.get()==1):
                trie(entree1.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck12.get()==1):
                triebarre(entree1.get(),"Formation.txt","Formationalire.txt")
                
    if  ((EtatCheck21.get()==1)&(EtatCheck22.get()==1)):
        print " mot "+ entree1.get() +" non prit en compte"
    else:
        if (entree2.get()!=''):
            if (EtatCheck21.get()==1):
                trie(entree2.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck22.get()==1):
                triebarre(entree2.get(),"Formation.txt","Formationalire.txt")
    if  ((EtatCheck31.get()==1)&(EtatCheck32.get()==1)):
        print " mot "+ entree4.get() +" non prit en compte"
    else:
        if (entree3.get()!=''):
            if (EtatCheck31.get()==1):
                trie(entree3.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck32.get()==1):
                triebarre(entree3.get(),"Formation.txt","Formationalire.txt")
                
    if  ((EtatCheck41.get()==1)&(EtatCheck42.get()==1)):
        print " mot "+ entree4.get() +" non prit en compte"
    else:
        if (entree4.get()!=''):
            if (EtatCheck41.get()==1):
                trie(entree4.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck42.get()==1):
                triebarre(entree4.get(),"Formation.txt","Formationalire.txt")
                
    if  ((EtatCheck51.get()==1)&(EtatCheck52.get()==1)):
        print " mot "+ entree5.get() +" non prit en compte"
    else:
        if (entree5.get()!=''):
            if (EtatCheck51.get()==1):
                trie(entree5.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck52.get()==1):
                triebarre(entree5.get(),"Formation.txt","Formationalire.txt")
                
    if  ((EtatCheck61.get()==1)&(EtatCheck62.get()==1)):
        print " mot "+ entree6.get() +" non prit en compte"
    else:
        if (entree2.get()!=''):
            if (EtatCheck61.get()==1):
                trie(entree6.get(),"Formation.txt","Formationalire.txt")
            if  (EtatCheck62.get()==1):
                triebarre(entree6.get(),"Formation.txt","Formationalire.txt")
    if (Frame4_entree1.get()!=''):
        OccFrame4_1.set(NbOccurence(Frame4_entree1.get(),"Formation.txt"))
    if (Frame4_entree2.get()!=''):
        OccFrame4_2.set(NbOccurence(Frame4_entree2.get(),"Formation.txt"))
    if (Frame4_entree3.get()!=''):    
        OccFrame4_3.set(NbOccurence(Frame4_entree3.get(),"Formation.txt"))
    if (Frame4_entree4.get()!=''):
        OccFrame4_4.set(NbOccurence(Frame4_entree4.get(),"Formation.txt"))
    if (Frame4_entree5.get()!=''):
        OccFrame4_5.set(NbOccurence(Frame4_entree5.get(),"Formation.txt"))
    if (Frame4_entree6.get()!=''):    
        OccFrame4_6.set(NbOccurence(Frame4_entree6.get(),"Formation.txt"))
    
    fichierSource=open("Formation.txt","rb")
    contenu= fichierSource.read()    
    fichierSource.close()
    Nombredeformation= str( txtcount("Formation.txt"))
    can.insert(0.0, '----Nouvelle recherche----\n\n\n'+ " Nombre de formation(s) :" + Nombredeformation + '\n'  + contenu + '\n\n\n\n----Fin de la nouvelle recherche----\n\n')
    #Soit ' + txtcount("Formation.txt") + 'formation(s)' +'    
    txtcount("Formation.txt")

"""
------------------------------------------------------------------------------------
|                                                                                  | 
|                                                                                  | 
|                           Création de la fenêtre                                 |
|                                                                                  | 
|                                                                                  | 
------------------------------------------------------------------------------------
"""
fenetre = Tk()

""""""   # 1er panneau 
PetiteFrame1 = PanedWindow(fenetre,bd=2,relief=GROOVE)
PetiteFrame1.grid(row=0,column=0,ipady=5,ipadx=37,sticky='NS')
Label(PetiteFrame1, text="Chemin du fichier source:").grid(row=0, column=0)

bouton=Button(PetiteFrame1, text="Parcourir", command = fenetre.destroy,width=20)
bouton.grid(row=1,column=0,padx=0,pady=0,)
filename='D:/ProgPython/ListeDesFormations.csv'
labeldansFrame1 = Label(PetiteFrame1, text=filename)
labeldansFrame1.grid(row=1,column=1,padx=0,pady=0)

conversion(filename,"Formation.txt")
filtre ("Formation.txt")
""""""   # 2eme panneau 

PetiteFrame2 = PanedWindow(fenetre,bd=2,relief=GROOVE,height=600,)
PetiteFrame2.grid(row=1,rowspan=4,column=0,sticky='NS')
Label(PetiteFrame2, text="Insertion(s) mot(s) clé(s): ").grid(row=0, column=0,padx=10)

Tvarentree1 = StringVar() 
entree1 = Entry(PetiteFrame2, textvariable=Tvarentree1,width= 50,)
entree1.grid(row=1,column=0,pady=15)
Tvarentree2 = StringVar() 
entree2 = Entry(PetiteFrame2, textvariable=Tvarentree2,width= 50)
entree2.grid(row=2,column=0,pady=10)
Tvarentree3 = StringVar()
entree3 = Entry(PetiteFrame2, textvariable=Tvarentree3,width= 50)
entree3.grid(row=3,column=0,pady=10)
Tvarentree4 = StringVar()
entree4 = Entry(PetiteFrame2, textvariable=Tvarentree4,width= 50)
entree4.grid(row=4,column=0,pady=10)
Tvarentree5 = StringVar()
entree5 = Entry(PetiteFrame2, textvariable=Tvarentree5,width= 50)
entree5.grid(row=5,column=0,pady=10)
Tvarentree6 = StringVar()
entree6 = Entry(PetiteFrame2, textvariable=Tvarentree6,width= 50)
entree6.grid(row=6,column=0,pady=10)

Label(PetiteFrame2, text=" Garder ").grid(row=0, column=1,padx=10)
EtatCheck11 = IntVar()
Check11=Checkbutton(PetiteFrame2,text='',variable=EtatCheck11)
Check11.grid(row=1,column=1,pady=10,)
EtatCheck21 = IntVar()
Check21=Checkbutton(PetiteFrame2,text='',variable=EtatCheck21)
Check21.grid(row=2,column=1,pady=10)
EtatCheck31 = IntVar()
Check31=Checkbutton(PetiteFrame2,text='',variable=EtatCheck31)
Check31.grid(row=3,column=1,pady=10)
EtatCheck41 = IntVar()
Check41=Checkbutton(PetiteFrame2,text='',variable=EtatCheck41)
Check41.grid(row=4,column=1,pady=10)
EtatCheck51 = IntVar()
Check51=Checkbutton(PetiteFrame2,text='',variable=EtatCheck51)
Check51.grid(row=5,column=1,pady=10)
EtatCheck61 = IntVar()
Check61=Checkbutton(PetiteFrame2,text='',variable=EtatCheck61)
Check61.grid(row=6,column=1,pady=10)

Label(PetiteFrame2, text=" Supprimer ").grid(row=0, column=2)
EtatCheck12= IntVar()
Check12=Checkbutton(PetiteFrame2,text='',variable=EtatCheck12)
Check12.grid(row=1,column=2,pady=10)
EtatCheck22= IntVar()
Check22=Checkbutton(PetiteFrame2,text='',variable=EtatCheck22)
Check22.grid(row=2,column=2,pady=10)
EtatCheck32= IntVar()
Check32=Checkbutton(PetiteFrame2,text='',variable=EtatCheck32)
Check32.grid(row=3,column=2,pady=10)
EtatCheck42= IntVar()
Check42=Checkbutton(PetiteFrame2,text='',variable=EtatCheck42)
Check42.grid(row=4,column=2,pady=10)
EtatCheck52= IntVar()
Check52=Checkbutton(PetiteFrame2,text='',variable=EtatCheck52)
Check52.grid(row=5,column=2,pady=10)
EtatCheck62= IntVar()
Check62=Checkbutton(PetiteFrame2,text='',variable=EtatCheck62)
Check62.grid(row=6,column=2,pady=10)

""""""   # 3eme panneau 
         
fichierSource=open("Formation.txt","rb")
contenu= fichierSource.read()
fichierSource.close()

PetiteFrame3 = PanedWindow(fenetre,bd=2,relief=GROOVE)
PetiteFrame3.grid(row=0,rowspan=3,column=1,columnspan=3)
can = Text(PetiteFrame3,bg="white")
can.grid(row=0,column=0,ipadx=375)
can.insert(END, contenu)
scly = Scrollbar(PetiteFrame3,command=movey,)
scly.grid(row=0,column=1,sticky='NS')
sclx = Scrollbar(PetiteFrame3,command=movex,orient='horizontal')
sclx.grid(row=1,column=0,sticky='EW')

""""""   #4eme panneau

PetiteFrame4 = PanedWindow(fenetre,bd=2,relief=GROOVE,height=600)
PetiteFrame4.grid(row=4,column=1,columnspan=2,sticky='EW')
Label(PetiteFrame4, text="Occurrence(s) ").grid(row=0, column=1,padx=10)
Label(PetiteFrame4, text="Occurrence(s) ").grid(row=0, column=3,padx=10)

OccFrame4_1 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_1).grid(row=1, column=1,padx=10)
OccFrame4_2 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_2).grid(row=2, column=1,padx=10)
OccFrame4_3 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_3).grid(row=3, column=1,padx=10)
OccFrame4_4 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_4).grid(row=1, column=3,padx=10)
OccFrame4_5 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_5).grid(row=2, column=3,padx=10)
OccFrame4_6 = StringVar() 
Label(PetiteFrame4, textvariable=OccFrame4_6).grid(row=3, column=3,padx=10)


TvarentreeFrame4_1 = StringVar() 
Frame4_entree1 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_1,width= 30)
Frame4_entree1.grid(row=1,column=0,pady=15)
TvarentreeFrame4_2 = StringVar() 
Frame4_entree2 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_2,width= 30)
Frame4_entree2.grid(row=2,column=0,pady=10)
TvarentreeFrame4_3= StringVar()
Frame4_entree3 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_3,width= 30)
Frame4_entree3.grid(row=3,column=0,pady=10)
TvarentreeFrame4_4= StringVar()
Frame4_entree4 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_4,width= 30)
Frame4_entree4.grid(row=1,column=2,pady=10)
TvarentreeFrame4_5= StringVar()
Frame4_entree5 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_5,width= 30)
Frame4_entree5.grid(row=2,column=2,pady=10)
TvarentreeFrame4_6=StringVar()
Frame4_entree6 = Entry(PetiteFrame4, textvariable=TvarentreeFrame4_6,width= 30)
Frame4_entree6.grid(row=3,column=2,pady=10)

""""""   #5eme panneau 
PetiteFrame5 = PanedWindow(fenetre,bd=2,relief=GROOVE)
PetiteFrame5.grid(row=4,column=3)
BoutonLancer=Button(PetiteFrame5, text="Lancer la recherche",command=lancerrecherche)
BoutonLancer.grid(row=0,column=0,padx=0,pady=0,)

fenetre.mainloop()


    
    
    