import os

"""
La classe
"""

class Node:
    def __init__(self,nodeValue):
        self.subNodes = []
        self.value = nodeValue

    def addSubNode(self,node):
        self.subNodes.append(node)

    def construct(self,depth = 0):
        global affichage
        global line
        global first_file
        if self.subNodes:
            if line == 0:
                affichage += "\\begin{tikzpicture}\n"
                affichage += "\\node[right] at (0,0.15) {\\strut\\includegraphics{racine.png}~\\raisebox{3pt}{"+self.value.replace('_','\\_')+"}};\n"
            else:
                affichage += "\\node[right] at ("+str(0.5*depth)+",-"+str(0.5*line)+") {\\strut\\includegraphics{dossier.png}~\\raisebox{3pt}{"+self.value.replace('_','\\_')+"}};\n"
                affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*depth)+",-"+str(0.5*line)+");\n"
                if line > 1:
                    affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*(line-1))+");\n"
                else:
                    affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*(line-1))+");\n"
                first_file = True
        else:
            ext = self.value.split('.')[-1]
            if ext in dico_ext:
                icone = dico_ext[ext]
            else:
                icone = "file"
            affichage += "\\node[right] (icone) at ("+str(0.5*depth)+",-"+str(0.5*line)+") {\\strut\\includegraphics{"+icone+".png}~\\raisebox{3pt}{"+self.value.replace('_','\\_')+"}};\n"
            affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*depth)+",-"+str(0.5*line)+");\n"
            if first_file == True:
                if line > 1:
                    affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*(line-1)+0.2)+");\n"
                else:
                    affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*(depth-1)+0.25)+",0);\n"
                first_file = False
            else:
                affichage += "\\draw ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*(depth-1)+0.25)+",-"+str(0.5*(line-1))+");\n"
            
        for x in range(depth-1):
            affichage += "\\draw ("+str(0.5*x+0.25)+",-"+str(0.5*line)+") -- ("+str(0.5*x+0.25)+",-"+str(0.5*(line-1))+");\n"
        depth += 1
        line += 1
        for node in self.subNodes: node.construct(depth)
"""
Application
"""
def getDirectoryNode(path):
    node = Node(os.path.split(path)[1])
    
    if os.path.isdir(path):
        for itemName in os.listdir(path):
            fullPathName = os.path.join(path,itemName)
            node.addSubNode(getDirectoryNode(fullPathName))
    return node


"""
Conversion en LaTeX
"""

def create_latex(filename):
    document = "\\documentclass{standalone}\n"
    document += "\\usepackage{tikz}\\usetikzlibrary{calc,decorations.pathmorphing}\n"
    document += "\\begin{document}\n"
    document += "\\newsavebox{\\arbor}%\n"
    document += "\\begin{lrbox}{\\arbor}%\n"
    document += affichage
    document += "\\end{tikzpicture}\n"
    document += "\\end{lrbox}%\n"
    document += "\\begin{tikzpicture}\n"
    document += "\\node[inner sep=0pt,outer sep=0pt,fill=yellow!25,draw,thick,decorate, decoration={random steps,segment length=3pt,amplitude=1pt}] {\\usebox{\\arbor}};\n"
    document += "\\end{tikzpicture}\n"
    document += "\\end{document}"
    
    if os.path.isfile(filename+".tex"):
        os.remove(filename+".tex")
    
    fichier = open(filename+".tex","x")
    fichier.write(document)
    fichier.close()

    # compilation PdfLaTeX dans le répertoire courant

    cmd = "pdflatex  --shell-escape -synctex=1 -interaction=nonstopmode "+filename+".tex"
    os.system(cmd)
   

affichage = ''
line = 0
first_file = True
dico_ext = { 'png' : 'img' , 'jpg' : 'img' , 'gif' : 'img' , 'tiff' : 'img' , 'pdf' : 'pdf' }
tree = getDirectoryNode("exemple")
tree.construct()
create_latex('arborescence')

