diff --git a/cuesubmit/cuesubmit/Config.py b/cuesubmit/cuesubmit/Config.py index 3bfb443a7..d2906d9a2 100644 --- a/cuesubmit/cuesubmit/Config.py +++ b/cuesubmit/cuesubmit/Config.py @@ -35,19 +35,59 @@ def getConfigValues(): """Reads the config file from disk and returns the values it defines.""" - configData = {} configFile = os.environ.get(CONFIG_FILE_ENV_VAR) if not configFile: configFile = os.path.join(opencue.config.config_base_directory(), 'cuesubmit.yaml') - if os.path.exists(configFile): - with open(configFile, 'r', encoding='utf-8') as data: - try: - configData = yaml.load(data, Loader=yaml.SafeLoader) - except yaml.YAMLError: - raise CuesubmitConfigError("Could not load yaml file: {}. Please check its " - "formatting".format(configFile)) + configData = _loadYamlFile(yaml_file=configFile) + if 'RENDER_CMDS' in configData: + # look for any sub-config files and load them + configData['RENDER_CMDS'] = _expandRenderConfigValues(configData['RENDER_CMDS']) return configData +def _loadYamlFile(yaml_file): + """ Load config yaml as dict + :param yaml_file: path to a config.yaml file (path can be an env var) + :type yaml_file: str + :returns: yaml content + :rtype: dict + """ + _yaml_file = os.path.expandvars(yaml_file) + if not os.path.exists(_yaml_file): + raise FileExistsError(f'{_yaml_file=} not found') + config_data = {} + with open(_yaml_file, 'r') as data: + try: + config_data = yaml.load(data, Loader=yaml.SafeLoader) + except yaml.YAMLError: + raise CuesubmitConfigError("Could not load yaml file: {}. Please check its " + "formatting".format(_yaml_file)) + return config_data + + +def _expandRenderConfigValues(RENDER_CMDS): + """ Looks through each render command and loads their 'config_file' if any + If 'config_file' is set but does not exist, replace its content with error for proper feedback + + :param RENDER_CMDS: all render commands from the cuesubmit_config.yaml file + :type RENDER_CMDS: dict + :returns: Updated RENDER_CMDS + :rtype: dict + """ + for job_type, _options in RENDER_CMDS.items(): + _sub_config_file = _options.get('config_file') + if not _sub_config_file: + continue + try: + RENDER_CMDS[job_type] = _loadYamlFile(yaml_file=_sub_config_file) + except FileExistsError as error: + RENDER_CMDS[job_type] = { + 'command': 'error', + 'options': { + '{ERROR}': error} + } + return RENDER_CMDS + + class CuesubmitConfigError(Exception): """Thrown when an error occurs reading the config file."""