From 5f21693abb02e8aba8cc069caa04a1c12a69d4ea Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Thu, 4 Apr 2019 14:04:09 +0100 Subject: _yaml.py: Remove node_contains Now that we permit `key in somenode` remove the no longer needed function to check if a node contains a key. Signed-off-by: Daniel Silverstone --- buildstream/_context.py | 4 ++-- buildstream/_project.py | 4 ++-- buildstream/_projectrefs.py | 2 +- buildstream/_yaml.py | 14 -------------- buildstream/element.py | 2 +- buildstream/sandbox/_sandboxremote.py | 8 ++++---- tests/elements/filter.py | 4 ++-- tests/format/include.py | 2 +- tests/frontend/cross_junction_workspace.py | 2 +- tests/frontend/workspace.py | 2 +- tests/internals/yaml.py | 4 ++-- tests/sources/generic/mirror.py | 4 ++-- tests/sources/generic/track.py | 4 ++-- tests/sources/git.py | 14 +++++++------- 14 files changed, 28 insertions(+), 42 deletions(-) diff --git a/buildstream/_context.py b/buildstream/_context.py index a7d308a6f..fffeea17e 100644 --- a/buildstream/_context.py +++ b/buildstream/_context.py @@ -202,11 +202,11 @@ class Context(): _yaml.composite(defaults, user_config) # Give obsoletion warnings - if _yaml.node_contains(defaults, 'builddir'): + if 'builddir' in defaults: raise LoadError(LoadErrorReason.INVALID_DATA, "builddir is obsolete, use cachedir") - if _yaml.node_contains(defaults, 'artifactdir'): + if 'artifactdir' in defaults: raise LoadError(LoadErrorReason.INVALID_DATA, "artifactdir is obsolete") diff --git a/buildstream/_project.py b/buildstream/_project.py index 18c2319c0..af0c8d7b3 100644 --- a/buildstream/_project.py +++ b/buildstream/_project.py @@ -421,7 +421,7 @@ class Project(): else: config = self.config - if not alias or not _yaml.node_contains(config._aliases, alias): + if not alias or alias not in config._aliases: return [None] mirror_list = [] @@ -950,7 +950,7 @@ class Project(): plugins = _yaml.node_get(origin, Mapping, plugin_group, default_value={}) _yaml.node_set(origin_node, 'plugins', [k for k in _yaml.node_keys(plugins)]) for group in expected_groups: - if _yaml.node_contains(origin_node, group): + if group in origin_node: _yaml.node_del(origin_node, group) if _yaml.node_get(origin_node, str, 'origin') == 'local': diff --git a/buildstream/_projectrefs.py b/buildstream/_projectrefs.py index b1443ef32..09205a7c3 100644 --- a/buildstream/_projectrefs.py +++ b/buildstream/_projectrefs.py @@ -86,7 +86,7 @@ class ProjectRefs(): # Ensure we create our toplevel entry point on the fly here for node in [self._toplevel_node, self._toplevel_save]: - if not _yaml.node_contains(node, 'projects'): + if 'projects' not in node: _yaml.node_set(node, 'projects', _yaml.new_empty_node(ref_node=node)) # lookup_ref() diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index a873cd62c..3d718f9ad 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -774,20 +774,6 @@ def __new_node_from_list(inlist): return Node(ret, None, 0, next(_SYNTHETIC_COUNTER)) -# node_contains() -# -# Args: -# node (Node): The mapping node to query the contents of -# entry (str): The key to look for in the mapping node -# -# Returns: -# (bool): Whether entry is in the mapping in node. -# -def node_contains(node, entry): - assert type(node) is Node - return entry in node[0] - - # _is_composite_list # # Checks if the given node is a Mapping with array composition diff --git a/buildstream/element.py b/buildstream/element.py index 2bb492cb3..5c28b4753 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -753,7 +753,7 @@ class Element(Plugin): if workspace and old_dep_keys: dep.__assert_cached() - if _yaml.node_contains(old_dep_keys, dep.name): + if dep.name in old_dep_keys: key_new = dep._get_cache_key() key_old = _yaml.node_get(old_dep_keys, str, dep.name) diff --git a/buildstream/sandbox/_sandboxremote.py b/buildstream/sandbox/_sandboxremote.py index 14e160dc5..ffd53f0d0 100644 --- a/buildstream/sandbox/_sandboxremote.py +++ b/buildstream/sandbox/_sandboxremote.py @@ -136,8 +136,8 @@ class SandboxRemote(Sandbox): # Maintain some backwards compatibility with older configs, in which # 'url' was the only valid key for remote-execution: - if _yaml.node_contains(remote_config, 'url'): - if not _yaml.node_contains(remote_config, 'execution-service'): + if 'url' in remote_config: + if 'execution-service' not in remote_config: exec_config = _yaml.new_node_from_dict({'url': remote_config['url']}) else: provenance = _yaml.node_get_provenance(remote_config, key='url') @@ -157,7 +157,7 @@ class SandboxRemote(Sandbox): for config_key, config in zip(service_keys, service_configs): # Either both or none of the TLS client key/cert pair must be specified: - if _yaml.node_contains(config, 'client-key') != _yaml.node_contains(config, 'client-cert'): + if ('client-key' in config) != ('client-cert' in config): provenance = _yaml.node_get_provenance(remote_config, key=config_key) raise _yaml.LoadError(_yaml.LoadErrorReason.INVALID_DATA, "{}: TLS client key/cert pair is incomplete. " @@ -166,7 +166,7 @@ class SandboxRemote(Sandbox): .format(str(provenance))) for tls_key in tls_keys: - if _yaml.node_contains(config, tls_key): + if tls_key in config: _yaml.node_set(config, tls_key, resolve_path(_yaml.node_get(config, str, tls_key))) return RemoteExecutionSpec(*[_yaml.node_sanitize(conf) for conf in service_configs]) diff --git a/tests/elements/filter.py b/tests/elements/filter.py index 614d1bc68..1579bf06c 100644 --- a/tests/elements/filter.py +++ b/tests/elements/filter.py @@ -295,7 +295,7 @@ def test_filter_track_excepted(datafiles, cli, tmpdir): # Now check that a ref field exists new_input = _yaml.load(input_file) source_node = _yaml.node_get(new_input, dict, 'sources', indices=[0]) - assert not _yaml.node_contains(source_node, 'ref') + assert 'ref' not in source_node @pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic')) @@ -483,7 +483,7 @@ def test_filter_track_multi_exclude(datafiles, cli, tmpdir): # Now check that a ref field exists new_input = _yaml.load(input_file) source_node = _yaml.node_get(new_input, dict, 'sources', indices=[0]) - assert not _yaml.node_contains(source_node, 'ref') + assert 'ref' not in source_node new_input2 = _yaml.load(input2_file) source_node2 = _yaml.node_get(new_input2, dict, 'sources', indices=[0]) diff --git a/tests/format/include.py b/tests/format/include.py index 308e1751b..793bdb0e4 100644 --- a/tests/format/include.py +++ b/tests/format/include.py @@ -214,7 +214,7 @@ def test_list_overide_does_not_fail_upon_first_composition(cli, datafiles): # Assert that the explicitly overwritten public data is present bst = _yaml.node_get(loaded, dict, 'bst') - assert _yaml.node_contains(bst, 'foo-commands') + assert 'foo-commands' in bst assert _yaml.node_get(bst, list, 'foo-commands') == ['need', 'this'] diff --git a/tests/frontend/cross_junction_workspace.py b/tests/frontend/cross_junction_workspace.py index 325c39264..88bda857d 100644 --- a/tests/frontend/cross_junction_workspace.py +++ b/tests/frontend/cross_junction_workspace.py @@ -79,7 +79,7 @@ def test_list_cross_junction(cli, tmpdir): assert isinstance(_yaml.node_get(loaded, None, 'workspaces'), list) workspaces = _yaml.node_get(loaded, list, 'workspaces') assert len(workspaces) == 1 - assert _yaml.node_contains(workspaces[0], 'element') + assert 'element' in workspaces[0] assert _yaml.node_get(workspaces[0], str, 'element') == element diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py index c54022f36..78f3600d1 100644 --- a/tests/frontend/workspace.py +++ b/tests/frontend/workspace.py @@ -1141,7 +1141,7 @@ def test_external_track(cli, datafiles, tmpdir_factory, guess_element): # Element is tracked now element_contents = _yaml.load(element_file) - assert _yaml.node_contains(_yaml.node_get(element_contents, dict, 'sources', [0]), 'ref') + assert 'ref' in _yaml.node_get(element_contents, dict, 'sources', [0]) @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/internals/yaml.py b/tests/internals/yaml.py index 8d8494e4f..dae20b326 100644 --- a/tests/internals/yaml.py +++ b/tests/internals/yaml.py @@ -127,7 +127,7 @@ def test_node_set(datafiles): base = _yaml.load(filename) - assert not _yaml.node_contains(base, 'mother') + assert 'mother' not in base _yaml.node_set(base, 'mother', 'snow white') assert _yaml.node_get(base, str, 'mother') == 'snow white' @@ -283,7 +283,7 @@ def test_nonexistent_list_extension(datafiles): base = os.path.join(datafiles.dirname, datafiles.basename, 'basics.yaml') base = _yaml.load(base, shortname='basics.yaml') - assert not _yaml.node_contains(base, 'todo') + assert 'todo' not in base _yaml.node_extend_list(base, 'todo', 3, 'empty') diff --git a/tests/sources/generic/mirror.py b/tests/sources/generic/mirror.py index b011872b1..e9fe254c5 100644 --- a/tests/sources/generic/mirror.py +++ b/tests/sources/generic/mirror.py @@ -358,7 +358,7 @@ def test_mirror_track_upstream_present(cli, tmpdir, datafiles, kind): # Tracking tries upstream first. Check the ref is from upstream. new_element = _yaml.load(element_path) source = _yaml.node_get(new_element, dict, 'sources', [0]) - if _yaml.node_contains(source, 'ref'): + if 'ref' in source: assert _yaml.node_get(source, str, 'ref') == upstream_ref @@ -423,5 +423,5 @@ def test_mirror_track_upstream_absent(cli, tmpdir, datafiles, kind): # Check that tracking fell back to the mirror new_element = _yaml.load(element_path) source = _yaml.node_get(new_element, dict, 'sources', [0]) - if _yaml.node_contains(source, 'ref'): + if 'ref' in source: assert _yaml.node_get(source, str, 'ref') == mirror_ref diff --git a/tests/sources/generic/track.py b/tests/sources/generic/track.py index 05519719b..4c65602e9 100644 --- a/tests/sources/generic/track.py +++ b/tests/sources/generic/track.py @@ -322,13 +322,13 @@ def test_track_include(cli, tmpdir, datafiles, ref_storage, kind): new_sources = _yaml.load(os.path.join(element_path, 'sources.yml')) # Get all of the sources - assert _yaml.node_contains(new_sources, 'sources') + assert 'sources' in new_sources sources_list = _yaml.node_get(new_sources, list, 'sources') assert len(sources_list) == 1 # Get the first source from the sources list new_source = _yaml.node_get(new_sources, dict, 'sources', indices=[0]) - assert _yaml.node_contains(new_source, 'ref') + assert 'ref' in new_source assert ref == _yaml.node_get(new_source, str, 'ref') diff --git a/tests/sources/git.py b/tests/sources/git.py index c11b90c06..dd53df2b5 100644 --- a/tests/sources/git.py +++ b/tests/sources/git.py @@ -853,9 +853,9 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): tags = _yaml.node_get(_yaml.node_get(element, dict, 'sources', [0]), list, 'tags') assert len(tags) == 2 for tag in tags: - assert _yaml.node_contains(tag, 'tag') - assert _yaml.node_contains(tag, 'commit') - assert _yaml.node_contains(tag, 'annotated') + assert 'tag' in tag + assert 'commit' in tag + assert 'annotated' in tag assert _yaml.node_get(tag, bool, 'annotated') == (tag_type == 'annotated') assert {(_yaml.node_get(tag, str, 'tag'), @@ -968,9 +968,9 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty assert len(tags) == 1 tag = _yaml.node_get(source, dict, 'tags', indices=[0]) - assert _yaml.node_contains(tag, 'tag') - assert _yaml.node_contains(tag, 'commit') - assert _yaml.node_contains(tag, 'annotated') + assert 'tag' in tag + assert 'commit' in tag + assert 'annotated' in tag assert _yaml.node_get(tag, bool, 'annotated') == (tag_type == 'annotated') tag_name = _yaml.node_get(tag, str, 'tag') @@ -1127,7 +1127,7 @@ def test_default_do_not_track_tags(cli, tmpdir, datafiles): element = _yaml.load(element_path) source = _yaml.node_get(element, dict, 'sources', indices=[0]) - assert not _yaml.node_contains(source, 'tags') + assert 'tags' not in source @pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") -- cgit v1.2.1