make config object oriented

master
BodgeMaster 2022-02-15 20:38:33 +01:00
parent b6cfc46fe4
commit b5124686bf
2 changed files with 67 additions and 64 deletions

122
config.py
View File

@ -3,69 +3,71 @@ import tkinter as tk
from tkinter import ttk from tkinter import ttk
import gui_helper, util import gui_helper, util
def load_configuration(default_config, file_path): class Config:
if os.path.isfile(file_path): def __init__(self, file_path, default_config):
try: self.__file_path = file_path
config_file = open(file_path, "r") self.__default_config = default_config
global configuration self.__current_config = {}
configuration = json.loads(config_file.read())
config_file.close() if os.path.isfile(file_path):
return configuration
except Exception:
util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False)
return {}
else:
# config not found
dialog_interaction_handler = gui_helper.Window_Interaction_Handler()
dialog = tk.Tk()
dialog.title("No configuration found")
ttk.Label(dialog, text="No configuration found!").pack()
buttons_frame = tk.Frame(dialog)
buttons_frame.pack()
ttk.Button(buttons_frame, text="Create", command=lambda: dialog_interaction_handler.interact("create", True, additional_action=dialog.destroy)).grid(column=0, row=0)
ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_interaction_handler.interact("create", False, additional_action=dialog.destroy)).grid(column=1, row=0)
dialog.resizable(0,0)
dialog.mainloop()
if dialog_interaction_handler.get_result("create"):
try: try:
config_file = open(file_path, "w") config_file = open(self.__file_path, "r")
config_file.write(json.dumps(default_config)) self.__current_config = json.loads(config_file.read())
config_file.close() config_file.close()
except: except:
util.warn("Failed to save initial config file.", is_exception=True) util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False)
dialog = tk.Tk()
dialog.title("Failed to save initial config file")
ttk.Label(dialog, text="Failed to save the initial config file.\n" +
"The IDE can still start up, but it is likely that all changes to the configuration will be lost where they would be saved otherwise.").pack()
ttk.Button(dialog, text="Continue", command=dialog.destroy).pack()
dialog.resizable(0,0)
dialog.mainloop()
return default_config
else: else:
util.error("No config present and user chose not to create one. Exiting.", is_exception=False, handle_gracefully=True) # config not found
# exit with success exit code anyway because this is not a program failure dialog_interaction_handler = gui_helper.Window_Interaction_Handler()
sys.exit(util.EXIT_SUCCESS)
def get_configuration_value(config, default_config, key): dialog = tk.Tk()
if not key in config: dialog.title("No configuration found")
util.info("Requested configuration value for "+str(key)+" not in configuration. Loading from default configuration.") ttk.Label(dialog, text="No configuration found!").pack()
try: buttons_frame = tk.Frame(dialog)
config[key] = default_config[key] buttons_frame.pack()
except KeyError: ttk.Button(buttons_frame, text="Create", command=lambda: dialog_interaction_handler.interact("create", True, additional_action=dialog.destroy)).grid(column=0, row=0)
util.error("Requested an invalid configuration key.") ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_interaction_handler.interact("create", False, additional_action=dialog.destroy)).grid(column=1, row=0)
return None dialog.resizable(0,0)
return config[key] dialog.mainloop()
def set_configuration_value(file_path, config, key, value, save_to_disk=True): if dialog_interaction_handler.get_result("create"):
if not key in config: self.__current_config = default_config
util.info("Writing configuration for previously unknown key "+str(key)+".") try:
config[key]=value config_file = open(self.__file_path, "w")
if save_to_disk: config_file.write(json.dumps(self.__current_config))
try: config_file.close()
config_file = open(file_path, "w") except:
config_file.write(json.dumps(config)) util.warn("Failed to save initial config file.", is_exception=True)
config_file.close() dialog = tk.Tk()
except: dialog.title("Failed to save initial config file")
util.error("Failed to save config file.") ttk.Label(dialog, text="Failed to save the initial config file.\n" +
"The IDE can still run, but it is likely that all changes to the configuration will be lost where they would be saved otherwise.").pack()
ttk.Button(dialog, text="Continue", command=dialog.destroy).pack()
dialog.resizable(0,0)
dialog.mainloop()
else:
util.error("No config present and user chose not to create one. Exiting.", is_exception=False, handle_gracefully=True)
# exit with success exit code anyway because this is not a program failure
sys.exit(util.EXIT_SUCCESS)
def get_configuration_value(self, key):
if not key in self.__current_config:
util.info("Requested configuration value for "+str(key)+" not in configuration. Loading from default configuration.")
try:
self.__current_config[key] = self.__default_config[key]
except KeyError:
util.error("Requested an invalid configuration key.")
return None
return self.__current_config[key]
def set_configuration_value(self, key, value, save_to_disk=True):
if not key in self.__current_config:
util.info("Writing configuration for previously unknown key "+str(key)+".")
self.__current_config[key]=value
if save_to_disk:
try:
config_file = open(self.__file_path, "w")
config_file.write(json.dumps(self.__current_config))
config_file.close()
except:
util.error("Failed to save config file.")

View File

@ -2,7 +2,8 @@
import os import os
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
import config, gui_helper import gui_helper
from config import Config
################################################################################ ################################################################################
# CONSTANTS # CONSTANTS
@ -17,7 +18,7 @@ configuration_file_path = os.path.join(os.path.expanduser("~"), "some_ide_config
# PROGRAM STARTUP # PROGRAM STARTUP
################################################################################ ################################################################################
configuration = config.load_configuration(default_configuration, configuration_file_path) configuration = Config(configuration_file_path, default_configuration)
################################################################################ ################################################################################
# PROGRAM MAIN WINDOW # PROGRAM MAIN WINDOW
@ -25,7 +26,7 @@ configuration = config.load_configuration(default_configuration, configuration_f
main_window = tk.Tk() main_window = tk.Tk()
main_window.title("IDE") main_window.title("IDE")
main_window.geometry(config.get_configuration_value(configuration, default_configuration, "window geometry")) main_window.geometry(configuration.get_configuration_value("window geometry"))
menubar = None menubar = None
def rebuild_menu(structure_dict): def rebuild_menu(structure_dict):
@ -36,7 +37,7 @@ def rebuild_menu(structure_dict):
rebuild_menu(gui_helper.menu_structure) rebuild_menu(gui_helper.menu_structure)
def handle_exit(): def handle_exit():
config.set_configuration_value(configuration_file_path, configuration, "window geometry", main_window.geometry()) configuration.set_configuration_value("window geometry", main_window.geometry())
main_window.destroy() main_window.destroy()
main_window.protocol("WM_DELETE_WINDOW", handle_exit) main_window.protocol("WM_DELETE_WINDOW", handle_exit)