@@ -146,34 +146,51 @@ def validate_required_env_vars(self) -> None:
146
146
Raises:
147
147
ConfigurationError: If any required environment variables are missing
148
148
"""
149
+ # Load the config file
149
150
try :
150
151
with open (self .config_path , "rb" ) as f :
151
152
config = tomllib .load (f )
153
+
154
+ # If there is an error, raise a ConfigurationError
152
155
except Exception as e :
153
156
raise ConfigurationError (f"Cannot validate env vars - config error: { e } " ) from e
154
157
155
- missing_vars = []
156
-
157
- def check_env_vars (obj ):
158
- if isinstance (obj , str ):
159
- env_vars = self ._env_var_pattern .findall (obj )
160
- for var in env_vars :
161
- if os .getenv (var ) is None :
162
- missing_vars .append (var )
163
- elif isinstance (obj , dict ):
164
- for value in obj .values ():
165
- check_env_vars (value )
166
- elif isinstance (obj , list ):
167
- for item in obj :
168
- check_env_vars (item )
169
-
170
- check_env_vars (config )
158
+ # Collect all missing environment variables from config object
159
+ missing_vars = self ._collect_missing_env_vars (config )
171
160
161
+ # If there are missing variables, raise a ConfigurationError
172
162
if missing_vars :
173
163
raise ConfigurationError (
174
164
f"Missing required environment variables: { ', ' .join (sorted (set (missing_vars )))} "
175
165
)
176
166
167
+ def _collect_missing_env_vars (self , obj : Any ) -> list [str ]:
168
+ """
169
+ Collect all missing environment variables from config object.
170
+
171
+ Args:
172
+ obj: config object to collect missing environment variables from
173
+
174
+ Returns:
175
+ list of missing environment variables (if any)
176
+ """
177
+ missing_vars = []
178
+ # Collect the missing enviroment vaiables using the appropriate speicifc method
179
+ if isinstance (obj , str ):
180
+ env_vars = self ._env_var_pattern .findall (obj )
181
+ for var in env_vars :
182
+ if os .getenv (var ) is None :
183
+ missing_vars .append (var )
184
+ elif isinstance (obj , dict ):
185
+ for value in obj .values ():
186
+ missing_vars .extend (self ._collect_missing_env_vars (value ))
187
+ elif isinstance (obj , list ):
188
+ for item in obj :
189
+ missing_vars .extend (self ._collect_missing_env_vars (item ))
190
+
191
+ # After all the missing variables have been collected, return the list
192
+ return missing_vars
193
+
177
194
def get_flat_config (self ) -> dict [str , Any ]:
178
195
"""
179
196
Get configuration in flat format.
0 commit comments