28
28
29
29
def parse_config_file (path : str ) -> list [Resource ]:
30
30
"""
31
- Parse a configuration file and return a list of Resource objects.
32
-
33
- This function processes a filepath, extracting resource
34
- definitions and any exclusion rules. It performs validation on the configuration
35
- structure and creates Resource objects based on the provided specifications.
36
-
37
- Args:
38
- path (string): A dictionary containing the configuration data.
39
-
40
- Returns:
41
- List[Resource]: A list of instantiated Resource objects.
42
-
43
- Raises:
44
- KeyError: If the required resources block is missing from the config.
45
- DataError: If there are invalid keys or missing required keys in the config.
46
- InvalidResourceTypeError: If an invalid resource type is specified.
47
-
48
- The function expects the following structure in the config_json:
49
- - An optional 'exclude_block' key for specifying resources to exclude.
50
- - A required 'resources' key containing resource definitions.
51
-
52
- Each resource definition should include 'active', 'validate', and other
53
- required keys as specified in REQUIRED_RESOURCE_CONFIG_KEYS.
54
31
"""
55
32
56
33
# // TODO sanitise the path
@@ -66,29 +43,6 @@ def parse_config_file(path: str) -> list[Resource]:
66
43
67
44
def parse_config_dict (config_json : dict ) -> List [Resource ]:
68
45
"""
69
- Parse a configuration file and return a list of Resource objects.
70
-
71
- This function processes a JSON configuration dictionary, extracting resource
72
- definitions and any exclusion rules. It performs validation on the configuration
73
- structure and creates Resource objects based on the provided specifications.
74
-
75
- Args:
76
- config_json (dict): A dictionary containing the configuration data.
77
-
78
- Returns:
79
- List[Resource]: A list of instantiated Resource objects.
80
-
81
- Raises:
82
- KeyError: If the required resources block is missing from the config.
83
- DataError: If there are invalid keys or missing required keys in the config.
84
- InvalidResourceTypeError: If an invalid resource type is specified.
85
-
86
- The function expects the following structure in the config_json:
87
- - An optional 'exclude_block' key for specifying resources to exclude.
88
- - A required 'resources' key containing resource definitions.
89
-
90
- Each resource definition should include 'active', 'validate', and other
91
- required keys as specified in REQUIRED_RESOURCE_CONFIG_KEYS.
92
46
"""
93
47
out = []
94
48
exclude_block = {}
@@ -107,30 +61,39 @@ def parse_config_dict(config_json: dict) -> List[Resource]:
107
61
res_block : dict = config_json [RESOURCE_BLOCK_CONFIG_KEY ]
108
62
k : str
109
63
v : dict
64
+
65
+ # for res key, keys
110
66
for k , v in res_block .items ():
111
67
68
+ # If invalid resource
112
69
if k not in ALL_RESOURCE_TYPES :
113
70
raise InvalidResourceTypeError (f"invalid resource type: { k } " )
114
71
72
+ # If it contains an invalid config key
115
73
for i in v :
116
74
if i not in VALID_RESOURCE_CONFIG_KEYS :
117
75
raise DataError (f"invalid config key: { i } " )
118
76
77
+ # If missing a required config key
119
78
for i in REQUIRED_RESOURCE_CONFIG_KEYS :
120
79
if i not in v :
121
80
raise DataError (f"missing required config key: { i } " )
122
81
82
+ # If the resource should be validated
123
83
validate = v ["validate" ]
124
84
if not isinstance (validate , bool ):
125
85
raise AssertionError (f"validate key is of the wrong type: { validate } , { type (validate )} " )
126
86
87
+ # If the resource should be prossessed.
127
88
active = v ["active" ]
128
89
if not isinstance (active , bool ):
129
90
raise AssertionError (f"active key is of the wrong type: { active } , { type (active )} " )
130
91
92
+ # If inactive, skip
131
93
if not active :
132
94
continue
133
95
96
+
134
97
out .append (
135
98
RESOURCE_TYPE_OBJECT_MAP [k ](
136
99
options = Options ().from_json (v ),
0 commit comments