import sqlite3 import json import sys destroy_button_table = r''' DROP TABLE IF EXISTS buttons ''' destroy_group_table = r''' DROP TABLE IF EXISTS groups ''' make_button_table = r''' CREATE TABLE IF NOT EXISTS buttons (button_name TEXT PRIMARY KEY, page_group TEXT NOT NULL, video_url TEXT NOT NULL, video_arguments TEXT NOT NULL, is_default BOOLEAN NOT NULL CHECK (is_default IN (0, 1)), is_active BOOLEAN NOT NULL CHECK (is_active IN (0, 1))) ''' make_group_table = r''' CREATE TABLE IF NOT EXISTS groups (group_name TEXT PRIMARY KEY, parent_group TEXT NOT NULL ) ''' insert_button_row = r''' INSERT INTO buttons VALUES (?, ?, ?, ?, ?, ?) ''' insert_group_row = r''' INSERT INTO groups VALUES (?, ?) ''' default_count = 0 failed_a_test = False def recursive_data_check(next_list: list, current_group_name: str): global default_count global failed_a_test for element in next_list: if not "type" in element.keys(): print(f"An element in {current_group_name} is missing the type key") failed_a_test = True if not "name" in element.keys(): print(f"An element in {current_group_name} with type {element["type"]} is missing the name key") failed_a_test = True if element["type"] == "Group": if not "data" in element.keys(): print(f"The element in {current_group_name} with name {element["name"]} is missing the data key") failed_a_test = True recursive_data_check(element["data"], element["name"]) else: if not "video_url" in element.keys(): print(f"The element in {current_group_name} with name {element["name"]} is missing the video_url key") failed_a_test = True if not "video_tool_arguments" in element.keys(): print(f"The element in {current_group_name} with name {element["name"]} is missing the video_tool_arguments key") failed_a_test = True if not "default" in element.keys(): print(f"The element in {current_group_name} with name {element["name"]} is missing the default key") failed_a_test = True if element["default"]: default_count = default_count + 1 def recursive_insert(db: sqlite3.Connection, next_list: list, current_group_name: str): for element in next_list: if element["type"] == "Group": db.execute(insert_group_row, [ element["name"], current_group_name ]) db.commit() recursive_insert(db, element["data"], element["name"]) else: db.execute(insert_button_row, [ element["name"], current_group_name, element["video_url"], " ".join(element["video_tool_arguments"]) if len(element["video_tool_arguments"]) != 0 else "", 1 if element["default"] else 0, 1 if element["default"] else 0 ]) db.commit() with open("config.json", "r") as config_file: config_json = json.loads(config_file.read()) if not "instance_name" in config_json.keys(): print("The config item instance_name is required") recursive_data_check(config_json["data"], "Home") if default_count > 1: print(f"There can only be one default button configured, current number configured: {default_count}") failed_a_test = True elif default_count == 0: print("There must be one default button configured, currently there are none") if failed_a_test: sys.exit(1) db = sqlite3.connect(config_json["db_name"]) db.execute(destroy_button_table) db.execute(destroy_group_table) db.execute(make_button_table) db.execute(make_group_table) recursive_insert(db, config_json["data"], "Home") db.close()