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

View File

@ -3,17 +3,19 @@ import tkinter as tk
from tkinter import ttk
import gui_helper, util
def load_configuration(default_config, file_path):
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, "r")
global configuration
configuration = json.loads(config_file.read())
config_file = open(self.__file_path, "r")
self.__current_config = json.loads(config_file.read())
config_file.close()
return configuration
except Exception:
except:
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()
@ -29,43 +31,43 @@ def load_configuration(default_config, file_path):
dialog.mainloop()
if dialog_interaction_handler.get_result("create"):
self.__current_config = default_config
try:
config_file = open(file_path, "w")
config_file.write(json.dumps(default_config))
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 start up, but it is likely that all changes to the configuration will be lost where they would be saved otherwise.").pack()
"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()
return default_config
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(config, default_config, key):
if not key in config:
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:
config[key] = default_config[key]
self.__current_config[key] = self.__default_config[key]
except KeyError:
util.error("Requested an invalid configuration key.")
return None
return config[key]
return self.__current_config[key]
def set_configuration_value(file_path, config, key, value, save_to_disk=True):
if not key in config:
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)+".")
config[key]=value
self.__current_config[key]=value
if save_to_disk:
try:
config_file = open(file_path, "w")
config_file.write(json.dumps(config))
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)