Core configuration assembly logic for the fejao_config_parser package.
This module defines GetConfig, the orchestrator responsible for loading
configuration values from defaults, files, environment variables, and terminal
arguments. It validates configuration metadata, resolves precedence, and
exposes final values as attributes on the GetConfig instance.
GetConfig
Assemble and validate configuration values from multiple sources.
GetConfig performs the following steps:
1. Validates the provided configuration schema.
2. Loads default values from ParseFromDefault.
3. Loads values from the optional config file via ParseFromFile.
4. Loads environment values via ParseFromEnvironment.
5. Loads terminal values via ParseFromTerminal.
6. Resolves final values based on precedence: terminal > environment > file > default.
| Attributes: |
-
config
(list)
–
Configuration schema definitions.
-
config_file_path
(str | None)
–
Optional path to a config file.
-
description
(str | None)
–
CLI description used by the terminal parser.
-
debug
(bool)
–
When True, prints debug information about value resolution.
-
values_model
(ValuesModel)
–
Container holding parsed values from each source.
-
values_set_info
(dict)
–
Metadata for the final selected values and their sources.
|
Source code in fejao_config_parser/src/get_config.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 | class GetConfig:
"""Assemble and validate configuration values from multiple sources.
`GetConfig` performs the following steps:
1. Validates the provided configuration schema.
2. Loads default values from `ParseFromDefault`.
3. Loads values from the optional config file via `ParseFromFile`.
4. Loads environment values via `ParseFromEnvironment`.
5. Loads terminal values via `ParseFromTerminal`.
6. Resolves final values based on precedence: terminal > environment > file > default.
Attributes:
config (list): Configuration schema definitions.
config_file_path (str | None): Optional path to a config file.
description (str | None): CLI description used by the terminal parser.
debug (bool): When True, prints debug information about value resolution.
values_model (ValuesModel): Container holding parsed values from each source.
values_set_info (dict): Metadata for the final selected values and their sources.
"""
def __init__(
self,
config: list,
config_file_path: str | None,
description: str | None,
debug: bool = False) -> None:
"""Initialize the configuration loader and resolve final values.
Args:
config (list): List of configuration definitions.
config_file_path (str | None): Optional path to the configuration file.
description (str | None): Description string used for CLI help text.
debug (bool): Enable debug output after configuration resolution.
Returns:
None
"""
self.config = config
self.config_file_path = config_file_path
self.description = description
self.debug = debug
self.values_model = ValuesModel()
self.values_set_info = {}
self.__check_values_used()
self.__get_config_default()
self.__get_config_from_file()
self.__get_config_from_environment()
self.__get_config_from_terminal()
self.__set_values_used()
if self.debug:
self.__print_debug()
def __check_values_used(self) -> None:
"""Validate that the provided configuration schema uses supported keys.
This method ensures every configuration item is a dictionary, contains all
required keys, and does not include any unsupported keys.
Raises:
ValueError: If an item is not a dictionary, is missing a required key,
or contains an unsupported key.
Returns:
None
"""
keys_possible = set(DEFAULT_KEYS_POSSIBLE)
for i, item in enumerate(self.config):
if not isinstance(item, dict):
raise ValueError(
"\n\nError at the configuration. "
"Each item in the 'config' list must be a dictionary. "
"Please refer to the documentation for more details.\n")
for key in DEFAULT_KEYS_REQUIRED:
if key not in item:
raise ValueError(
"\n\nError at the configuration. "
f"Each dictionary in the 'config' list must contain the key '{key}'. "
"Please refer to the documentation for more details.\n")
item_keys = set(item.keys())
# Find unexpected keys
extra_keys = item_keys - keys_possible
if extra_keys:
raise ValueError(
"\n\nError checking the configuration values. "
f"Unknown parsed key name {extra_keys} in config at the item {i}. \n"
f"Possible keys to be used are: {DEFAULT_KEYS_POSSIBLE} \n"
"Please refer to the documentation for more details.\n")
def __get_config_default(self) -> None:
"""Load default configuration values.
Uses `ParseFromDefault` to derive values from the configuration schema.
Raises:
ValueError: If default value parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromDefault(config=self.config)
self.values_model.from_default = parser.get_values()
def __get_config_from_file(self) -> None:
"""Load configuration values from the optional config file.
If no config file path is provided, this method assigns an empty mapping.
Raises:
ValueError: If config file parsing fails or if parsed values do not match expected types.
Returns:
None
"""
if self.config_file_path is None:
self.values_model.from_file = {}
else:
parser = ParseFromFile(config=self.config, config_file_path=self.config_file_path)
self.values_model.from_file = parser.get_values()
def __get_config_from_environment(self) -> None:
"""Load configuration values from environment variables.
Results in a dictionary of parsed environment values that is stored in the `values_model`.
Raises:
ValueError: If environment variable parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromEnvironment(config=self.config)
self.values_model.from_environment = parser.get_values()
def __get_config_from_terminal(self) -> None:
"""Load configuration values from terminal arguments.
Registers terminal arguments for all configuration items that include `terminal_name` and `terminal_help`.
Boolean values are handled with special logic to support common true/false string representations.
Results in a dictionary of parsed terminal values that is stored in the `values_model`.
Raises:
ValueError: If terminal argument parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromTerminal(config=self.config, description=self.description)
self.values_model.from_terminal = parser.get_values()
def __set_single_value(self, value_name: str) -> str | int | float | bool | list | dict:
"""Resolve the final value for a single configuration item.
Resolution precedence is:
1. terminal
2. environment
3. config file
4. default
Args:
value_name (str): Name of the configuration item.
Returns:
str | int | float | bool | list | dict: The resolved configuration value.
"""
### FROM DEFAULT
value_from_default = self.values_model.from_default.get(value_name)
### FROM TERMINAL
from_terminal = self.values_model.from_terminal.get(value_name)
if isinstance(from_terminal, bool) and from_terminal != value_from_default:
self.values_set_info[value_name] = {"value": from_terminal, "source": "terminal"}
return from_terminal
if from_terminal is not None and from_terminal != value_from_default:
self.values_set_info[value_name] = {"value": from_terminal, "source": "terminal"}
return from_terminal
### FROM ENVIRONMENT
from_environment = self.values_model.from_environment.get(value_name)
if from_environment is not None:
self.values_set_info[value_name] = {"value": from_environment, "source": "environment"}
return from_environment
### FROM CONFIG FILE
from_config_file = self.values_model.from_file.get(value_name)
if from_config_file is not None:
# self.values_set_info[value_name] = {"value": from_config_file, "source": "config_file"}
self.values_set_info[value_name] = {"value": from_config_file, "source": self.config_file_path}
return from_config_file
### If nothing else was set, use default values
self.values_set_info[value_name] = {"value": value_from_default, "source": "default"}
return value_from_default
def __set_values_used(self) -> None:
"""Assign final resolved values as attributes on the instance.
Resolves the final value for each configuration item and sets it as
an attribute on the `GetConfig` instance.
This allows users to access configuration values directly as attributes.
Returns:
None
"""
for value in self.config:
setattr(self, value.get("name"), self.__set_single_value(value_name=value.get("name")))
def __print_debug(self) -> None:
"""Print debug output showing resolved values and their sources.
Returns:
None
"""
color_red = "\033[91m"
color_green = "\033[92m"
color_yellow = "\033[93m"
color_blue_dark = "\033[94m"
color_blue_light = "\033[96m"
color_end = "\033[0m"
print(color_green + "\nCONFIGURATION VALUES DEBUG INFO:" + color_end)
for key, value in self.values_set_info.items():
print(
color_yellow + "\nValue name: " + color_blue_light + f"'{key}'"
+ color_yellow + "\nValue set: " + color_red + f"'{value.get('value')}'"
+ color_yellow + "\nValue source: " + color_blue_dark + f"'{value.get('source')}'"
+ color_end
)
print("")
|
Methods:
__check_values_used
__check_values_used() -> None
Validate that the provided configuration schema uses supported keys.
This method ensures every configuration item is a dictionary, contains all
required keys, and does not include any unsupported keys.
| Raises: |
-
ValueError
–
If an item is not a dictionary, is missing a required key,
or contains an unsupported key.
|
Source code in fejao_config_parser/src/get_config.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 | def __check_values_used(self) -> None:
"""Validate that the provided configuration schema uses supported keys.
This method ensures every configuration item is a dictionary, contains all
required keys, and does not include any unsupported keys.
Raises:
ValueError: If an item is not a dictionary, is missing a required key,
or contains an unsupported key.
Returns:
None
"""
keys_possible = set(DEFAULT_KEYS_POSSIBLE)
for i, item in enumerate(self.config):
if not isinstance(item, dict):
raise ValueError(
"\n\nError at the configuration. "
"Each item in the 'config' list must be a dictionary. "
"Please refer to the documentation for more details.\n")
for key in DEFAULT_KEYS_REQUIRED:
if key not in item:
raise ValueError(
"\n\nError at the configuration. "
f"Each dictionary in the 'config' list must contain the key '{key}'. "
"Please refer to the documentation for more details.\n")
item_keys = set(item.keys())
# Find unexpected keys
extra_keys = item_keys - keys_possible
if extra_keys:
raise ValueError(
"\n\nError checking the configuration values. "
f"Unknown parsed key name {extra_keys} in config at the item {i}. \n"
f"Possible keys to be used are: {DEFAULT_KEYS_POSSIBLE} \n"
"Please refer to the documentation for more details.\n")
|
__get_config_default
__get_config_default() -> None
Load default configuration values.
Uses ParseFromDefault to derive values from the configuration schema.
| Raises: |
-
ValueError
–
If default value parsing fails or if parsed values do not match expected types.
|
Source code in fejao_config_parser/src/get_config.py
115
116
117
118
119
120
121
122
123
124
125
126
127 | def __get_config_default(self) -> None:
"""Load default configuration values.
Uses `ParseFromDefault` to derive values from the configuration schema.
Raises:
ValueError: If default value parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromDefault(config=self.config)
self.values_model.from_default = parser.get_values()
|
__get_config_from_environment
__get_config_from_environment() -> None
Load configuration values from environment variables.
Results in a dictionary of parsed environment values that is stored in the values_model.
| Raises: |
-
ValueError
–
If environment variable parsing fails or if parsed values do not match expected types.
|
Source code in fejao_config_parser/src/get_config.py
148
149
150
151
152
153
154
155
156
157
158
159 | def __get_config_from_environment(self) -> None:
"""Load configuration values from environment variables.
Results in a dictionary of parsed environment values that is stored in the `values_model`.
Raises:
ValueError: If environment variable parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromEnvironment(config=self.config)
self.values_model.from_environment = parser.get_values()
|
__get_config_from_file
__get_config_from_file() -> None
Load configuration values from the optional config file.
If no config file path is provided, this method assigns an empty mapping.
| Raises: |
-
ValueError
–
If config file parsing fails or if parsed values do not match expected types.
|
Source code in fejao_config_parser/src/get_config.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 | def __get_config_from_file(self) -> None:
"""Load configuration values from the optional config file.
If no config file path is provided, this method assigns an empty mapping.
Raises:
ValueError: If config file parsing fails or if parsed values do not match expected types.
Returns:
None
"""
if self.config_file_path is None:
self.values_model.from_file = {}
else:
parser = ParseFromFile(config=self.config, config_file_path=self.config_file_path)
self.values_model.from_file = parser.get_values()
|
__get_config_from_terminal
__get_config_from_terminal() -> None
Load configuration values from terminal arguments.
Registers terminal arguments for all configuration items that include terminal_name and terminal_help.
Boolean values are handled with special logic to support common true/false string representations.
Results in a dictionary of parsed terminal values that is stored in the values_model.
| Raises: |
-
ValueError
–
If terminal argument parsing fails or if parsed values do not match expected types.
|
Source code in fejao_config_parser/src/get_config.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 | def __get_config_from_terminal(self) -> None:
"""Load configuration values from terminal arguments.
Registers terminal arguments for all configuration items that include `terminal_name` and `terminal_help`.
Boolean values are handled with special logic to support common true/false string representations.
Results in a dictionary of parsed terminal values that is stored in the `values_model`.
Raises:
ValueError: If terminal argument parsing fails or if parsed values do not match expected types.
Returns:
None
"""
parser = ParseFromTerminal(config=self.config, description=self.description)
self.values_model.from_terminal = parser.get_values()
|
__init__
__init__(config: list, config_file_path: str | None, description: str | None, debug: bool = False) -> None
Initialize the configuration loader and resolve final values.
| Parameters: |
-
config
(list)
–
List of configuration definitions.
-
config_file_path
(str | None)
–
Optional path to the configuration file.
-
description
(str | None)
–
Description string used for CLI help text.
-
debug
(bool, default:
False
)
–
Enable debug output after configuration resolution.
|
Source code in fejao_config_parser/src/get_config.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 | def __init__(
self,
config: list,
config_file_path: str | None,
description: str | None,
debug: bool = False) -> None:
"""Initialize the configuration loader and resolve final values.
Args:
config (list): List of configuration definitions.
config_file_path (str | None): Optional path to the configuration file.
description (str | None): Description string used for CLI help text.
debug (bool): Enable debug output after configuration resolution.
Returns:
None
"""
self.config = config
self.config_file_path = config_file_path
self.description = description
self.debug = debug
self.values_model = ValuesModel()
self.values_set_info = {}
self.__check_values_used()
self.__get_config_default()
self.__get_config_from_file()
self.__get_config_from_environment()
self.__get_config_from_terminal()
self.__set_values_used()
if self.debug:
self.__print_debug()
|
__print_debug
Print debug output showing resolved values and their sources.
Source code in fejao_config_parser/src/get_config.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 | def __print_debug(self) -> None:
"""Print debug output showing resolved values and their sources.
Returns:
None
"""
color_red = "\033[91m"
color_green = "\033[92m"
color_yellow = "\033[93m"
color_blue_dark = "\033[94m"
color_blue_light = "\033[96m"
color_end = "\033[0m"
print(color_green + "\nCONFIGURATION VALUES DEBUG INFO:" + color_end)
for key, value in self.values_set_info.items():
print(
color_yellow + "\nValue name: " + color_blue_light + f"'{key}'"
+ color_yellow + "\nValue set: " + color_red + f"'{value.get('value')}'"
+ color_yellow + "\nValue source: " + color_blue_dark + f"'{value.get('source')}'"
+ color_end
)
print("")
|
__set_single_value
__set_single_value(value_name: str) -> str | int | float | bool | list | dict
Resolve the final value for a single configuration item.
Resolution precedence is
- terminal
- environment
- config file
- default
| Parameters: |
-
value_name
(str)
–
Name of the configuration item.
|
| Returns: |
-
str | int | float | bool | list | dict
–
str | int | float | bool | list | dict: The resolved configuration value.
|
Source code in fejao_config_parser/src/get_config.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 | def __set_single_value(self, value_name: str) -> str | int | float | bool | list | dict:
"""Resolve the final value for a single configuration item.
Resolution precedence is:
1. terminal
2. environment
3. config file
4. default
Args:
value_name (str): Name of the configuration item.
Returns:
str | int | float | bool | list | dict: The resolved configuration value.
"""
### FROM DEFAULT
value_from_default = self.values_model.from_default.get(value_name)
### FROM TERMINAL
from_terminal = self.values_model.from_terminal.get(value_name)
if isinstance(from_terminal, bool) and from_terminal != value_from_default:
self.values_set_info[value_name] = {"value": from_terminal, "source": "terminal"}
return from_terminal
if from_terminal is not None and from_terminal != value_from_default:
self.values_set_info[value_name] = {"value": from_terminal, "source": "terminal"}
return from_terminal
### FROM ENVIRONMENT
from_environment = self.values_model.from_environment.get(value_name)
if from_environment is not None:
self.values_set_info[value_name] = {"value": from_environment, "source": "environment"}
return from_environment
### FROM CONFIG FILE
from_config_file = self.values_model.from_file.get(value_name)
if from_config_file is not None:
# self.values_set_info[value_name] = {"value": from_config_file, "source": "config_file"}
self.values_set_info[value_name] = {"value": from_config_file, "source": self.config_file_path}
return from_config_file
### If nothing else was set, use default values
self.values_set_info[value_name] = {"value": value_from_default, "source": "default"}
return value_from_default
|
__set_values_used
__set_values_used() -> None
Assign final resolved values as attributes on the instance.
Resolves the final value for each configuration item and sets it as
an attribute on the GetConfig instance.
This allows users to access configuration values directly as attributes.
Source code in fejao_config_parser/src/get_config.py
224
225
226
227
228
229
230
231
232
233
234
235
236 | def __set_values_used(self) -> None:
"""Assign final resolved values as attributes on the instance.
Resolves the final value for each configuration item and sets it as
an attribute on the `GetConfig` instance.
This allows users to access configuration values directly as attributes.
Returns:
None
"""
for value in self.config:
setattr(self, value.get("name"), self.__set_single_value(value_name=value.get("name")))
|