import re
from contextlib import closing
from glob import glob
from json import load
from os import path, listdir


def __find_files():
    # list folders in labels/
    for folder in glob('labels/*'):
        # list files
        for filename in listdir(folder):
            file_path = path.join(folder, filename)

            # filter configuration files
            if not path.isfile(file_path):
                continue

            if not re.match(r'^configuration(\d+)?\.json$', filename):
                continue

            # yield element
            yield folder, filename, file_path


def discover(database):
    """
    find label providers in the corresponding folder and add them to the database

    :param database:
    :return:
    """
    with closing(database.cursor()) as cursor:
        for folder, configuration_file, configuration_path in __find_files():
            # load configuration file
            with open(configuration_path, 'r') as file:
                label = load(file)

            # extract data
            name = label['name']
            description = label['description'] if 'description' in label else None

            # save to database
            cursor.execute('''
                INSERT INTO label_providers (name, description, root_folder, configuration_file)
                VALUES (?, ?, ?, ?)
                ON CONFLICT (root_folder, configuration_file)
                DO UPDATE SET name = ?, description = ?
            ''', (name, description, folder, configuration_file, name, description))
