|
1
|
import configparser
|
|
2
|
from pathlib import Path
|
|
3
|
from typing import Any, Union
|
|
4
|
|
|
5
|
class ConfigReader:
|
|
6
|
"""!
|
|
7
|
@brief Handles the parsing and retrieval of application settings from INI files.
|
|
8
|
|
|
9
|
This class provides a wrapper around configparser to read settings from a file
|
|
10
|
with fallback values and type conversion.
|
|
11
|
"""
|
|
12
|
|
|
13
|
def __init__(self, config_path: str):
|
|
14
|
"""!
|
|
15
|
@brief Initializes the ConfigReader.
|
|
16
|
|
|
17
|
@param config_path The path to the configuration file.
|
|
18
|
@exception FileNotFoundError Raised if the configuration file does not exist.
|
|
19
|
"""
|
|
20
|
self.file_path = Path(config_path)
|
|
21
|
if not self.file_path.exists():
|
|
22
|
raise FileNotFoundError(f"Configuration file not found: {self.file_path}")
|
|
23
|
|
|
24
|
self._parser = configparser.ConfigParser()
|
|
25
|
self._parser.read(self.file_path)
|
|
26
|
|
|
27
|
def _get(self, section: str, key: str, fallback: Any = None) -> str:
|
|
28
|
"""!
|
|
29
|
@brief Internal method to safely get a value from the config parser.
|
|
30
|
|
|
31
|
@param section The section in the INI file.
|
|
32
|
@param key The key within the section.
|
|
33
|
@param fallback The value to return if the section or key is missing.
|
|
34
|
@return The value from the config file or the fallback value.
|
|
35
|
"""
|
|
36
|
try:
|
|
37
|
return self._parser.get(section, key)
|
|
38
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
|
39
|
return fallback
|
|
40
|
|
|
41
|
def get_value(self, section: str, key: str, fallback: Any = None) -> Union[str, Any]:
|
|
42
|
"""!
|
|
43
|
@brief Retrieves a value from the configuration.
|
|
44
|
|
|
45
|
@param section The section in the INI file.
|
|
46
|
@param key The key within the section.
|
|
47
|
@param fallback The value to return if the section or key is missing.
|
|
48
|
@return The value from the config file or the fallback value.
|
|
49
|
"""
|
|
50
|
return self._get(section, key, fallback)
|
|
51
|
|
|
52
|
def get_linear_parameter(self) -> float:
|
|
53
|
"""!
|
|
54
|
@brief Retrieves the linear blending parameter.
|
|
55
|
|
|
56
|
@return The linear parameter as a float. Default is 0.0.
|
|
57
|
"""
|
|
58
|
val = self._get('Parameters', 'linear_parameter', '0.0')
|
|
59
|
return float(val)
|
|
60
|
|
|
61
|
def get_image_path(self) -> str:
|
|
62
|
"""!
|
|
63
|
@brief Retrieves the path to the input image.
|
|
64
|
|
|
65
|
@return The image path as a string. Default is empty string.
|
|
66
|
"""
|
|
67
|
return self._get('Parameters', 'image_path', '')
|
|
68
|
|
|
69
|
def get_overlap(self) -> str:
|
|
70
|
"""!
|
|
71
|
@brief Retrieves the overlap value.
|
|
72
|
|
|
73
|
@return The overlap value as a string. Default is '0'.
|
|
74
|
"""
|
|
75
|
return self._get('Parameters', 'overlap', '0')
|
|
76
|
|
|
77
|
def get_blend_mode(self) -> str:
|
|
78
|
"""!
|
|
79
|
@brief Retrieves the blend mode.
|
|
80
|
|
|
81
|
@return The blend mode as a string. Default is 'linear'.
|
|
82
|
"""
|
|
83
|
return self._get('Parameters', 'blend_mode', 'linear')
|
|
84
|
|
|
85
|
def save_config(self) -> None:
|
|
86
|
"""!
|
|
87
|
@brief Saves the current configuration to the file.
|
|
88
|
"""
|
|
89
|
with open(self.file_path, 'w') as configfile:
|
|
90
|
self._parser.write(configfile)
|