SXXXXXXX_Radalyze/radalyze/config/manager.py
2025-06-13 14:13:29 +02:00

56 lines
1.6 KiB
Python

import configparser
import os
from typing import Dict
class ConfigManager:
"""Manages the application's INI configuration file."""
_SECTION_NAME = 'Settings'
_LAST_DIR_KEY = 'last_directory'
_LAST_CMAP_KEY = 'last_colormap'
def __init__(self, config_file: str):
"""
Initializes the ConfigManager.
Args:
config_file: The path to the configuration file.
"""
self.config_file = config_file
self.config = configparser.ConfigParser()
def load_config(self) -> Dict[str, str]:
"""
Loads the configuration from the file.
If the file does not exist, it returns default values.
Returns:
A dictionary containing the configuration settings.
"""
if os.path.exists(self.config_file):
self.config.read(self.config_file)
# Use .get with fallback for safety, even if file exists
last_directory = self.config.get(
self._SECTION_NAME, self._LAST_DIR_KEY, fallback='.'
)
last_colormap = self.config.get(
self._SECTION_NAME, self._LAST_CMAP_KEY, fallback='viridis'
)
return {
'last_directory': last_directory,
'last_colormap': last_colormap
}
def save_config(self, settings: Dict[str, str]) -> None:
"""
Saves the given settings to the configuration file.
Args:
settings: A dictionary of settings to save.
"""
self.config[self._SECTION_NAME] = settings
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)