-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
61 lines (50 loc) · 1.8 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# config.py
import yaml
def load_config(file_path):
"""
Load the configuration from a YAML file.
Args:
file_path (str): The path to the YAML configuration file.
Returns:
dict: The loaded configuration dictionary, or None if an error occurs.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
return config
except FileNotFoundError:
print(f"Configuration file not found: {file_path}")
return None
except yaml.YAMLError as e:
print(f"Error loading configuration file: {str(e)}")
return None
def validate_config(config, required_keys):
"""
Validate the configuration dictionary to ensure all required keys are present.
Args:
config (dict): The configuration dictionary to validate.
required_keys (list): A list of keys that must be present in the configuration.
Returns:
bool: True if all required keys are present, False otherwise.
"""
missing_keys = [key for key in required_keys if key not in config]
if missing_keys:
print(f"Missing required configuration keys: {missing_keys}")
return False
return True
def get_config(file_path, required_keys):
"""
Load and validate the configuration.
Args:
file_path (str): The path to the YAML configuration file.
required_keys (list): A list of keys that must be present in the configuration.
Returns:
dict: The validated configuration dictionary, or None if validation fails.
"""
config = load_config(file_path)
if config is None:
return None
if validate_config(config, required_keys):
return config
else:
return None