Skip to content
Snippets Groups Projects
Commit 1aa01582 authored by Angelos Evripiotis's avatar Angelos Evripiotis
Browse files

_context: refactor, extract _node_get_option_str

Use a new helper function to simplify working with nodes that can only
accept certain strings. This will be used when adding the prompt.*
config options.

In later work we can see if this function would be useful elsewhere, and
could be added to '_yaml.py'.
parent 31fd24b6
No related branches found
No related tags found
No related merge requests found
......@@ -206,7 +206,8 @@ class Context():
'on-error', 'fetchers', 'builders',
'pushers', 'network-retries'
])
self.sched_error_action = _yaml.node_get(scheduler, str, 'on-error')
self.sched_error_action = _node_get_option_str(
scheduler, 'on-error', ['continue', 'quit', 'terminate'])
self.sched_fetchers = _yaml.node_get(scheduler, int, 'fetchers')
self.sched_builders = _yaml.node_get(scheduler, int, 'builders')
self.sched_pushers = _yaml.node_get(scheduler, int, 'pushers')
......@@ -222,13 +223,6 @@ class Context():
profile_end(Topics.LOAD_CONTEXT, 'load')
valid_actions = ['continue', 'quit', 'terminate']
if self.sched_error_action not in valid_actions:
provenance = _yaml.node_get_provenance(scheduler, 'on-error')
raise LoadError(LoadErrorReason.INVALID_DATA,
"{}: on-error should be one of: {}".format(
provenance, ", ".join(valid_actions)))
@property
def artifactcache(self):
if not self._artifactcache:
......@@ -581,3 +575,30 @@ class Context():
os.environ['XDG_CONFIG_HOME'] = os.path.expanduser('~/.config')
if not os.environ.get('XDG_DATA_HOME'):
os.environ['XDG_DATA_HOME'] = os.path.expanduser('~/.local/share')
# _node_get_option_str()
#
# Fetches a value from a dictionary node, and makes sure it's one of the
# pre-defined options.
#
# Args:
# node (dict): The dictionary node
# key (str): The key to get a value for in node
# allowed_options (iterable): Only accept these values
#
# Returns:
# The value if found in node
#
# Raises:
# LoadError, when the value found is not of the expected type, or is not
# found.
#
def _node_get_option_str(node, key, allowed_options):
result = _yaml.node_get(node, str, key)
if result not in allowed_options:
provenance = _yaml.node_get_provenance(node, key)
raise LoadError(LoadErrorReason.INVALID_DATA,
"{}: {} should be one of: {}".format(
provenance, key, ", ".join(allowed_options)))
return result
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