import json
import os
def aggregate_config(config_dir, fallback_values):
"""
Aggregates configuration values from files in a directory, using fallback values if a value is missing.
Args:
config_dir (str): The directory containing the configuration files.
fallback_values (dict): A dictionary containing default values to use if a configuration file doesn't contain a specific key.
Returns:
dict: A dictionary containing the aggregated configuration values.
"""
aggregated_config = fallback_values.copy() # Start with fallback values
for filename in os.listdir(config_dir):
if filename.endswith(".json"):
filepath = os.path.join(config_dir, filename)
try:
with open(filepath, "r") as f:
config = json.load(f)
for key, value in config.items():
if key not in aggregated_config:
aggregated_config[key] = value # Add new key/value pair
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Error reading {filename}: {e}")
# Optionally log the error to a file
return aggregated_config
Add your comment