Skip to content

Commit

Permalink
♻️ SSOT preconfig_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
shnizzedy committed Nov 8, 2023
1 parent 907fab5 commit fd4c83c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
10 changes: 5 additions & 5 deletions CPAC/utils/configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def __init__(self, config_map=None):
config_map = update_nested_dict(base_config.dict(), config_map)
else:
# base everything on blank pipeline for unspecified keys
with open(preconfig_yaml('blank'), 'r', encoding='utf-8') as _f:
config_map = update_nested_dict(yaml.safe_load(_f), config_map)
config_map = update_nested_dict(
preconfig_yaml('blank', load=True), config_map)

config_map = self._nonestr_to_None(config_map)

Expand Down Expand Up @@ -636,9 +636,9 @@ def preconfig_yaml(preconfig_name='default', load=False):
from CPAC.pipeline import ALL_PIPELINE_CONFIGS, AVAILABLE_PIPELINE_CONFIGS
if preconfig_name not in ALL_PIPELINE_CONFIGS:
raise BadParameter(
"The pre-configured pipeline name '{0}' you provided is not one "
"of the available pipelines.\n\nAvailable pipelines:\n"
f"{1}\n".format(preconfig_name, str(AVAILABLE_PIPELINE_CONFIGS)),
f"The pre-configured pipeline name '{preconfig_name}' you "
"provided is not one of the available pipelines.\n\nAvailable "
f"pipelines:\n{str(AVAILABLE_PIPELINE_CONFIGS)}\n",
param='preconfig')
if load:
with open(preconfig_yaml(preconfig_name), 'r', encoding='utf-8') as _f:
Expand Down
13 changes: 9 additions & 4 deletions CPAC/utils/configuration/yaml_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import re
from datetime import datetime
from hashlib import sha1
from click import BadParameter
import yaml

from CPAC.utils.configuration import Configuration, Preconfiguration, \
Expand Down Expand Up @@ -58,16 +59,20 @@ def __init__(self, original_yaml, base_config=None):
base_config : Configuration, optional
"""
preconfig_path = preconfig_yaml(original_yaml)
if os.path.exists(preconfig_path):
original_yaml = preconfig_path
try:
original_yaml = preconfig_yaml(original_yaml)
except BadParameter:
pass
if os.path.exists(original_yaml):
with open(original_yaml, 'r', encoding='utf-8') as _f:
original_yaml = _f.read()
self.comments = {}
self.template = original_yaml
if base_config is None:
self._dict = yaml.safe_load(self.template)
if isinstance(self.template, dict):
self._dict = self.template
if isinstance(self.template, str):
self._dict = yaml.safe_load(self.template)
else:
self._dict = base_config.dict()
self._parse_comments()
Expand Down
3 changes: 1 addition & 2 deletions CPAC/utils/tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def test_yaml_template():
# Create a new YAML configuration file based on the default pipeline
# YAML file.
pipeline_file = preconfig_yaml('default')
with open(pipeline_file, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
config = preconfig_yaml('default', load=True)
new_config = create_yaml_from_template(config, pipeline_file)
with open(config_file, 'w', encoding='utf-8') as f:
f.write(new_config)
Expand Down
7 changes: 5 additions & 2 deletions dev/docker_data/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,14 @@ def run_main():
run(f"bids-validator {bids_dir}")

if args.preconfig:
args.pipeline_file = preconfig_yaml(args.preconfig, load=True)
args.pipeline_file = preconfig_yaml(args.preconfig)

# otherwise, if we are running group, participant, or dry run we
# begin by conforming the configuration
c = load_yaml_config(args.pipeline_file, args.aws_input_creds)
if isinstance(args.pipeline_file, dict):
c = args.pipeline_file
else:
c = load_yaml_config(args.pipeline_file, args.aws_input_creds)

if 'pipeline_setup' not in c:
_url = (f'{DOCS_URL_PREFIX}/user/pipelines/'
Expand Down

0 comments on commit fd4c83c

Please sign in to comment.