summaryrefslogtreecommitdiff
path: root/src/buildstream/node.pyx
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-16 18:31:19 +0100
committerBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-16 18:32:48 +0100
commitf2ece705b7bf36d7d923264af2b1f51b66a9f9a7 (patch)
tree7df4a79b12013f6094371e86bcf52bb6aa8719e7 /src/buildstream/node.pyx
parentfbb8eea8334023e30c8719cb52f9b0e226b4eb8b (diff)
downloadbuildstream-bschubert/api-improvements.tar.gz
node: Add 'get_str_list' on 'MappingNode'bschubert/api-improvements
`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/buildstream/node.pyx')
-rw-r--r--src/buildstream/node.pyx22
1 files changed, 22 insertions, 0 deletions
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).