diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-10 14:20:23 +0100 |
---|---|---|
committer | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-10 14:20:23 +0100 |
commit | 95cadbb4a44324be81c373b3448aa878ce0e9c35 (patch) | |
tree | c53adf3e1891850b94f95a67b373a171d51f0ea0 | |
parent | 62b7863d578ac880b5a51353161a080c399ff88f (diff) | |
download | buildstream-bschubert/node-api-bak.tar.gz |
Add a as_bool to ScalarNodebschubert/node-api-bak
-rw-r--r-- | src/buildstream/_context.py | 8 | ||||
-rw-r--r-- | src/buildstream/_gitsourcebase.py | 6 | ||||
-rw-r--r-- | src/buildstream/_options/optionbool.py | 4 | ||||
-rw-r--r-- | src/buildstream/_project.py | 12 | ||||
-rw-r--r-- | src/buildstream/_variables.pyx | 2 | ||||
-rw-r--r-- | src/buildstream/_workspaces.py | 2 | ||||
-rw-r--r-- | src/buildstream/_yaml.pxd | 1 | ||||
-rw-r--r-- | src/buildstream/_yaml.pyx | 17 | ||||
-rw-r--r-- | tests/format/include.py | 6 |
9 files changed, 38 insertions, 20 deletions
diff --git a/src/buildstream/_context.py b/src/buildstream/_context.py index d385c5981..76719578a 100644 --- a/src/buildstream/_context.py +++ b/src/buildstream/_context.py @@ -273,7 +273,7 @@ class Context(): self.remote_execution_specs = SandboxRemote.specs_from_config_node(defaults) # Load pull build trees configuration - self.pull_buildtrees = _yaml.node_get(cache, bool, 'pull-buildtrees') + self.pull_buildtrees = cache.get_scalar('pull-buildtrees').as_bool() # Load cache build trees configuration self.cache_buildtrees = _node_get_option_str( @@ -287,8 +287,8 @@ class Context(): 'debug', 'element-format', 'message-format' ]) self.log_key_length = _yaml.node_get(logging, int, 'key-length') - self.log_debug = _yaml.node_get(logging, bool, 'debug') - self.log_verbose = _yaml.node_get(logging, bool, 'verbose') + self.log_debug = logging.get_scalar('debug').as_bool() + self.log_verbose = logging.get_scalar('verbose').as_bool() self.log_error_lines = _yaml.node_get(logging, int, 'error-lines') self.log_message_lines = _yaml.node_get(logging, int, 'message-lines') self.log_element_format = logging.get_scalar('element-format').as_str() @@ -423,7 +423,7 @@ class Context(): # so work out if we should be strict, and then cache the result toplevel = self.get_toplevel_project() overrides = self.get_overrides(toplevel.name) - self._strict_build_plan = _yaml.node_get(overrides, bool, 'strict', default_value=True) + self._strict_build_plan = overrides.get_scalar('strict', default=True).as_bool() # If it was set by the CLI, it overrides any config # Ditto if we've already computed this, then we return the computed diff --git a/src/buildstream/_gitsourcebase.py b/src/buildstream/_gitsourcebase.py index 8d3b8593e..7556a76d2 100644 --- a/src/buildstream/_gitsourcebase.py +++ b/src/buildstream/_gitsourcebase.py @@ -388,7 +388,7 @@ class _GitSourceBase(Source): self.node_validate(tag_node, ['tag', 'commit', 'annotated']) tags = self._load_tags(node) - self.track_tags = self.node_get_member(node, bool, 'track-tags', False) + self.track_tags = node.get_scalar('track-tags', False).as_bool() self.original_url = node.get_scalar('url').as_str() self.mirror = self.BST_MIRROR_CLASS(self, '', self.original_url, ref, tags=tags, primary=True) @@ -405,7 +405,7 @@ class _GitSourceBase(Source): raise SourceError("{}: Git sources require a ref and/or track".format(self), reason="missing-track-and-ref") - self.checkout_submodules = self.node_get_member(node, bool, 'checkout-submodules', True) + self.checkout_submodules = node.get_scalar('checkout-submodules', True).as_bool() self.submodules = [] # Parse a dict of submodule overrides, stored in the submodule_overrides @@ -423,7 +423,7 @@ class _GitSourceBase(Source): self.submodule_overrides[path] = url if 'checkout' in submodule: - checkout = self.node_get_member(submodule, bool, 'checkout') + checkout = submodule.get_scalar('checkout').as_bool() self.submodule_checkout_overrides[path] = checkout self.mark_download_url(self.original_url) diff --git a/src/buildstream/_options/optionbool.py b/src/buildstream/_options/optionbool.py index 137f53707..16ffd7224 100644 --- a/src/buildstream/_options/optionbool.py +++ b/src/buildstream/_options/optionbool.py @@ -34,13 +34,13 @@ class OptionBool(Option): super(OptionBool, self).load(node) _yaml.node_validate(node, OPTION_SYMBOLS + ['default']) - self.value = _yaml.node_get(node, bool, 'default') + self.value = node.get_scalar('default').as_bool() def load_value(self, node, *, transform=None): if transform: self.set_value(transform(node.get_scalar(self.name).as_str())) else: - self.value = _yaml.node_get(node, bool, self.name) + self.value = node.get_scalar(self.name).as_bool() def set_value(self, value): if value in ('True', 'true'): diff --git a/src/buildstream/_project.py b/src/buildstream/_project.py index a59bcee40..51cbf622d 100644 --- a/src/buildstream/_project.py +++ b/src/buildstream/_project.py @@ -692,13 +692,13 @@ class Project(): self._splits = config.get_mapping('split-rules') # Support backwards compatibility for fail-on-overlap - fail_on_overlap = _yaml.node_get(config, bool, 'fail-on-overlap', default_value=None) - - if (CoreWarnings.OVERLAPS not in self._fatal_warnings) and fail_on_overlap: - self._fatal_warnings.append(CoreWarnings.OVERLAPS) + try: + fail_on_overlap = config.get_scalar('fail-on-overlap').as_bool() - # Deprecation check - if fail_on_overlap is not None: + if (CoreWarnings.OVERLAPS not in self._fatal_warnings) and fail_on_overlap: + self._fatal_warnings.append(CoreWarnings.OVERLAPS) + except LoadError: + # Deprecation check self._context.message( Message( None, diff --git a/src/buildstream/_variables.pyx b/src/buildstream/_variables.pyx index b90ee722b..a37e83a78 100644 --- a/src/buildstream/_variables.pyx +++ b/src/buildstream/_variables.pyx @@ -120,7 +120,7 @@ cdef class Variables: # element, then override max-jobs to be 1. # Initialize it as a string as all variables are processed as strings. # - if _yaml.node_get(node, bool, 'notparallel', None, False): + if node.get_scalar('notparallel', False).as_bool(): _yaml.node_set(node, 'max-jobs', str(1)) cdef dict ret = {} diff --git a/src/buildstream/_workspaces.py b/src/buildstream/_workspaces.py index ca1834c64..9a67984c9 100644 --- a/src/buildstream/_workspaces.py +++ b/src/buildstream/_workspaces.py @@ -630,7 +630,7 @@ class Workspaces(): # def _load_workspace(self, node): dictionary = { - 'prepared': _yaml.node_get(node, bool, 'prepared', default_value=False), + 'prepared': node.get_scalar('prepared', default=False).as_bool(), 'path': node.get_scalar('path').as_str(), 'last_successful': node.get_scalar('last_successful', default=None).as_str(), 'running_files': _yaml.node_sanitize( diff --git a/src/buildstream/_yaml.pxd b/src/buildstream/_yaml.pxd index 06cc8b29a..2ca2856bf 100644 --- a/src/buildstream/_yaml.pxd +++ b/src/buildstream/_yaml.pxd @@ -35,6 +35,7 @@ cdef class MappingNode(Node): cdef class ScalarNode(Node): + cpdef bint as_bool(self) except * cpdef str as_str(self) diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index efcc3ac23..bd648acd5 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -85,6 +85,18 @@ cdef class ScalarNode(Node): self.line = line self.column = column + cpdef bint as_bool(self) except *: + if type(self.value) is bool: + return self.value + + # Don't coerce booleans to string, this makes "False" strings evaluate to True + if self.value in ('True', 'true'): + return True + elif self.value in ('False', 'false'): + return False + else: + raise ValueError() + cpdef str as_str(self): return self.value @@ -606,6 +618,11 @@ cpdef ProvenanceInformation node_get_provenance(Node node, str key=None, list in # Returned strings are stripped of leading and trailing whitespace # cpdef object node_get(Node node, object expected_type, str key, list indices=None, object default_value=_sentinel, bint allow_none=False): + if expected_type is bool and indices is None: + import traceback + traceback.print_stack() + assert False + if indices is None: value = node.value.get(key, _sentinel) diff --git a/tests/format/include.py b/tests/format/include.py index 8c5846416..0bfc2b342 100644 --- a/tests/format/include.py +++ b/tests/format/include.py @@ -28,7 +28,7 @@ def test_include_project_file(cli, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, bool, 'included') + assert loaded.get_scalar('included').as_bool() def test_include_missing_file(cli, tmpdir): @@ -87,7 +87,7 @@ def test_include_junction_file(cli, tmpdir, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, bool, 'included') + assert loaded.get_scalar('included').as_bool() @pytest.mark.datafiles(DATA_DIR) @@ -310,4 +310,4 @@ def test_local_to_junction(cli, tmpdir, datafiles): 'element.bst']) result.assert_success() loaded = _yaml.load_data(result.output) - assert _yaml.node_get(loaded, bool, 'included') + assert loaded.get_scalar('included').as_bool() |