diff options
author | Benjamin Schubert <contact@benschubert.me> | 2019-06-11 07:55:21 +0100 |
---|---|---|
committer | Benjamin Schubert <contact@benschubert.me> | 2019-06-25 21:08:50 +0100 |
commit | 8ee5b3722f9a20c384748ce9298ff9a5a1a56583 (patch) | |
tree | 338bcc8a01651bf2fe7cd30f85026d82e26ea662 | |
parent | b68b6d14564584df0da60f41483021568bd91b91 (diff) | |
download | buildstream-8ee5b3722f9a20c384748ce9298ff9a5a1a56583.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
-rw-r--r-- | src/buildstream/_context.py | 15 | ||||
-rw-r--r-- | src/buildstream/_project.py | 6 | ||||
-rw-r--r-- | src/buildstream/_workspaces.py | 2 | ||||
-rw-r--r-- | src/buildstream/_yaml.pxd | 2 | ||||
-rw-r--r-- | src/buildstream/_yaml.pyx | 15 | ||||
-rw-r--r-- | src/buildstream/element.py | 4 | ||||
-rw-r--r-- | src/buildstream/plugins/sources/patch.py | 2 | ||||
-rw-r--r-- | tests/internals/yaml.py | 2 |
8 files changed, 33 insertions, 15 deletions
diff --git a/src/buildstream/_context.py b/src/buildstream/_context.py index f32f5aa91..f78df32da 100644 --- a/src/buildstream/_context.py +++ b/src/buildstream/_context.py @@ -286,11 +286,12 @@ class Context(): 'error-lines', 'message-lines', 'debug', 'element-format', 'message-format' ]) - self.log_key_length = _yaml.node_get(logging, int, 'key-length') + self.log_key_length = logging.get_int('key-length') self.log_debug = logging.get_bool('debug') self.log_verbose = logging.get_bool('verbose') - self.log_error_lines = _yaml.node_get(logging, int, 'error-lines') - self.log_message_lines = _yaml.node_get(logging, int, 'message-lines') + self.log_error_lines = logging.get_int('error-lines') + self.log_message_lines = logging.get_int('message-lines') + self.log_message_lines = logging.get_int('message-lines') self.log_element_format = logging.get_str('element-format') self.log_message_format = logging.get_str('message-format') @@ -302,10 +303,10 @@ class Context(): ]) 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') - self.sched_network_retries = _yaml.node_get(scheduler, int, 'network-retries') + self.sched_fetchers = scheduler.get_int('fetchers') + self.sched_builders = scheduler.get_int('builders') + self.sched_pushers = scheduler.get_int('pushers') + self.sched_network_retries = scheduler.get_int('network-retries') # Load per-projects overrides self._project_overrides = defaults.get_mapping('projects', default={}) diff --git a/src/buildstream/_project.py b/src/buildstream/_project.py index 6157206ff..66253b5ea 100644 --- a/src/buildstream/_project.py +++ b/src/buildstream/_project.py @@ -573,7 +573,7 @@ class Project(): _yaml.composite(pre_config_node, self._project_conf) # Assert project's format version early, before validating toplevel keys - format_version = _yaml.node_get(pre_config_node, int, 'format-version') + format_version = pre_config_node.get_int('format-version') if BST_FORMAT_VERSION < format_version: major, minor = utils.get_bst_version() raise LoadError( @@ -894,7 +894,7 @@ class Project(): raise LoadError( LoadErrorReason.INVALID_YAML, "Duplicate listing of source '{}'".format(key)) - source_format_versions[key] = _yaml.node_get(source_versions, int, key) + source_format_versions[key] = source_versions.get_int(key) # Store element versions for checking later element_versions = origin.get_mapping('elements', default={}) @@ -903,7 +903,7 @@ class Project(): raise LoadError( LoadErrorReason.INVALID_YAML, "Duplicate listing of element '{}'".format(key)) - element_format_versions[key] = _yaml.node_get(element_versions, int, key) + element_format_versions[key] = element_versions.get_int(key) # Store the origins if they're not 'core'. # core elements are loaded by default, so storing is unnecessary. diff --git a/src/buildstream/_workspaces.py b/src/buildstream/_workspaces.py index bcf0ac92c..3847c7a85 100644 --- a/src/buildstream/_workspaces.py +++ b/src/buildstream/_workspaces.py @@ -570,7 +570,7 @@ class Workspaces(): # def _parse_workspace_config(self, workspaces): try: - version = _yaml.node_get(workspaces, int, 'format-version', default_value=0) + version = workspaces.get_int('format-version', default=0) except ValueError: raise LoadError(LoadErrorReason.INVALID_DATA, "Format version is not an integer in workspace configuration") diff --git a/src/buildstream/_yaml.pxd b/src/buildstream/_yaml.pxd index d308a23a8..146bec0fc 100644 --- a/src/buildstream/_yaml.pxd +++ b/src/buildstream/_yaml.pxd @@ -33,11 +33,13 @@ cdef class MappingNode(Node): cpdef MappingNode get_mapping(self, str key, default=*) cpdef ScalarNode get_scalar(self, str key, default=*) cpdef bint get_bool(self, str key, default=*) except * + cpdef int get_int(self, str key, default=*) except * cpdef str get_str(self, str key, object default=*) cdef class ScalarNode(Node): cpdef bint as_bool(self) except * + cpdef int as_int(self) except * cpdef str as_str(self) cpdef bint is_none(self) diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index eb73ef4d6..71c3b7949 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: @@ -165,6 +175,11 @@ 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 *: + # 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_int() + 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) diff --git a/src/buildstream/element.py b/src/buildstream/element.py index 05e590823..a175ee1d2 100644 --- a/src/buildstream/element.py +++ b/src/buildstream/element.py @@ -2709,8 +2709,8 @@ class Element(Plugin): build_arch = host_arch return SandboxConfig( - _yaml.node_get(sandbox_config, int, 'build-uid'), - _yaml.node_get(sandbox_config, int, 'build-gid'), + sandbox_config.get_int('build-uid'), + sandbox_config.get_int('build-gid'), sandbox_config.get_str('build-os', default=host_os), build_arch) diff --git a/src/buildstream/plugins/sources/patch.py b/src/buildstream/plugins/sources/patch.py index e42868264..01117db78 100644 --- a/src/buildstream/plugins/sources/patch.py +++ b/src/buildstream/plugins/sources/patch.py @@ -57,7 +57,7 @@ class PatchSource(Source): def configure(self, node): self.path = self.node_get_project_path(node, 'path', check_is_file=True) - self.strip_level = self.node_get_member(node, int, "strip-level", 1) + self.strip_level = node.get_int("strip-level", default=1) self.fullpath = os.path.join(self.get_project_directory(), self.path) def preflight(self): diff --git a/tests/internals/yaml.py b/tests/internals/yaml.py index 012449ee4..0e747fa48 100644 --- a/tests/internals/yaml.py +++ b/tests/internals/yaml.py @@ -451,7 +451,7 @@ def test_value_doesnt_match_expected(datafiles): test_dict = _yaml.load(conf_file) with pytest.raises(LoadError) as exc: - _yaml.node_get(test_dict, int, "Test4") + test_dict.get_int("Test4") assert exc.value.reason == LoadErrorReason.INVALID_DATA |