From 1aa0158231a557d889254918534cac6b9da8f949 Mon Sep 17 00:00:00 2001 From: Angelos Evripiotis <jevripiotis@bloomberg.net> Date: Thu, 15 Nov 2018 10:23:30 +0000 Subject: [PATCH] _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'. --- buildstream/_context.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/buildstream/_context.py b/buildstream/_context.py index 6431559f85..277966c8b2 100644 --- a/buildstream/_context.py +++ b/buildstream/_context.py @@ -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 -- GitLab