summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngelos Evripiotis <jevripiotis@bloomberg.net>2018-11-15 10:23:30 +0000
committerAngelos Evripiotis <jevripiotis@bloomberg.net>2018-11-20 11:17:38 +0000
commitb81c43331ef9c09e7252cef6b9ee04a605329fea (patch)
tree6bff0cca2e84f7ce2e2d27683a031e2ca76ce91a
parenteb2d376f14220ae5bcc60bac2622f2fd865d8b25 (diff)
downloadbuildstream-b81c43331ef9c09e7252cef6b9ee04a605329fea.tar.gz
_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'.
-rw-r--r--buildstream/_context.py37
1 files changed, 29 insertions, 8 deletions
diff --git a/buildstream/_context.py b/buildstream/_context.py
index d40210f20..55cce685b 100644
--- a/buildstream/_context.py
+++ b/buildstream/_context.py
@@ -214,7 +214,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')
@@ -230,13 +231,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:
@@ -589,3 +583,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()
+#
+# Like _yaml.node_get(), but also checks value is one of the allowed option
+# strings. 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 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