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
import gui_helper, util
def load_configuration(default_config, file_path):
if os.path.isfile(file_path):
try:
config_file = open(file_path, "r")
global configuration
configuration = json.loads(config_file.read())
config_file.close()
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"):
class Config:
def __init__(self, file_path, default_config):
self.__file_path = file_path
self.__default_config = default_config
self.__current_config = {}
if os.path.isfile(file_path):
try:
config_file = open(file_path, "w")
config_file.write(json.dumps(default_config))
config_file = open(self.__file_path, "r")
self.__current_config = json.loads(config_file.read())
config_file.close()
except:
util.warn("Failed to save initial config file.", is_exception=True)
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
util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False)
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)
# config not found
dialog_interaction_handler = gui_helper.Window_Interaction_Handler()
def get_configuration_value(config, default_config, key):
if not key in config:
util.info("Requested configuration value for "+str(key)+" not in configuration. Loading from default configuration.")
try:
config[key] = default_config[key]
except KeyError:
util.error("Requested an invalid configuration key.")
return None
return config[key]
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()
def set_configuration_value(file_path, config, key, value, save_to_disk=True):
if not key in config:
util.info("Writing configuration for previously unknown key "+str(key)+".")
config[key]=value
if save_to_disk:
try:
config_file = open(file_path, "w")
config_file.write(json.dumps(config))
config_file.close()
except:
util.error("Failed to save config file.")
if dialog_interaction_handler.get_result("create"):
self.__current_config = default_config
try:
config_file = open(self.__file_path, "w")
config_file.write(json.dumps(self.__current_config))
config_file.close()
except:
util.warn("Failed to save initial config file.", is_exception=True)
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 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 tkinter as tk
from tkinter import ttk
import config, gui_helper
import gui_helper
from config import Config
################################################################################
# CONSTANTS
@ -17,7 +18,7 @@ configuration_file_path = os.path.join(os.path.expanduser("~"), "some_ide_config
# PROGRAM STARTUP
################################################################################
configuration = config.load_configuration(default_configuration, configuration_file_path)
configuration = Config(configuration_file_path, default_configuration)
################################################################################
# PROGRAM MAIN WINDOW
@ -25,7 +26,7 @@ configuration = config.load_configuration(default_configuration, configuration_f
main_window = tk.Tk()
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
def rebuild_menu(structure_dict):
@ -36,7 +37,7 @@ def rebuild_menu(structure_dict):
rebuild_menu(gui_helper.menu_structure)
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.protocol("WM_DELETE_WINDOW", handle_exit)