summaryrefslogtreecommitdiff
path: root/src/buildstream/_yaml.pyx
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2019-06-11 07:55:21 +0100
committerbst-marge-bot <marge-bot@buildstream.build>2019-07-15 14:14:02 +0000
commit4a56ebe6e589e2aa5d8da3fc2b76f76a19aa03b7 (patch)
treeffbdb49a64bd27f0ce0d9e421170a60f6db6706d /src/buildstream/_yaml.pyx
parentd14b809cc7b56b7be9e3e40c2e12e8f7245ba680 (diff)
downloadbuildstream-4a56ebe6e589e2aa5d8da3fc2b76f76a19aa03b7.tar.gz
_yaml: Add 'as_int()' on ScalarNode
- Add the 'as_int()' method on 'ScalarNode' to replace 'node_get(mapping, key, int)' - Adapt all call sites to use the new API
Diffstat (limited to 'src/buildstream/_yaml.pyx')
-rw-r--r--src/buildstream/_yaml.pyx14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx
index 455abbc11..2c1714158 100644
--- a/src/buildstream/_yaml.pyx
+++ b/src/buildstream/_yaml.pyx
@@ -104,6 +104,16 @@ cdef class ScalarNode(Node):
"{}: Value of '{}' is not of the expected type '{}'"
.format(provenance, path, bool.__name__, self.value))
+ cpdef int as_int(self) except *:
+ try:
+ return int(self.value)
+ except ValueError:
+ provenance = node_get_provenance(self)
+ path = node_find_target(provenance.toplevel, self)[-1]
+ raise LoadError(LoadErrorReason.INVALID_DATA,
+ "{}: Value of '{}' is not of the expected type '{}'"
+ .format(provenance, path, int.__name__))
+
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:
@@ -164,6 +174,10 @@ cdef class MappingNode(Node):
cdef ScalarNode scalar = self.get_scalar(key, default)
return scalar.as_bool()
+ cpdef int get_int(self, str key, object default=_sentinel) except *:
+ cdef ScalarNode scalar = self.get_scalar(key, default)
+ return scalar.as_int()
+
cpdef str get_str(self, str key, object default=_sentinel):
cdef ScalarNode scalar = self.get_scalar(key, default)
return scalar.as_str()