Skip to content

Commit

Permalink
feat: extract yaml loading to a function and add one to expand any su…
Browse files Browse the repository at this point in the history
…b `config_file` + error handling
  • Loading branch information
KernAttila committed Mar 28, 2023
1 parent 76383d8 commit 4a57c96
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions cuesubmit/cuesubmit/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,55 @@

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') 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:
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."""

0 comments on commit 4a57c96

Please sign in to comment.