2024-02-06 20:10:41 +00:00
|
|
|
from config.constants import configFilePathBase
|
2023-11-04 18:13:42 +00:00
|
|
|
from utils.dict import copyDict
|
2022-05-14 09:43:02 +00:00
|
|
|
from utils.logging import logger
|
2022-05-12 07:14:23 +00:00
|
|
|
import json
|
2022-05-14 09:43:02 +00:00
|
|
|
import models.config
|
2022-05-12 07:14:23 +00:00
|
|
|
import os
|
|
|
|
import time
|
2023-11-04 19:39:30 +00:00
|
|
|
import yaml
|
2022-05-12 07:14:23 +00:00
|
|
|
|
2022-05-14 09:43:02 +00:00
|
|
|
config: models.config.Config = {
|
2022-05-12 07:14:23 +00:00
|
|
|
"logging": {
|
|
|
|
"debug": True,
|
2022-05-22 04:37:09 +00:00
|
|
|
"writeToFile": False,
|
2022-05-12 07:14:23 +00:00
|
|
|
},
|
|
|
|
"display": {
|
2022-09-05 19:35:22 +00:00
|
|
|
"hideTotalTime": False,
|
2022-05-12 07:14:23 +00:00
|
|
|
"useRemainingTime": False,
|
|
|
|
"posters": {
|
|
|
|
"enabled": False,
|
|
|
|
"imgurClientID": "",
|
2024-02-10 07:12:58 +00:00
|
|
|
"maxSize": 256,
|
2022-05-12 07:14:23 +00:00
|
|
|
},
|
2022-08-25 20:37:50 +00:00
|
|
|
"buttons": [],
|
2022-05-12 07:14:23 +00:00
|
|
|
},
|
|
|
|
"users": [],
|
|
|
|
}
|
2023-11-04 19:39:30 +00:00
|
|
|
supportedConfigFileExtensions = {
|
|
|
|
"yaml": "yaml",
|
|
|
|
"yml": "yaml",
|
|
|
|
"json": "json",
|
|
|
|
}
|
|
|
|
configFileExtension = ""
|
|
|
|
configFileType = ""
|
|
|
|
configFilePath = ""
|
2022-05-12 07:14:23 +00:00
|
|
|
|
|
|
|
def loadConfig() -> None:
|
2023-11-05 05:24:45 +00:00
|
|
|
global configFileExtension, configFileType, configFilePath
|
|
|
|
doesFileExist = False
|
|
|
|
for i, (fileExtension, fileType) in enumerate(supportedConfigFileExtensions.items()):
|
2024-02-06 20:10:41 +00:00
|
|
|
doesFileExist = os.path.isfile(f"{configFilePathBase}.{fileExtension}")
|
2023-11-05 05:24:45 +00:00
|
|
|
isFirstItem = i == 0
|
|
|
|
if doesFileExist or isFirstItem:
|
2023-11-04 19:39:30 +00:00
|
|
|
configFileExtension = fileExtension
|
|
|
|
configFileType = fileType
|
2024-02-06 20:10:41 +00:00
|
|
|
configFilePath = f"{configFilePathBase}.{configFileExtension}"
|
2023-11-05 05:24:45 +00:00
|
|
|
if doesFileExist:
|
|
|
|
break
|
|
|
|
if doesFileExist:
|
2022-05-12 07:14:23 +00:00
|
|
|
try:
|
|
|
|
with open(configFilePath, "r", encoding = "UTF-8") as configFile:
|
2023-11-04 19:39:30 +00:00
|
|
|
if configFileType == "yaml":
|
|
|
|
loadedConfig = yaml.safe_load(configFile) or {}
|
|
|
|
else:
|
|
|
|
loadedConfig = json.load(configFile) or {}
|
2022-05-12 07:14:23 +00:00
|
|
|
except:
|
2024-02-06 20:10:41 +00:00
|
|
|
os.rename(configFilePath, f"{configFilePathBase}-{time.time():.0f}.{configFileExtension}")
|
2023-11-05 05:24:45 +00:00
|
|
|
logger.exception("Failed to parse the config file. A new one will be created.")
|
2022-05-22 04:37:09 +00:00
|
|
|
else:
|
2023-11-04 18:13:42 +00:00
|
|
|
copyDict(loadedConfig, config)
|
2022-05-12 07:14:23 +00:00
|
|
|
saveConfig()
|
|
|
|
|
2023-11-04 19:39:30 +00:00
|
|
|
class YamlSafeDumper(yaml.SafeDumper):
|
|
|
|
def increase_indent(self, flow: bool = False, indentless: bool = False) -> None:
|
|
|
|
return super().increase_indent(flow, False)
|
|
|
|
|
2022-05-12 07:14:23 +00:00
|
|
|
def saveConfig() -> None:
|
|
|
|
try:
|
|
|
|
with open(configFilePath, "w", encoding = "UTF-8") as configFile:
|
2023-11-04 19:39:30 +00:00
|
|
|
if configFileType == "yaml":
|
|
|
|
yaml.dump(config, configFile, sort_keys = False, Dumper = YamlSafeDumper, allow_unicode = True)
|
|
|
|
else:
|
|
|
|
json.dump(config, configFile, indent = "\t")
|
|
|
|
configFile.write("\n")
|
2022-05-12 07:14:23 +00:00
|
|
|
except:
|
2023-11-05 05:24:45 +00:00
|
|
|
logger.exception("Failed to write to the config file")
|