Skip to content
Snippets Groups Projects
Commit efa86659 authored by Dominik Burri's avatar Dominik Burri
Browse files

Report missing required keys, restructure rule config handling.

parent 7e050f29
No related branches found
No related tags found
1 merge request!91Config validation
Pipeline #12880 passed
......@@ -28,36 +28,37 @@ try:
with open(template_config_path) as _file:
template_config = yaml.safe_load(_file)
logger.info(f"Loaded template config from {template_config_path}.")
# Check if config contains required fields in example config
for key in template_config:
if key != 'optional':
assert key in config
except FileNotFoundError:
logger.error(f"No config file found at {template_config_path}! ")
raise
except AssertionError:
logger.error(f"Required config fields from {template_config_path} do not match {config.keys()}")
raise
# Check if config contains required fields in example config
missing = [key for key in template_config if key not in config and key != 'optional']
if missing:
err_msg = f"Required keys missing: {missing}"
logger.error(err_msg)
raise ValueError(err_msg)
# Check if optional field available
if 'optional' not in config:
if 'optional' not in config or config['optional'] != dict:
logger.info(f'No "optional" field found or no valid configuration for config.yaml.')
config['optional'] = {}
# Check optional fields and include in config if not present
for optkey, value in template_config['optional'].items():
if optkey not in config['optional']:
config['optional'][optkey] = value
logger.info(f"Added optional field: \"{optkey}\" with value: \"{value}\" to config.")
logger.info(f'Set default value for optional parameter "{optkey}" to : "{value}"')
# Parse YAML rule config file
if 'rule_config' in config['optional'] and config['optional']['rule_config']:
try:
with open(config['optional']['rule_config']) as _file:
rule_config = yaml.safe_load(_file)
logger.info(f"Loaded rule_config from {config['optional']['rule_config']}.")
except FileNotFoundError:
logger.error(f"No rule config file found at {config['optional']['rule_config']}. Either provide file or remove rule_config parameter from config.yaml! ")
raise
else:
try:
with open(config['optional']['rule_config']) as _file:
rule_config = yaml.safe_load(_file)
logger.info(f"Loaded rule_config from {config['optional']['rule_config']}.")
except TypeError:
logger.error(f'No string supplied at field "rule_config", but: {config["optional"]["rule_config"]}')
except FileNotFoundError:
logger.error(f"No rule config file found at {config['optional']['rule_config']}. Either provide file or remove rule_config parameter from config.yaml! ")
raise
except KeyError:
rule_config = {}
logger.warning(f"No rule config specified: using default values for all tools.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment