2022-02-15 20:03:45 +01:00
|
|
|
import os, sys, json
|
2022-03-19 11:42:31 +01:00
|
|
|
#TODO: port to QT once that little mainloop issue has been resolved...
|
2022-02-15 20:03:45 +01:00
|
|
|
import tkinter as tk
|
|
|
|
from tkinter import ttk
|
2022-03-19 11:42:31 +01:00
|
|
|
import util
|
2022-02-15 20:03:45 +01:00
|
|
|
|
2022-02-15 20:38:33 +01:00
|
|
|
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):
|
2022-02-15 20:03:45 +01:00
|
|
|
try:
|
2022-02-15 20:38:33 +01:00
|
|
|
config_file = open(self.__file_path, "r")
|
|
|
|
self.__current_config = json.loads(config_file.read())
|
2022-02-15 20:03:45 +01:00
|
|
|
config_file.close()
|
|
|
|
except:
|
2022-02-15 20:38:33 +01:00
|
|
|
util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False)
|
2022-02-15 20:03:45 +01:00
|
|
|
else:
|
2022-02-15 20:38:33 +01:00
|
|
|
# config not found
|
2022-03-19 11:42:31 +01:00
|
|
|
dialog_communication = util.Communication()
|
2022-02-15 20:38:33 +01:00
|
|
|
|
|
|
|
dialog = tk.Tk()
|
|
|
|
dialog.title("No configuration found")
|
|
|
|
ttk.Label(dialog, text="No configuration found!").pack()
|
|
|
|
buttons_frame = tk.Frame(dialog)
|
|
|
|
buttons_frame.pack()
|
2022-03-19 11:42:31 +01:00
|
|
|
ttk.Button(buttons_frame, text="Create", command=lambda: dialog_communication.send("create", True, additional_action=dialog.destroy)).grid(column=0, row=0)
|
|
|
|
ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_communication.send("create", False, additional_action=dialog.destroy)).grid(column=1, row=0)
|
2022-02-15 20:38:33 +01:00
|
|
|
dialog.resizable(0,0)
|
|
|
|
dialog.mainloop()
|
2022-02-15 20:03:45 +01:00
|
|
|
|
2022-03-19 11:42:31 +01:00
|
|
|
if dialog_communication.get("create"):
|
2022-02-15 20:38:33 +01:00
|
|
|
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]
|
2022-02-15 20:03:45 +01:00
|
|
|
|
2022-02-15 20:38:33 +01:00
|
|
|
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.")
|