From 3f2e50a5ddc2a912ddd053166b756b5dfeee60c1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 13 Oct 2022 12:17:31 -0700 Subject: [PATCH] Add config compare to not overwrite user changes --- app/classes/shared/helpers.py | 32 +++++++++++++++++++ app/classes/shared/main_controller.py | 46 +++++++++++++++++++++++++++ main.py | 1 + 3 files changed, 79 insertions(+) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index c4d9ad10..7202caa6 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -376,6 +376,38 @@ class Helpers: return default_return + def set_settings(self, data): + try: + with open(self.settings_file, "w", encoding="utf-8") as f: + json.dump(data, f, indent=4) + + except Exception as e: + logger.critical( + f"Config File Error: Unable to read {self.settings_file} due to {e}" + ) + Console.critical( + f"Config File Error: Unable to read {self.settings_file} due to {e}" + ) + return False + + return True + + def get_all_settings(self): + try: + with open(self.settings_file, "r", encoding="utf-8") as f: + data = json.load(f) + + except Exception as e: + data = {} + logger.critical( + f"Config File Error: Unable to read {self.settings_file} due to {e}" + ) + Console.critical( + f"Config File Error: Unable to read {self.settings_file} due to {e}" + ) + + return data + def set_setting(self, key, new_value): try: with open(self.settings_file, "r", encoding="utf-8") as f: diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index f61941ca..7ed949bc 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -243,6 +243,52 @@ class Controller: exec_user["user_id"], "support_status_update", results ) + def get_config_diff(self): + master_config = { + "config_ver": 1, + "http_port": 8000, + "https_port": 8443, + "language": "en_EN", + "cookie_expire": 30, + "cookie_secret": "random", + "apikey_secret": "random", + "show_errors": True, + "history_max_age": 7, + "stats_update_frequency": 30, + "delete_default_json": False, + "show_contribute_link": True, + "virtual_terminal_lines": 70, + "max_log_lines": 700, + "max_audit_entries": 300, + "disabled_language_files": ["lol_EN.json", ""], + "stream_size_GB": 1, + "keywords": ["help", "chunk"], + "allow_nsfw_profile_pictures": False, + "enable_user_self_delete": False, + } + user_config = self.helper.get_all_settings() + try: + if user_config["config_ver"] == master_config["config_ver"]: + return user_config + else: + user_config["config_ver"] = master_config["config_ver"] + except: + logger.debug("No config version found") + items_to_del = [] + + for key in user_config: + if key not in master_config.keys(): + items_to_del.append(key) + + for item in items_to_del[:]: + del user_config[item] + + for key, value in master_config.items(): + if key not in user_config.keys(): + user_config[key] = value + + self.helper.set_settings(user_config) + def send_log_status(self): try: return self.log_stats diff --git a/main.py b/main.py index b6fdd89f..60ad4409 100644 --- a/main.py +++ b/main.py @@ -159,6 +159,7 @@ if __name__ == "__main__": logger.info("Initializing all servers defined") Console.info("Initializing all servers defined") controller.servers.init_all_servers() + controller.get_config_diff() def tasks_starter(): # start stats logging