Files
pyqcrm/lib/ConfigLoader.py
2024-11-25 15:17:20 +01:00

89 lines
2.4 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
from .Vermasseln import Vermasseln
import shutil
from urllib.parse import urlparse
from .DB.DbManager import DbManager
import os
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(str, str)
def importConfig(self, confile, password):
confile = urlparse(confile)
confile = confile.path
if os.name == "nt":
confile = confile[1:]
shutil.copyfile(confile, self.config_dir+ '/pyqcrm.toml')
@Slot(dict, result= bool)
def addAdminUser(self, admin_config):
print(admin_config)
return True
@Slot(dict, result= bool)
def setConfig(self, app_config):
conf = self.__checkDbConnection(app_config)
if conf:
try:
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
config = Vermasseln().oscarVermasseln(toml.dumps(app_config))
f.write(config)
except FileNotFoundError:
conf = False
print("Konnte die Konfiguration nicht speichern.")
conf = self.__checkAdminUser()
return conf
def __configLoad(self):
try:
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
config = f.read()
self.__config = toml.loads(Vermasseln().entschluesseln(config))
except FileNotFoundError:
print("Konnte die Konfiguration nicht laden.")
except TypeError:
print("Invalid Configuration")
# except Exception as e:
# print(str(e))
def getConfig(self):
return self.__config
def __createConfig(self):
with open(self.config_dir + '/pyqcrm.toml', "w") as datei:
datei.write("[pyqcrm]")
def __checkDbConnection(self, db_config):
con = DbManager(db_config['database']).getConnection()
if con:
return True
else:
return False
def __checkAdminUser(self):
pass