diff --git a/src/config.py b/src/config.py index f11e341..29f7318 100644 --- a/src/config.py +++ b/src/config.py @@ -1,7 +1,23 @@ +from os import path from pathlib import Path +from sys import platform from pydantic import BaseModel -from pydantic_settings import BaseSettings # , SettingsConfigDict +from pydantic_settings import BaseSettings, SettingsConfigDict + +app_name = "DocuMentor" + + +if platform == "win32": + config_path = f"%APPDATA%\\{app_name}\\config.json" +elif platform == "linux" or platform == "linux2": + config_path = f"~/.config/{app_name}/config.json" +elif platform == "darwin": + config_path = f"~/Library/Application Support/{app_name}/͏͏͏͏config.json" +else: + config_path = f"~/.config/{app_name}/config.json" + +config_path = Path(path.expandvars(config_path)) class JavaVm(BaseModel): @@ -51,17 +67,31 @@ class PdfProject(BaseModel): class AppSettings(BaseSettings): - java_vms: list[JavaVm] - diff_pdfs: list[DiffPdf] - saxon_jars: list[SaxonJar] - apache_fops: list[ApacheFop] - xsl_dirs: list[XslDir] + java_vms: list[JavaVm] = [] + diff_pdfs: list[DiffPdf] = [] + saxon_jars: list[SaxonJar] = [] + apache_fops: list[ApacheFop] = [] + xsl_dirs: list[XslDir] = [] pdf_projects: list[PdfProject] = [] + model_config = SettingsConfigDict(json_file=config_path) + +app_settings = AppSettings() + +if not config_path.exists(): + # Datei existert nicht + if not config_path.parent.exists(): + # Ordner existiert nicht + config_path.parent.mkdir() + + # Konfiguration speichern + with open(config_path, "wb") as c: + c.write(app_settings.model_dump_json(indent=4).encode()) + class PdfProjectSettings(BaseSettings): """ - Speichert Projekt-Einstellungen direkt im Ordner des Projekts in einer Klartextdatei (JSON, YAML oder XML) + Speichert Projekt-Einstellungen direkt im Ordner des Projekts in einer Klartextdatei JSON """ pass