From 322dfa73e286b689c0fce8f33aad15a91ce5564e Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Thu, 27 Jun 2019 11:18:20 +0100 Subject: _yaml: Remove 'key' from node_find_target - node_find_target with 'key' is only used once in the codebase. We can remove and simplify this function - Allow 'MappingNode.get_node()' to be called without any 'expected_types' --- src/buildstream/_yaml.pxd | 2 +- src/buildstream/_yaml.pyx | 13 +++---------- src/buildstream/source.py | 4 +++- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/buildstream/_yaml.pxd b/src/buildstream/_yaml.pxd index 98ea64b28..6e4fd6218 100644 --- a/src/buildstream/_yaml.pxd +++ b/src/buildstream/_yaml.pxd @@ -33,7 +33,7 @@ cdef class Node: cdef class MappingNode(Node): cdef Node get(self, str key, default, default_constructor) cpdef MappingNode get_mapping(self, str key, default=*) - cpdef Node get_node(self, str key, list allowed_types, bint allow_none=*) + cpdef Node get_node(self, str key, list allowed_types=*, bint allow_none=*) cpdef ScalarNode get_scalar(self, str key, default=*) cpdef SequenceNode get_sequence(self, str key, object default=*) cpdef bint get_bool(self, str key, default=*) except * diff --git a/src/buildstream/_yaml.pyx b/src/buildstream/_yaml.pyx index b2a7f88aa..e0c04b655 100644 --- a/src/buildstream/_yaml.pyx +++ b/src/buildstream/_yaml.pyx @@ -173,7 +173,7 @@ cdef class MappingNode(Node): return value - cpdef Node get_node(self, str key, list allowed_types, bint allow_none = False): + cpdef Node get_node(self, str key, list allowed_types = None, bint allow_none = False): cdef value = self.value.get(key, _sentinel) if value is _sentinel: @@ -184,7 +184,7 @@ cdef class MappingNode(Node): raise LoadError(LoadErrorReason.INVALID_DATA, "{}: Dictionary did not contain expected key '{}'".format(provenance, key)) - if type(value) not in allowed_types: + if allowed_types and type(value) not in allowed_types: provenance = node_get_provenance(self) raise LoadError(LoadErrorReason.INVALID_DATA, "{}: Value of '{}' is not one of the following: {}.".format( @@ -1339,19 +1339,12 @@ def assert_symbol_name(ProvenanceInformation provenance, str symbol_name, str pu # Args: # node (Node): The node at the root of the tree to search # target (Node): The node you are looking for in that tree -# key (str): Optional string key within target node # # Returns: # (list): A path from `node` to `target` or None if `target` is not in the subtree -cpdef list node_find_target(MappingNode node, Node target, str key=None): - if key is not None: - target = target.value[key] - +cpdef list node_find_target(MappingNode node, Node target): cdef list path = [] if _walk_find_target(node, path, target): - if key: - # Remove key from end of path - path = path[:-1] return path return None diff --git a/src/buildstream/source.py b/src/buildstream/source.py index a57cb758c..75861f6da 100644 --- a/src/buildstream/source.py +++ b/src/buildstream/source.py @@ -970,7 +970,9 @@ class Source(Plugin): if action == 'add': path = _yaml.node_find_target(toplevel_node, node) else: - path = _yaml.node_find_target(toplevel_node, node, key=key) + full_path = _yaml.node_find_target(toplevel_node, node.get_node(key)) + # We want the path to the node containing the key, not to the key + path = full_path[:-1] roundtrip_file = roundtrip_cache.get(provenance.filename) if not roundtrip_file: -- cgit v1.2.1