67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
# This Python file uses the following encoding: utf-8
|
|
import toml
|
|
from platformdirs import user_config_dir
|
|
from pathlib import Path
|
|
from PySide6.QtCore import QObject, Slot
|
|
|
|
|
|
class ConfigLoader(QObject):
|
|
__config = None
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config_dir = user_config_dir() + '/pyqcrm' #user_config_dir = Funktion platformdirs
|
|
config_dir = Path(self.config_dir)
|
|
|
|
if config_dir.exists():
|
|
self.__configLoad()
|
|
else:
|
|
config_dir.mkdir(0o750, True, True)
|
|
|
|
@Slot(dict)
|
|
def setConfig(self, app_config):
|
|
pyqcrm = '[pyqcrm]\n'
|
|
db = '[database]\n'
|
|
for k, v in app_config.items():
|
|
if k == 'username':
|
|
pyqcrm = pyqcrm + f"PYQCRM_ADMIN = \"{v}\"\n"
|
|
elif k == 'password':
|
|
pyqcrm = pyqcrm + f"PYQCRM_ADMIN_PASS = \"{v}\"\n"
|
|
elif k == 'db-host':
|
|
db = db + f"DB_HOST = \"{v}\"\n"
|
|
elif k == 'db-name':
|
|
db = db + f"DB_PORT = \"{v}\"\n"
|
|
elif k == 'db-port':
|
|
db = db + f"DB_NAME = \"{v}\"\n"
|
|
elif k == 'db-username':
|
|
db = db + f"DB_USER = \"{v}\"\n"
|
|
elif k == 'db-password':
|
|
db = db + f"DB_PASS = \"{v}\"\n"
|
|
|
|
appconf = pyqcrm + '\n' + db + '\n'
|
|
|
|
try:
|
|
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
|
|
f.write(appconf)
|
|
except FileNotFoundError:
|
|
print("Konnte die Konfiguration nicht speichern.")
|
|
|
|
|
|
def __configLoad(self):
|
|
|
|
try:
|
|
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
|
|
self.__config = toml.load(f)
|
|
except FileNotFoundError:
|
|
print("Konnte die Konfiguration nicht laden.")
|
|
|
|
|
|
def getConfig(self):
|
|
|
|
return self.__config
|
|
|
|
def createConfig(self):
|
|
|
|
with open(self.config_dir + '/pyqcrm.toml', "w") as datei:
|
|
datei.write("[pyqcrm]")
|