From efa866595c1aec6a089a8f1e2efd57e142de3da4 Mon Sep 17 00:00:00 2001
From: burri0000 <dominik.burri@unibas.ch>
Date: Wed, 14 Jul 2021 09:59:24 +0200
Subject: [PATCH] Report missing required keys, restructure rule config
 handling.

---
 workflow/Snakefile | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/workflow/Snakefile b/workflow/Snakefile
index e5d270b..346f081 100644
--- a/workflow/Snakefile
+++ b/workflow/Snakefile
@@ -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.")
 
-- 
GitLab