#####################################################
# @module    file_utils.py                          #
# @brief     Common file_handling tools             #
# @author    Original Code form multiple authors    #
# @date      Sept 2020                              #
# @copyright Racelogic Ltd., 2020                   #
#####################################################

import sys
import os

def file_list(directory, extension):
    from os import listdir
    ext_len = len(extension) * -1
    try:
        ext = extension.upper()
        file_list = [f for f in listdir(directory) if f[ext_len:].upper()==ext]
    except:
        file_list = []
    return file_list

def check_directory_valid(path):
    from os import stat, mkdir
    try:
        stats = stat(path)
        ifmt = stats[0] & 0xF000
    except Exception as e:
        print (e)
        ifmt = None
    if ifmt is None:
        mkdir(path)
    else:
        if not ifmt == 0x4000:
            raise Exception('object exists at {}'.format(path))

def check_file_exists(path):
    from os import stat
    try:
        stats = stat(path)
        print ('stats {}'.format(stats))
        if (not stats[0] & 0x8000):
            raise Exception('not a file')
        return True
    except Exception as e:
        return False



#end