feat: create config class to replace use of config file in application

This commit is contained in:
WorldTeacher
2024-10-29 16:07:30 +01:00
parent 2137799f9b
commit f3eed7486d
13 changed files with 224 additions and 111 deletions

View File

@@ -1,7 +1,11 @@
__version__ = "1.0.0"
__author__ = "Alexander Kirchner"
from config import Config
settings = Config("config/config.yaml")
from .utils.icon import Icon
from .logic.log import MyLogger
from .utils.icon import Icon
from .ui.userInterface import launch_gui as UI

View File

@@ -2,12 +2,10 @@ import os
from pathlib import Path
from icecream import ic
from omegaconf import OmegaConf
from src.backend.database import Database
db = Database()
config = OmegaConf.load("config.yaml")
def recreateFile(name, app_id, filetype, open=True) -> Path:

View File

@@ -3,6 +3,7 @@ import inspect
import sqlite3 as sql
import tempfile
from pathlib import Path
from src import database
from typing import Any, Dict, List, Optional, Tuple, Union
# from icecream import ic
from omegaconf import OmegaConf
@@ -28,7 +29,7 @@ from src.utils import create_blob, dump_pickle, load_pickle
from .semester import generateSemesterByDate
from icecream import ic
config = OmegaConf.load("config.yaml")
ascii_lowercase = "abcdefghijklmnopqrstuvwxyz0123456789)"
caller_frame = inspect.stack()[1]
# get the line that called the function
@@ -54,14 +55,14 @@ class Database:
self.name = script_name
self.logger = MyLogger(name)
if db_path is None:
self.db_path = config.database.path + config.database.name
self.db_path = database.path + database.name
self.db_path = self.db_path.replace("~", str(Path.home()))
else:
self.db_path = db_path
self.checkDatabaseStatus()
def checkDatabaseStatus(self):
path = config.database.path
path = database.path
path = path.replace("~", str(Path.home()))
# print(path)
path = os.path.abspath(path)
@@ -507,7 +508,7 @@ class Database:
str: The filename of the recreated file
"""
blob = self.getBlob(filename, app_id)
tempdir = config.database.tempdir
tempdir = database.tempdir
tempdir = tempdir.replace("~", str(Path.home()))
tempdir_path = Path(tempdir)
if not os.path.exists(tempdir_path):
@@ -1414,7 +1415,7 @@ class Database:
"SELECT fileblob FROM elsa_files WHERE filename=?", (filename,), one=True
)[0]
# print(blob)
tempdir = config.database.tempdir
tempdir = database.tempdir
tempdir = tempdir.replace("~", str(Path.home()))
tempdir_path = Path(tempdir)
if not os.path.exists(tempdir_path):

View File

@@ -1,16 +1,14 @@
import os
from pathlib import Path
from src import database
from omegaconf import OmegaConf
config = OmegaConf.load("config.yaml")
def delete_temp_contents():
"""
delete_temp_contents deletes the contents of the temp directory.
"""
path = config.database.tempdir
path = database.tempdir
path = path.replace("~", str(Path.home()))
path = Path(path)
path = path.resolve()

View File

@@ -2,12 +2,11 @@ import sqlite3
from PyQt6.QtCore import QThread
from PyQt6.QtCore import pyqtSignal as Signal
from omegaconf import OmegaConf
from src.backend.database import Database
from src.logic.log import MyLogger
from src.logic.webrequest import BibTextTransformer, WebRequest
config = OmegaConf.load("config.yaml")
class BookGrabber(QThread):

View File

@@ -1,6 +1,5 @@
import requests
from bs4 import BeautifulSoup
from omegaconf import OmegaConf
# import sleep_and_retry decorator to retry requests
from ratelimit import limits, sleep_and_retry
@@ -10,7 +9,6 @@ from src.logic.log import MyLogger
from src.transformers import ARRAYData, BibTeXData, COinSData, RDSData, RISData
logger = MyLogger(__name__)
config = OmegaConf.load("config.yaml")
API_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndexrecord/{}/"
PPN_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndex/Search?type0%5B%5D=allfields&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=au&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ti&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ct&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=isn&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ta&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=co&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=py&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pp&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pu&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=si&lookfor0%5B%5D={}&join=AND&bool0%5B%5D=AND&type0%5B%5D=zr&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=cc&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND"

View File

@@ -1,10 +1,8 @@
from pyzotero import zotero
from dataclasses import dataclass
from src.logic.webrequest import WebRequest, BibTextTransformer
from omegaconf import OmegaConf
from src import settings
config = OmegaConf.load("config.yaml")
config = config["zotero"]
@dataclass
@@ -160,9 +158,12 @@ class JournalArticle:
class ZoteroController:
zoterocfg = settings.zotero
def __init__(self):
self.zot = zotero.Zotero(
config["library_id"], config["library_type"], config["api_key"]
self.zoterocfg.library_id,
self.zoterocfg.library_type,
self.zoterocfg.api_key,
)
def get_books(self):

View File

@@ -1,11 +1,9 @@
from omegaconf import OmegaConf
from PyQt6 import QtCore, QtGui, QtWidgets
from src import Icon
from src import Icon, settings
from .dialog_sources.Ui_settings import Ui_Dialog as _settings
config = OmegaConf.load("config.yaml")
base = """'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style
@@ -61,18 +59,18 @@ class Settings(QtWidgets.QDialog, _settings):
self.buttonBox.accepted.connect(self.save)
def load_config(self):
self.db_name.setText(config.database.name)
self.db_path.setText(config.database.path)
self.save_path.setText(config.save_path)
self.smtp_address.setText(config.mail.smtp_server)
self.smtp_port.setText(str(config.mail.port))
self.sender_email.setText(config.mail.sender)
self.mail_username.setText(config.mail.user_name)
self.password.setText(config.mail.password)
self.db_name.setText(settings.database.name)
self.db_path.setText(settings.database.path)
self.save_path.setText(settings.save_path)
self.smtp_address.setText(settings.mail.smtp_server)
self.smtp_port.setText(str(settings.mail.port))
self.sender_email.setText(settings.mail.sender)
self.mail_username.setText(settings.mail.user_name)
self.password.setText(settings.mail.password)
self.use_username_smtp_login.setChecked(
config.mail.use_user_name if config.mail.use_user_name else False
settings.mail.use_user_name if settings.mail.use_user_name else False
)
self.editSignature.setHtml(config.mail.signature)
self.editSignature.setHtml(settings.mail.signature)
def setCurrentFont(self):
font = self.fontComboBox.currentFont()
@@ -148,22 +146,28 @@ class Settings(QtWidgets.QDialog, _settings):
signature = signature
else:
signature = ""
config.database.name = self.db_name.text()
config.database.path = self.db_path.text()
config.save_path = self.save_path.text()
config.mail.smtp_server = self.smtp_address.text()
config.mail.port = port
config.mail.sender = self.sender_email.text()
config.mail.user_name = self.mail_username.text()
config.mail.password = self.password.text()
config.mail.use_user_name = self.use_username_smtp_login.isChecked()
config.mail.signature = signature
return config
settings.set_database_attr("name", self.db_name.text())
settings.set_database_attr("path", self.db_path.text())
settings.save_path = self.save_path.text()
settings.set_mail_attr("smtp_server", self.smtp_address.text())
settings.set_mail_attr("port", port)
settings.set_mail_attr("sender", self.sender_email.text())
settings.set_mail_attr("user_name", self.mail_username.text())
settings.set_mail_attr("password", self.password.text())
settings.set_mail_attr(
"use_user_name", self.use_username_smtp_login.isChecked()
)
settings.set_mail_attr("signature", signature)
print("Settings:")
print(settings)
return settings
def save(self):
config = self.return_data()
OmegaConf.save(config, "config.yaml")
print(config.mail)
print("Saving config")
print(config)
config.save()
self.accept()

View File

@@ -1,7 +1,7 @@
from PyQt6 import QtWidgets, QtCore
from src.backend.database import Database
from PyQt6.QtCore import QDate
from PyQt6.QtGui import QColor, QPen
from src.backend import Database
import darkdetect
from icecream import ic

View File

@@ -2,8 +2,12 @@ import darkdetect
from omegaconf import OmegaConf
from PyQt6 import QtGui
import re
config = OmegaConf.load("config.yaml")
config = OmegaConf.load(f"{config.icon_path}/icons.yaml")
from config import Config
settings = Config("config/config.yaml")
config = OmegaConf.load(f"{settings.icon_path}/icons.yaml")
path = config.icon_path