Files
Py-Backup/config_manager.py
Désiré Werner Menrath 974c8295f1 feat: Verbesserung der Backup-Typ-Auswahl und Korrektur der Füllstandsanzeige
2
   3 Dieses Commit enthält zwei wesentliche Verbesserungen:
   4
   5 1.  **Flexible Auswahl des Backup-Typs:** Der Benutzer kann jetzt manuell die
     Erstellung eines vollständigen Backups auswählen, auch wenn bereits ein früheres
     vollständiges Backup vorhanden ist. Die Anwendung wechselt in diesem Fall nicht mehr
     automatisch zu einem inkrementellen Backup.
   6
   7 2.  **Korrektur der Füllstandsanzeige:** Die Füllstandsanzeige zeigt jetzt die
     voraussichtliche Backup-Größe sowohl für vollständige als auch für inkrementelle
     Backups korrekt an. Dies wurde erreicht, indem sichergestellt wurde, dass die
     Quellgröße in allen Fällen korrekt berechnet und an die Benutzeroberfläche übergeben
     wird.
2025-08-31 17:37:05 +02:00

84 lines
2.5 KiB
Python

import json
from pathlib import Path
from typing import Any, Dict
from json import JSONEncoder
class PathEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, Path):
return str(obj)
return JSONEncoder.default(self, obj)
class ConfigManager:
"""Manages loading and saving of application settings in a JSON file."""
def __init__(self, file_path: Path):
"""
Initializes the ConfigManager.
Args:
file_path: The path to the configuration file.
"""
self.file_path = file_path
self.settings: Dict[str, Any] = {}
self.load()
def load(self) -> None:
"""Loads the settings from the JSON file."""
if self.file_path.exists():
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
self.settings = json.load(f)
except (IOError, json.JSONDecodeError) as e:
print(f"Error loading config file {self.file_path}: {e}")
self.settings = {}
else:
self.settings = {}
def save(self) -> None:
"""Saves the current settings to the JSON file."""
try:
# Ensure the parent directory exists
self.file_path.parent.mkdir(parents=True, exist_ok=True)
with open(self.file_path, 'w', encoding='utf-8') as f:
json.dump(self.settings, f, indent=4, cls=PathEncoder)
except IOError as e:
print(f"Error saving config file {self.file_path}: {e}")
def get_setting(self, key: str, default: Any = None) -> Any:
"""
Gets a setting value.
Args:
key: The key of the setting.
default: The default value to return if the key is not found.
Returns:
The value of the setting or the default value.
"""
return self.settings.get(key, default)
def set_setting(self, key: str, value: Any) -> None:
"""
Sets a setting value and immediately saves it to the file.
Args:
key: The key of the setting.
value: The value to set.
"""
self.settings[key] = value
self.save()
def remove_setting(self, key: str) -> None:
"""
Removes a setting from the configuration.
Args:
key: The key of the setting to remove.
"""
if key in self.settings:
del self.settings[key]
self.save()