summaryrefslogtreecommitdiff
path: root/src/buildstream/_yaml.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildstream/_yaml.pyx')
-rw-r--r--src/buildstream/_yaml.pyx27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx
index 9d9a12400..a7779c41e 100644
--- a/src/buildstream/_yaml.pyx
+++ b/src/buildstream/_yaml.pyx
@@ -78,11 +78,19 @@ cdef class Node:
cdef class ScalarNode(Node):
def __init__(self, object value, int file_index, int line, int column):
+ if type(value) is str:
+ value = value.strip()
self.value = value
self.file_index = file_index
self.line = line
self.column = column
+ cpdef str as_str(self):
+ # We keep 'None' as 'None' to simplify the API's usage and allow chaining for users
+ if self.value is None:
+ return None
+ return str(self.value)
+
cdef class MappingNode(Node):
@@ -119,6 +127,25 @@ cdef class MappingNode(Node):
return value
+ cpdef ScalarNode get_scalar(self, str key, object default=_sentinel):
+ value = self.get(key, default, ScalarNode)
+
+ if type(value) is not ScalarNode:
+ if value is None:
+ value = ScalarNode(None, _SYNTHETIC_FILE_INDEX, 0, next_synthetic_counter())
+ else:
+ provenance = node_get_provenance(value)
+ raise LoadError(LoadErrorReason.INVALID_DATA,
+ "{}: Value of '{}' is not of the expected type 'Scalar'"
+ .format(provenance, key))
+
+ return value
+
+ cpdef str get_str(self, str key, object default=_sentinel):
+ # TODO: don't go through 'get_scalar', we can directly get everything and optimize
+ cdef ScalarNode scalar = self.get_scalar(key, default)
+ return scalar.as_str()
+
class SequenceNode(Node):
def __init__(self, list value, int file_index, int line, int column):