summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-16 18:31:19 +0100
committerbst-marge-bot <marge-bot@buildstream.build>2019-07-17 08:26:26 +0000
commitb2a91466511f60e33e2e77fed2bf9c60bc31803d (patch)
treea34e90c980bf95d96c5db60a822e8954edf5c150 /src
parenta7ac4fed521881f54b76b199233c5aee3ba1f1ce (diff)
downloadbuildstream-b2a91466511f60e33e2e77fed2bf9c60bc31803d.tar.gz
node: Add 'get_str_list' on 'MappingNode'
`mapping.get_sequence(...).as_str_list()` is a very common pattern seen both in plugins and the core. Adding a helper to reduce the number of operations will make usage smoother
Diffstat (limited to 'src')
-rw-r--r--src/buildstream/_loader/loader.py2
-rw-r--r--src/buildstream/_options/optionenum.py2
-rw-r--r--src/buildstream/_options/optionflags.py2
-rw-r--r--src/buildstream/_plugincontext.py2
-rw-r--r--src/buildstream/_project.py8
-rw-r--r--src/buildstream/buildelement.py2
-rw-r--r--src/buildstream/element.py6
-rw-r--r--src/buildstream/node.pxd1
-rw-r--r--src/buildstream/node.pyx22
-rw-r--r--src/buildstream/plugins/elements/compose.py4
-rw-r--r--src/buildstream/plugins/sources/pip.py4
11 files changed, 39 insertions, 16 deletions
diff --git a/src/buildstream/_loader/loader.py b/src/buildstream/_loader/loader.py
index 207de9adb..a17eaf9e1 100644
--- a/src/buildstream/_loader/loader.py
+++ b/src/buildstream/_loader/loader.py
@@ -487,7 +487,7 @@ class Loader():
node.get_mapping(Symbol.CONFIG, default={}),
node.get_mapping(Symbol.VARIABLES, default={}),
node.get_mapping(Symbol.ENVIRONMENT, default={}),
- node.get_sequence(Symbol.ENV_NOCACHE, default=[]).as_str_list(),
+ node.get_str_list(Symbol.ENV_NOCACHE, default=[]),
node.get_mapping(Symbol.PUBLIC, default={}),
node.get_mapping(Symbol.SANDBOX, default={}),
element_kind == 'junction')
diff --git a/src/buildstream/_options/optionenum.py b/src/buildstream/_options/optionenum.py
index d1a7a85c9..3d5053639 100644
--- a/src/buildstream/_options/optionenum.py
+++ b/src/buildstream/_options/optionenum.py
@@ -45,7 +45,7 @@ class OptionEnum(Option):
node.validate_keys(valid_symbols)
- self.values = node.get_sequence('values', default=[]).as_str_list()
+ self.values = node.get_str_list('values', default=[])
if not self.values:
raise LoadError(LoadErrorReason.INVALID_DATA,
"{}: No values specified for {} option '{}'"
diff --git a/src/buildstream/_options/optionflags.py b/src/buildstream/_options/optionflags.py
index 80dd1b55d..64149d28e 100644
--- a/src/buildstream/_options/optionflags.py
+++ b/src/buildstream/_options/optionflags.py
@@ -93,4 +93,4 @@ class OptionFlags(Option):
def load_valid_values(self, node):
# Allow the more descriptive error to raise when no values
# exist rather than bailing out here (by specifying default_value)
- return node.get_sequence('values', default=[]).as_str_list()
+ return node.get_str_list('values', default=[])
diff --git a/src/buildstream/_plugincontext.py b/src/buildstream/_plugincontext.py
index 2442e306f..6b5d84e3b 100644
--- a/src/buildstream/_plugincontext.py
+++ b/src/buildstream/_plugincontext.py
@@ -137,7 +137,7 @@ class PluginContext():
loaded_dependency = False
for origin in self._plugin_origins:
- if kind not in origin.get_sequence('plugins').as_str_list():
+ if kind not in origin.get_str_list('plugins'):
continue
if origin.get_str('origin') == 'local':
diff --git a/src/buildstream/_project.py b/src/buildstream/_project.py
index 6a2c0f347..95afc78b5 100644
--- a/src/buildstream/_project.py
+++ b/src/buildstream/_project.py
@@ -591,10 +591,10 @@ class Project():
defaults = pre_config_node.get_mapping('defaults')
defaults.validate_keys(['targets'])
- self._default_targets = defaults.get_sequence("targets").as_str_list()
+ self._default_targets = defaults.get_str_list("targets")
# Fatal warnings
- self._fatal_warnings = pre_config_node.get_sequence('fatal-warnings', default=[]).as_str_list()
+ self._fatal_warnings = pre_config_node.get_str_list('fatal-warnings', default=[])
self.loader = Loader(self._context, self,
parent=parent_loader, fetch_subprojects=fetch_subprojects)
@@ -668,7 +668,7 @@ class Project():
# Load sandbox environment variables
self.base_environment = config.get_mapping('environment')
- self.base_env_nocache = config.get_sequence('environment-nocache').as_str_list()
+ self.base_env_nocache = config.get_str_list('environment-nocache')
# Load sandbox configuration
self._sandbox = config.get_mapping('sandbox')
@@ -700,7 +700,7 @@ class Project():
# Parse shell options
shell_options = config.get_mapping('shell')
shell_options.validate_keys(['command', 'environment', 'host-files'])
- self._shell_command = shell_options.get_sequence('command').as_str_list()
+ self._shell_command = shell_options.get_str_list('command')
# Perform environment expansion right away
shell_environment = shell_options.get_mapping('environment', default={})
diff --git a/src/buildstream/buildelement.py b/src/buildstream/buildelement.py
index b79876843..b33acfe12 100644
--- a/src/buildstream/buildelement.py
+++ b/src/buildstream/buildelement.py
@@ -281,7 +281,7 @@ class BuildElement(Element):
# Private Local Methods #
#############################################################
def __get_commands(self, node, name):
- raw_commands = node.get_sequence(name, []).as_str_list()
+ raw_commands = node.get_str_list(name, [])
return [
self.substitute_variables(command)
for command in raw_commands
diff --git a/src/buildstream/element.py b/src/buildstream/element.py
index 5856f3241..af171621c 100644
--- a/src/buildstream/element.py
+++ b/src/buildstream/element.py
@@ -816,7 +816,7 @@ class Element(Plugin):
if bstdata is not None:
with sandbox.batch(SandboxFlags.NONE):
- commands = bstdata.get_sequence('integration-commands', []).as_str_list()
+ commands = bstdata.get_str_list('integration-commands', [])
for command in commands:
cmd = self.substitute_variables(command)
@@ -2624,7 +2624,7 @@ class Element(Plugin):
else:
project_nocache = project.base_env_nocache
- default_nocache = cls.__defaults.get_sequence('environment-nocache', default=[]).as_str_list()
+ default_nocache = cls.__defaults.get_str_list('environment-nocache', default=[])
element_nocache = meta.env_nocache
# Accumulate values from the element default, the project and the element
@@ -2866,7 +2866,7 @@ class Element(Plugin):
# If this ever changes, things will go wrong unexpectedly.
if not self.__whitelist_regex:
bstdata = self.get_public_data('bst')
- whitelist = bstdata.get_sequence('overlap-whitelist', default=[]).as_str_list()
+ whitelist = bstdata.get_str_list('overlap-whitelist', default=[])
whitelist_expressions = [utils._glob2re(self.__variables.subst(exp.strip())) for exp in whitelist]
expression = ('^(?:' + '|'.join(whitelist_expressions) + ')$')
self.__whitelist_regex = re.compile(expression)
diff --git a/src/buildstream/node.pxd b/src/buildstream/node.pxd
index fdfa06c70..18520146d 100644
--- a/src/buildstream/node.pxd
+++ b/src/buildstream/node.pxd
@@ -52,6 +52,7 @@ cdef class MappingNode(Node):
cpdef ScalarNode get_scalar(self, str key, default=*)
cpdef SequenceNode get_sequence(self, str key, object default=*)
cpdef str get_str(self, str key, object default=*)
+ cpdef list get_str_list(self, str key, object default=*)
cpdef object items(self)
cpdef list keys(self)
cpdef void safe_del(self, str key)
diff --git a/src/buildstream/node.pyx b/src/buildstream/node.pyx
index aa1ff609d..fc17b8efa 100644
--- a/src/buildstream/node.pyx
+++ b/src/buildstream/node.pyx
@@ -679,6 +679,28 @@ cdef class MappingNode(Node):
cdef ScalarNode scalar = self.get_scalar(key, default)
return scalar.as_str()
+ cpdef list get_str_list(self, str key, object default=_sentinel):
+ """get_str_list(key, default=sentinel)
+
+ Get the value of the node for `key` as a list of strings.
+
+ This is equivalent to: :code:`mapping.get_sequence(my_key, my_default).as_str_list()`.
+
+ Args:
+ key (str): key for which to get the value
+ default (str): default value to return if `key` is not in the mapping
+
+ Raises:
+ :class:`buildstream._exceptions.LoadError`: if the value at `key` is not a
+ :class:`.SequenceNode` or if any
+ of its internal values is not a ScalarNode.
+
+ Returns:
+ :class:`list`: the value at `key` or the default
+ """
+ cdef SequenceNode sequence = self.get_sequence(key, default)
+ return sequence.as_str_list()
+
cpdef object items(self):
"""Get a new view of the mapping items ((key, value) pairs).
diff --git a/src/buildstream/plugins/elements/compose.py b/src/buildstream/plugins/elements/compose.py
index 83501d817..1c523eeb2 100644
--- a/src/buildstream/plugins/elements/compose.py
+++ b/src/buildstream/plugins/elements/compose.py
@@ -66,8 +66,8 @@ class ComposeElement(Element):
# We name this variable 'integration' only to avoid
# collision with the Element.integrate() method.
self.integration = node.get_bool('integrate')
- self.include = node.get_sequence('include').as_str_list()
- self.exclude = node.get_sequence('exclude').as_str_list()
+ self.include = node.get_str_list('include')
+ self.exclude = node.get_str_list('exclude')
self.include_orphans = node.get_bool('include-orphans')
def preflight(self):
diff --git a/src/buildstream/plugins/sources/pip.py b/src/buildstream/plugins/sources/pip.py
index 78c11fd89..40ddf8c68 100644
--- a/src/buildstream/plugins/sources/pip.py
+++ b/src/buildstream/plugins/sources/pip.py
@@ -114,8 +114,8 @@ class PipSource(Source):
self.ref = node.get_str('ref', None)
self.original_url = node.get_str('url', _PYPI_INDEX_URL)
self.index_url = self.translate_url(self.original_url)
- self.packages = node.get_sequence('packages', []).as_str_list()
- self.requirements_files = node.get_sequence('requirements-files', []).as_str_list()
+ self.packages = node.get_str_list('packages', [])
+ self.requirements_files = node.get_str_list('requirements-files', [])
if not (self.packages or self.requirements_files):
raise SourceError("{}: Either 'packages' or 'requirements-files' must be specified". format(self))