from contextlib import closing from glob import glob from json import load, dumps from os import path def discover(database): """ find models in the corresponding folder and add them to the database :param database: :return: """ with closing(database.cursor()) as cursor: # list folders in models/ for folder in glob('models/*'): # load distribution.json with open(path.join(folder, 'configuration.json'), 'r') as file: model = load(file) # extract data name = model['name'] description = model['description'] if 'description' in model else None supports = dumps(model['supports']) # save to database cursor.execute(''' INSERT INTO models (name, description, root_folder, supports) VALUES (?, ?, ?, ?) ON CONFLICT (root_folder) DO UPDATE SET name = ?, description = ?, supports = ? ''', (name, description, folder, supports, name, description, supports))