Konfiguration wird geladen und gespeichert.
This commit is contained in:
+24
-13
@@ -1,16 +1,17 @@
|
||||
from os import path
|
||||
from pathlib import Path
|
||||
from sys import platform
|
||||
from typing import Tuple, Type
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, JsonConfigSettingsSource
|
||||
|
||||
app_name = "DocuMentor"
|
||||
|
||||
|
||||
if platform == "win32":
|
||||
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"
|
||||
elif platform == "darwin":
|
||||
config_path = f"~/Library/Application Support/{app_name}/͏͏͏͏config.json"
|
||||
@@ -75,21 +76,31 @@ class AppSettings(BaseSettings):
|
||||
pdf_projects: list[PdfProject] = []
|
||||
|
||||
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):
|
||||
global config_path
|
||||
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())
|
||||
if not config_path.parent.exists():
|
||||
# Ordner existert nicht
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Konfiguration speichern
|
||||
with open(config_path, "wb") as c:
|
||||
c.write(app_settings.model_dump_json(indent=4).encode())
|
||||
|
||||
|
||||
app_settings = AppSettings()
|
||||
|
||||
print(app_settings)
|
||||
|
||||
class PdfProjectSettings(BaseSettings):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user