discord-rich-presence-plex/core/config.py

75 lines
2.1 KiB
Python
Raw Normal View History

2023-11-04 19:39:30 +00:00
from config.constants import configFilePathRoot
from utils.dict import copyDict
2022-05-14 09:43:02 +00:00
from utils.logging import logger
import json
2022-05-14 09:43:02 +00:00
import models.config
import os
import time
2023-11-04 19:39:30 +00:00
import yaml
2022-05-14 09:43:02 +00:00
config: models.config.Config = {
"logging": {
"debug": True,
"writeToFile": False,
},
"display": {
"hideTotalTime": False,
"useRemainingTime": False,
"posters": {
"enabled": False,
"imgurClientID": "",
},
"buttons": [],
},
"users": [],
}
2023-11-04 19:39:30 +00:00
supportedConfigFileExtensions = {
"yaml": "yaml",
"yml": "yaml",
"json": "json",
}
configFileExtension = ""
configFileType = ""
configFilePath = ""
def loadConfig() -> None:
2023-11-04 19:39:30 +00:00
global configFileType, configFileExtension, configFilePath
for fileExtension, fileType in supportedConfigFileExtensions.items():
filePath = f"{configFilePathRoot}.{fileExtension}"
if os.path.isfile(filePath):
configFileExtension = fileExtension
configFileType = fileType
break
else:
configFileExtension, configFileType = list(supportedConfigFileExtensions.items())[0]
filePath = f"{configFilePathRoot}.{configFileExtension}"
configFilePath = filePath
if os.path.isfile(configFilePath):
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 {}
except:
2023-11-04 19:39:30 +00:00
os.rename(configFilePath, f"{configFilePathRoot}-{time.time():.0f}.{configFileExtension}")
logger.exception("Failed to parse the application's config file. A new one will be created.")
else:
copyDict(loadedConfig, config)
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)
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")
except:
logger.exception("Failed to write to the application's config file.")