Nearly done rework of the whole application to add groups

This commit is contained in:
2026-07-11 22:06:18 -04:00
parent 8db9f3bd9f
commit d8a071a965
6 changed files with 251 additions and 136 deletions

View File

@@ -2,80 +2,124 @@ import sqlite3
import json
import sys
destroy_table = r'''
destroy_button_table = r'''
DROP TABLE IF EXISTS buttons
'''
make_table = r'''
destroy_group_table = r'''
DROP TABLE IF EXISTS groups
'''
make_button_table = r'''
CREATE TABLE IF NOT EXISTS buttons
(button_name TEXT PRIMARY KEY,
(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)))
'''
insert_row = r'''
INSERT INTO buttons VALUES
(?, ?, ?, ?, ?)
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())
config_issues = ""
if not "instance_name" in config_json.keys():
config_issues = config_issues + "The config item instance_name is required\n"
print("The config item instance_name is required")
for i in range(len(config_json["buttons"])):
if not "name" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required name parameter\n".format(index = (i + 1))
recursive_data_check(config_json["data"], "Home")
if not "video_url" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required video_url parameter\n".format(index = (i + 1))
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 not "default" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required default parameter\n".format(index = (i + 1))
all_defaults = [element for element in config_json["buttons"] if element["default"]]
if len(all_defaults) > 1:
config_issues = config_issues + "More than one button config is set as default, only one can be the default, remove one of these: {button_list}\n".format(
button_list = ", ".join([element["name"] if "name" in element else "NAME MISSING" for element in all_defaults])
)
elif len(all_defaults) == 0:
config_issues = config_issues = "At least one button config must be set as the default\n"
if len(config_issues) != 0:
print("Config issues detected! Please correct these issues before trying again")
print(config_issues)
if failed_a_test:
sys.exit(1)
db = sqlite3.connect(config_json["db_name"])
db.execute(destroy_table)
db.execute(make_table)
db.execute(destroy_button_table)
db.execute(destroy_group_table)
db.execute(make_button_table)
db.execute(make_group_table)
db.execute(insert_row, [
config_json["disabled_name"],
"",
"",
0,
0
])
db.commit()
for button in config_json["buttons"]:
db.execute(insert_row, [
button["name"],
button["video_url"],
" ".join(button["video_tool_arguments"]) if len(button["video_tool_arguments"]) != 0 else "",
1 if button["default"] else 0,
1 if button["default"] else 0
])
db.commit()
recursive_insert(db, config_json["data"], "Home")
db.close()