From a61ba7b9e1f45825170fc489e8c11ae83c132baf Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 28 Jul 2022 21:11:01 +0100 Subject: [PATCH] Seamlessly handle nonexistent configurations --- main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 42950b9..2854525 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import json import pickle from os import urandom +from os.path import exists from string import digits from redis import StrictRedis @@ -9,6 +10,7 @@ from redis import StrictRedis ZNCErrors = ["Error:", "Unable to load", "does not exist", "doesn't exist"] configPath = "conf/" +exampleConfigPath = "conf/example" certPath = "cert/" filemap = { @@ -80,8 +82,16 @@ def saveConf(var): def loadConf(var): if filemap[var][2] == "json": - with open(configPath + filemap[var][0], "r") as f: + filename = configPath + filemap[var][0] + if not exists(filename): + if var == "config": + filename = exampleConfigPath + filemap[var][0] + else: + globals()[var] = {} + return + with open(filename, "r") as f: globals()[var] = json.load(f) + return if var == "alias": # This is a workaround to convert all the keys into integers since JSON # turns them into strings...