import re
import glob


def find_files(input_path, framerange=None):
    """
    Discovers files on the filesystem.
    :param input_path: Path to the file sequence
    :param framerange: optional framerange
    :return: array of single file paths
    """
    files = []
    if '%' not in input_path:
        return [input_path]
    if framerange:
        for part_range in framerange.split(','):
            if '-' in part_range:
                first, last = part_range.split('-')
                for i in range(int(first), int(last) + 1):
                    files.append(input_path % i)
            else:
                files.append(input_path % int(part_range))
    else:
        input_path = re.sub(r'(\%0[4-8]d)(\.[exr|EXR])', r'*\2', input_path)
        files = glob.glob(input_path)
    files = sorted(files)
    return files