"""Utils module.


"""
import os

def createfilepath(path, *paths):
    '''Spojeni cest k souboru
    '''
    return os.path.join(path, *paths)

def makedir(dirpath):
    '''Vytvoreni slozky
    '''
    if not os.path.exists(dirpath):
        os.mkdir(dirpath)

def filename(file):
    '''Ziskani jmena souboru
    '''
    return os.path.splitext(file)[0]

def findfiles(dirpath, ext):
    '''Hledani souboru
    '''
    filepath = []
    for file in os.listdir(dirpath):
        if file.endswith(ext):
            filepath.append(os.path.join(dirpath, file))
    return filepath

def deletedir(dirpath):
    '''Smazani slozky
    '''
    for root, dirs, files in os.walk(dirpath, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))

def cleandir(dirpath):
    '''Vycisteni slozky
    '''
    folder = os.getcwd() + '/' + dirpath
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print(e)

def cleanabsdir(dirpath):
    folder = dirpath
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print(e)

def savefig(name, fig):
    '''Ulozeni grafu
    '''
    if os.path.exists(os.getcwd() + name):
        os.remove(os.getcwd() + '/' + name)
    fig.savefig(os.getcwd() + '/' + name, dpi=300)

def deleteimage(name):
    '''Smazani obrazku
    '''
    if os.path.exists(os.getcwd() + name):
        os.remove(os.getcwd() + '/' + name)

