113 lines
3.3 KiB
Python
113 lines
3.3 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, Signal
|
|
from .Vermasseln import Vermasseln
|
|
import shutil
|
|
from urllib.parse import urlparse
|
|
from .DB.DbManager import DbManager
|
|
import os
|
|
from Crypto.Random import get_random_bytes
|
|
from base64 import b64encode, b64decode
|
|
from .DB.UserManager import UserManager
|
|
from .PyqcrmFlags import PyqcrmFlags
|
|
|
|
|
|
|
|
class ConfigLoader(QObject):
|
|
__config = None
|
|
__version = "0.1-alpha"
|
|
|
|
|
|
dbConnectionError = Signal(str, bool)
|
|
adminUserError = Signal(str, bool)
|
|
|
|
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, user_config):
|
|
admin = UserManager(user_config["user"], PyqcrmFlags.ADMIN).createUser()
|
|
return True
|
|
|
|
@Slot(dict, result= bool)
|
|
def setConfig(self, app_config):
|
|
base_conf = self.__initializeConfig()
|
|
conf = self.__checkDbConnection(app_config)
|
|
app_config = toml.dumps(app_config)
|
|
|
|
if conf:
|
|
app_config = base_conf + app_config
|
|
self.__config = toml.loads(app_config)
|
|
self.__saveConfig()
|
|
conf = self.__checkAdminUser()
|
|
|
|
|
|
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(f"Invalid Configuration: {__file__}")
|
|
except Exception as e:
|
|
print(str(e))
|
|
|
|
|
|
def getConfig(self):
|
|
return self.__config
|
|
|
|
def __initializeConfig(self):
|
|
encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
|
|
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\nENCRYPTION_KEY = \"{encrypt_key}\"\n\n"
|
|
return conf
|
|
|
|
|
|
def __checkDbConnection(self, db_config):
|
|
con = DbManager(db_config['database']).getConnection()
|
|
if con:
|
|
self.dbConnectionError.emit("Connection OK", True)
|
|
return True
|
|
else:
|
|
self.dbConnectionError.emit("Connection fehlgeschlagen", False)
|
|
return False
|
|
|
|
def __checkAdminUser(self):
|
|
|
|
self.adminUserError.emit("Kein Admin vorhanden", False)
|
|
|
|
@Slot(str)
|
|
def setEncyrptKey(self, key):
|
|
self.__config['pyqcrm']['ENCRYPTION_KEY'] = key
|
|
self.__saveConfig()
|
|
|
|
def __saveConfig(self):
|
|
try:
|
|
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
|
|
config = Vermasseln().oscarVermasseln(toml.dumps(self.__config))
|
|
f.write(config)
|
|
except FileNotFoundError:
|
|
print("Konnte die Konfiguration nicht speichern.")
|
|
|
|
|