Seamlessly handle nonexistent configurations

This commit is contained in:
Mark Veidemanis 2022-07-28 21:11:01 +01:00
parent 3d67578179
commit 2f74d79bc4
1 changed files with 11 additions and 1 deletions

12
main.py
View File

@ -1,6 +1,7 @@
import json import json
import pickle import pickle
from os import urandom from os import urandom
from os.path import exists
from string import digits from string import digits
from redis import StrictRedis from redis import StrictRedis
@ -9,6 +10,7 @@ from redis import StrictRedis
ZNCErrors = ["Error:", "Unable to load", "does not exist", "doesn't exist"] ZNCErrors = ["Error:", "Unable to load", "does not exist", "doesn't exist"]
configPath = "conf/" configPath = "conf/"
exampleConfigPath = "conf/example"
certPath = "cert/" certPath = "cert/"
filemap = { filemap = {
@ -80,8 +82,16 @@ def saveConf(var):
def loadConf(var): def loadConf(var):
if filemap[var][2] == "json": 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) globals()[var] = json.load(f)
return
if var == "alias": if var == "alias":
# This is a workaround to convert all the keys into integers since JSON # This is a workaround to convert all the keys into integers since JSON
# turns them into strings... # turns them into strings...