Konfiguration wird geladen und gespeichert.
This commit is contained in:
+18
-7
@@ -1,16 +1,17 @@
|
|||||||
from os import path
|
from os import path
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from sys import platform
|
from sys import platform
|
||||||
|
from typing import Tuple, Type
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, JsonConfigSettingsSource
|
||||||
|
|
||||||
app_name = "DocuMentor"
|
app_name = "DocuMentor"
|
||||||
|
|
||||||
|
|
||||||
if platform == "win32":
|
if platform == "win32":
|
||||||
config_path = f"%APPDATA%\\{app_name}\\config.json"
|
config_path = f"%APPDATA%\\{app_name}\\config.json"
|
||||||
elif platform == "linux" or platform == "linux2":
|
elif platform in ("linux", "linux2"):
|
||||||
config_path = f"~/.config/{app_name}/config.json"
|
config_path = f"~/.config/{app_name}/config.json"
|
||||||
elif platform == "darwin":
|
elif platform == "darwin":
|
||||||
config_path = f"~/Library/Application Support/{app_name}/͏͏͏͏config.json"
|
config_path = f"~/Library/Application Support/{app_name}/͏͏͏͏config.json"
|
||||||
@@ -76,20 +77,30 @@ class AppSettings(BaseSettings):
|
|||||||
|
|
||||||
model_config = SettingsConfigDict(json_file=config_path)
|
model_config = SettingsConfigDict(json_file=config_path)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def settings_customise_sources(
|
||||||
|
cls,
|
||||||
|
settings_cls: Type[BaseSettings],
|
||||||
|
init_settings: PydanticBaseSettingsSource,
|
||||||
|
env_settings: PydanticBaseSettingsSource,
|
||||||
|
dotenv_settings: PydanticBaseSettingsSource,
|
||||||
|
file_secret_settings: PydanticBaseSettingsSource,
|
||||||
|
) -> Tuple[PydanticBaseSettingsSource, ...]:
|
||||||
|
return (JsonConfigSettingsSource(settings_cls),)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
global config_path
|
global config_path
|
||||||
if not config_path.exists():
|
|
||||||
# Datei existert nicht
|
|
||||||
if not config_path.parent.exists():
|
if not config_path.parent.exists():
|
||||||
# Ordner existiert nicht
|
# Ordner existert nicht
|
||||||
config_path.parent.mkdir()
|
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Konfiguration speichern
|
# Konfiguration speichern
|
||||||
with open(config_path, "wb") as c:
|
with open(config_path, "wb") as c:
|
||||||
c.write(app_settings.model_dump_json(indent=4).encode())
|
c.write(app_settings.model_dump_json(indent=4).encode())
|
||||||
|
|
||||||
app_settings = AppSettings()
|
|
||||||
|
|
||||||
|
app_settings = AppSettings()
|
||||||
|
print(app_settings)
|
||||||
|
|
||||||
class PdfProjectSettings(BaseSettings):
|
class PdfProjectSettings(BaseSettings):
|
||||||
"""
|
"""
|
||||||
|
|||||||
+7
-2
@@ -6,7 +6,7 @@ from ui.MainWindow import MainWindow
|
|||||||
from ui.AppSettings import AppSettingsDlg
|
from ui.AppSettings import AppSettingsDlg
|
||||||
from conf import app_settings
|
from conf import app_settings
|
||||||
|
|
||||||
#import qdarktheme
|
# import qdarktheme
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -23,7 +23,12 @@ def main():
|
|||||||
# Hauptfenster anzeigen
|
# Hauptfenster anzeigen
|
||||||
window.show()
|
window.show()
|
||||||
|
|
||||||
if len(app_settings.apache_fops) == 0 or len(app_settings.diff_pdfs) == 0 or len(app_settings.java_vms) == 0 or len(app_settings.saxon_jars):
|
if (
|
||||||
|
len(app_settings.apache_fops) == 0
|
||||||
|
or len(app_settings.diff_pdfs) == 0
|
||||||
|
or len(app_settings.java_vms) == 0
|
||||||
|
or len(app_settings.saxon_jars) == 0
|
||||||
|
):
|
||||||
# Als Modal Dialog öffnen!
|
# Als Modal Dialog öffnen!
|
||||||
dlg = AppSettingsDlg(window, app_settings)
|
dlg = AppSettingsDlg(window, app_settings)
|
||||||
dlg.exec()
|
dlg.exec()
|
||||||
|
|||||||
@@ -582,6 +582,8 @@ class AppSettingsDlg(QDialog):
|
|||||||
self.settings.apache_fops = self.temp_apache_fops.copy()
|
self.settings.apache_fops = self.temp_apache_fops.copy()
|
||||||
self.settings.xsl_dirs = self.temp_xsl_dirs.copy()
|
self.settings.xsl_dirs = self.temp_xsl_dirs.copy()
|
||||||
|
|
||||||
|
self.settings.save()
|
||||||
|
|
||||||
super().accept()
|
super().accept()
|
||||||
|
|
||||||
def get_settings(self) -> AppSettings:
|
def get_settings(self) -> AppSettings:
|
||||||
|
|||||||
Reference in New Issue
Block a user