Adding documentation comments for doxygen (#1)
This commit is contained in:
@@ -1,4 +1,56 @@
|
||||
# This Python file uses the following encoding: utf-8
|
||||
"""! @brief Defines the configuration class."""
|
||||
##
|
||||
# @file ConfigLoader.py
|
||||
#
|
||||
# @brief Defines the ConfigLoader class.
|
||||
#
|
||||
# @section description_configloader Description
|
||||
# Defines the base class for the program configuration.
|
||||
# - ConfigLoader (base class)
|
||||
#
|
||||
# @section libraries_configloader Libraries/Modules
|
||||
# - <a href="https://docs.python.org/3/library/os.html">os</a> standard library
|
||||
# - Access to system specific information.
|
||||
# - <a href="https://docs.python.org/3/library/urllib.html">urllib</a> Python package
|
||||
# - Collects several modules for working with URLs.
|
||||
# - <a href="https://pypi.org/project/toml">toml</a> Python library
|
||||
# - A Python library for parsing and creating TOML.
|
||||
# - <a href="https://pypi.org/project/platformdirs/">platformdirs</a> Python package
|
||||
# - Determining appropriate platform-specific dirs.
|
||||
# - <a href="https://docs.python.org/3/library/pathlib.html">pathlib</a> Python module
|
||||
# - Offers classes representing filesystem paths with semantics appropriate for different operating systems.
|
||||
# - <a href="https://docs.python.org/3/library/shutil.html">shutil</a> Python module
|
||||
# - Offers a number of high-level operations on files and collections of files.
|
||||
# - <a href="https://docs.python.org/3/library/base64.html">base64</a> Python standard library
|
||||
# - Provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data.
|
||||
# - <a href="https://doc.qt.io/qtforpython-6/PySide6/QtCore/QObject.html">QObject</a> PySide6 class
|
||||
# - The base class of all Qt objects.
|
||||
# - <a href="https://doc.qt.io/qtforpython-6/PySide6/QtCore/Slot.html">Slot</a> PySide6 function
|
||||
# - A function that is called in response to a particular signal.
|
||||
# - <a href="https://doc.qt.io/qtforpython-6/PySide6/QtCore/Signal.html">Signal</a> PySide6 class
|
||||
# - Provides a way to declare and connect Qt signals in a pythonic way.
|
||||
# - <a href="https://pycryptodome.readthedocs.io/en/latest/src/api.html">Crypto</a> Python package
|
||||
# - Provides cryptographic functionalities.
|
||||
# - DbManager local class
|
||||
# - Provides a singleton database connection for the entire program.
|
||||
# - UserManager local class
|
||||
# - Provides a model to handle users.
|
||||
# - PyqcrmFlags local ENUM
|
||||
# - Provides ENUMS to facilitate working in the program.
|
||||
# - Vermasseln local class
|
||||
# - Provides encryption functionality for the program.
|
||||
#
|
||||
# @section notes_configloader Notes
|
||||
# - Needs a database connection.
|
||||
#
|
||||
# @section todo_configloader TODO
|
||||
# - None.
|
||||
#
|
||||
# @section author_configloader Author(s)
|
||||
# - Created by Linuxero on 03/14/2025.
|
||||
# - Modified by Linuxero on 03/14/2025.
|
||||
#
|
||||
# Copyright (c) 2025 Schnaxero. All rights reserved.
|
||||
import toml
|
||||
from platformdirs import user_config_dir
|
||||
from pathlib import Path
|
||||
@@ -17,6 +69,10 @@ from .PyqcrmFlags import PyqcrmFlags
|
||||
|
||||
|
||||
class ConfigLoader(QObject):
|
||||
"""! The ConfigLoader class.
|
||||
Defines the class utilized by all different parts of the program.
|
||||
Handles the local configuration of the whole program.
|
||||
"""
|
||||
__config = None
|
||||
__version = "0.1-alpha"
|
||||
__check_enc_key = True
|
||||
@@ -30,6 +86,8 @@ class ConfigLoader(QObject):
|
||||
invalidEncryptionKey = Signal()
|
||||
|
||||
def __init__(self):
|
||||
"""! The ConfigLoader class initializer.
|
||||
"""
|
||||
super().__init__()
|
||||
# print(f"In {__file__} file, __init__()")
|
||||
self.config_dir = user_config_dir() + '/pyqcrm'
|
||||
@@ -44,6 +102,10 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(dict, result = bool)
|
||||
def setConfig(self, app_config):
|
||||
"""! Prepares the configuration of the program.
|
||||
@param app_config The configuration as a dictionary.
|
||||
@return True on success, False on failure.
|
||||
"""
|
||||
# print(f"In {__file__} file, setConfig()")
|
||||
if not self.__config:
|
||||
base_conf = self.__initializeConfig()
|
||||
@@ -58,6 +120,8 @@ class ConfigLoader(QObject):
|
||||
self.configurationReady.emit()
|
||||
|
||||
def __initializeConfig(self):
|
||||
"""! Creates the initial configuration of the program.
|
||||
"""
|
||||
# print(f"In {__file__} file, __initializeConfig()")
|
||||
self.__encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
|
||||
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\n"
|
||||
@@ -66,6 +130,10 @@ class ConfigLoader(QObject):
|
||||
return conf
|
||||
|
||||
def __checkDbConnection(self, db_config):
|
||||
"""! Tests for a valid database connection.
|
||||
@param db_config The configuration of the database connection as a dictionary.
|
||||
@return True on success, False on failure.
|
||||
"""
|
||||
# print(f"In {__file__} file, __checkDbConnection()")
|
||||
con = DbManager(db_config['database']).getConnection()
|
||||
if con:
|
||||
@@ -77,6 +145,8 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def __saveConfig(self):
|
||||
"""! Saves the configuration of the program.
|
||||
"""
|
||||
# print(f"In {__file__} file, saveConfig()")
|
||||
try:
|
||||
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
|
||||
@@ -88,6 +158,9 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def __checkAdminUser(self):
|
||||
"""! Checks for a valid admin account of the program.
|
||||
@return True on success, False on failure.
|
||||
"""
|
||||
# print(f"In {__file__} file, __checkAdminUser()")
|
||||
result = UserManager().checkAdmin()
|
||||
if not result:
|
||||
@@ -100,6 +173,10 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(dict, result= bool)
|
||||
def addAdminUser(self, user_config):
|
||||
"""! Adds an admin account.
|
||||
@param user_config The credentials of the admin account as a dictionary.
|
||||
@return True on success, False on failure.
|
||||
"""
|
||||
# print(f"In {__file__} file, addAdminUser()")
|
||||
admin = UserManager(user_config["user"], PyqcrmFlags.ADMIN).createUser()
|
||||
if not admin:
|
||||
@@ -114,6 +191,12 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(str, str)
|
||||
def __saveData(self, recovery_file, recovery_password, data):
|
||||
"""! Generic function to save backups to a file.
|
||||
This function emits a configurationReady signal.
|
||||
@param recovery_file The full path of the backup file.
|
||||
@param recovery_password password to secure the backup file.
|
||||
@param data The content of the backup file.
|
||||
"""
|
||||
# print(f"In {__file__} file, __saveData()")
|
||||
local = False
|
||||
rp = self.__setRecoveryPassword(recovery_password)
|
||||
@@ -133,6 +216,11 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(str, str)
|
||||
def getRecoveryKey(self, recovery_file, recovery_password):
|
||||
"""! Loads the encryption key from a backup.
|
||||
This function emits a configurationReady signal.
|
||||
@param recovery_file The full path of the backup file.
|
||||
@param recovery_password password to secure the backup file.
|
||||
"""
|
||||
rec_file = urlparse(recovery_file)
|
||||
rec_file = rec_file.path
|
||||
if os.name == "nt":
|
||||
@@ -150,6 +238,11 @@ class ConfigLoader(QObject):
|
||||
print(str(e))
|
||||
|
||||
def __parseImport(self, rec_file, recovery_password):
|
||||
"""! Loads the content from a backup.
|
||||
@param rec_file The full path of the backup file.
|
||||
@param recovery_password password used to secure the backup file.
|
||||
@return The content on success, None on failure.
|
||||
"""
|
||||
local = False
|
||||
with open(rec_file, "r") as f:
|
||||
|
||||
@@ -167,23 +260,40 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def __invalidateEncryptionKey(self):
|
||||
"""! Flag the encryption key as invalid.
|
||||
"""
|
||||
# print(f"In {__file__} file, __invalidateEncryptionKey()")
|
||||
self.__config['pyqcrm']['ENCRYPTION_KEY_VALID'] = 'No'
|
||||
self.__saveConfig()
|
||||
|
||||
@Slot()
|
||||
def checkEncryptionKey(self):
|
||||
"""! Checks the validity of the encryption key.
|
||||
This function emits an invalidEncryptionKey signal.
|
||||
"""
|
||||
# print(f"In {__file__} file, __checkEncryptionKey()")
|
||||
if self.__config['pyqcrm']['ENCRYPTION_KEY_VALID'] == 'No':
|
||||
self.invalidEncryptionKey.emit()
|
||||
|
||||
def __checkRecoveryPassword(self, recovery_password, password, salt):
|
||||
"""! Generic function to save backups to a file.
|
||||
This function emits a configurationReady signal.
|
||||
@param recovery_password The password from the backup file.
|
||||
@param password The password used when creating the backup file.
|
||||
@param salt A salt to hash the password.
|
||||
@return A password.
|
||||
"""
|
||||
# print(f"In {__file__} file, __checkRecoveryPassword()")
|
||||
rp = self.__setRecoveryPassword(recovery_password, salt)
|
||||
return rp[1] == password
|
||||
|
||||
@Slot(str, str) # todo: non local encryption
|
||||
def importConfig(self, confile, password):
|
||||
"""! Generic function to import configuration from a backup.
|
||||
This function emits a invalidEncryptionKey signal.
|
||||
@param conffile The path of the backup file.
|
||||
@param password The password used when creating the backup file.
|
||||
"""
|
||||
confile = urlparse(confile)
|
||||
confile = confile.path
|
||||
if os.name == "nt":
|
||||
@@ -206,6 +316,9 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def __configLoad(self):
|
||||
"""! Loads the program configuration.
|
||||
This function emits a configurationReady signal.
|
||||
"""
|
||||
# print(f"In {__file__} file, __configLoad()")
|
||||
try:
|
||||
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
|
||||
@@ -221,6 +334,9 @@ class ConfigLoader(QObject):
|
||||
|
||||
|
||||
def getConfig(self):
|
||||
"""! Returns the program configuration.
|
||||
@return configuration as a toml file.
|
||||
"""
|
||||
# print(f"In {__file__} file, getConfig()")
|
||||
# print(self.__config)
|
||||
return self.__config
|
||||
@@ -239,17 +355,27 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(str, str)
|
||||
def backupConfig(self, filename, password):
|
||||
"""! Saves the program configuration.
|
||||
@param filename the path of the backup file.
|
||||
@param password the password to secure the backup.
|
||||
"""
|
||||
conf_file = toml.dumps(self.getConfig())
|
||||
self.__saveData(filename, password, conf_file)
|
||||
|
||||
|
||||
@Slot(dict)
|
||||
def saveDbConf(self, db = None):
|
||||
"""! Saves/Upates the database configuration.
|
||||
@param db Database configuration as a dictionary.
|
||||
"""
|
||||
self.__config.update(db)
|
||||
self.__saveConfig()
|
||||
|
||||
@Slot(result = dict)
|
||||
def getDbConf(self):
|
||||
"""! Loads the database configuration.
|
||||
@return Database configuration as a dictionary on success, None on failure.
|
||||
"""
|
||||
try:
|
||||
return self.__config['database']
|
||||
except KeyError as ex:
|
||||
@@ -258,11 +384,17 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(dict)
|
||||
def saveCompanyInfo(self, company = None):
|
||||
"""! Saves/Upates the company information.
|
||||
@param company Company configuration as a dictionary.
|
||||
"""
|
||||
self.__config.update(company)
|
||||
self.__saveConfig()
|
||||
|
||||
@Slot(result = dict)
|
||||
def getCompanyInfo(self):
|
||||
"""! Loads the company information.
|
||||
@return Company information as a dictionary on success, None on failure.
|
||||
"""
|
||||
try:
|
||||
return self.__config['company']
|
||||
except KeyError as ex:
|
||||
@@ -271,11 +403,17 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(dict)
|
||||
def saveMiscConf(self, misc_conf = None):
|
||||
"""! Saves/Upates the miscellaneous configuration.
|
||||
@param misc_conf Extra configuration as a dictionary.
|
||||
"""
|
||||
self.__config.update(misc_conf)
|
||||
self.__saveConfig()
|
||||
|
||||
@Slot(result = bool)
|
||||
def systray(self):
|
||||
"""! Loads the system tray configuration.
|
||||
@return boolean if system tray is set to be used, False can also be returned on failure.
|
||||
"""
|
||||
try:
|
||||
return self.__config['misc']['SYSTRAY']
|
||||
except KeyError as ex:
|
||||
@@ -285,6 +423,10 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(str, str)
|
||||
def backupEncryptkey(self, filename, password):
|
||||
"""! Saves/Upates the encryption key.
|
||||
@param filename Path to save the key.
|
||||
@param password Password to secure the backup.
|
||||
"""
|
||||
encrypt_key = self.__config['pyqcrm']['ENCRYPTION_KEY']
|
||||
self.__saveData(filename, password, encrypt_key)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user