def conv(d): if isinstance(d, (CommentedMap, CommentedSeq)): return d if isinstance(d, dict): ret_val = CommentedMap() if sort_dict and not isinstance(d, ordereddict): for key in sorted(d): ret_val[key] = conv(d[key]) else: for key in d: ret_val[key] = conv(d[key]) return ret_val if isinstance(d, list): ret_val = CommentedSeq() for k in d: ret_val.append(conv(k)) return ret_val return d
def deref_yaml(yaml_value): if "keys" in dir(yaml_value): # Dictionary-like type if "$ref" in yaml_value.keys(): # It's a reference; deref it reffed_value = self.deref(yaml_value["$ref"]) # The referenced object may contain more references, so # resolve those before returning return deref_yaml(reffed_value) else: # recurse through each key/value pair looking for refs the_copy = YamlMap() for k, v in yaml_value.items(): the_copy[k] = deref_yaml(v) return the_copy elif "append" in dir(yaml_value): # List-like type # recurse through each item looking for refs the_copy = YamlSeq() for item in yaml_value: the_copy.append(deref_yaml(item)) return the_copy else: # Probably a basic type # base case: return the value return yaml_value
def get_resolved_var_adapter_object(value, key, arg, root_arg=True, frecklet=None, is_secret=None, inventory=None): """Replaces string in object recursively, non-jinja-termplate version. Args: value: the object key: the string to be replaced repl: the replacement string Returns: tuple: the replaced object, and a boolean that indicates whether there was any change or not """ changed_global = False is_sec = False if isinstance(value, Mapping): result = CommentedMap() for k, v in value.items(): repl_value, sec, changed = get_resolved_var_adapter_object( value=v, key=k, arg=arg, root_arg=False, frecklet=frecklet, is_secret=is_secret, inventory=inventory, ) if changed: changed_global = True if sec: is_sec = True result[k] = repl_value elif isinstance(value, (list, tuple, CommentedSeq)): result = [] for v in value: repl_value, sec, changed = get_resolved_var_adapter_object( value=v, key=key, arg=arg, root_arg=False, frecklet=frecklet, is_secret=is_secret, inventory=inventory, ) if changed: changed_global = True if sec: is_sec = True result.append(repl_value) elif isinstance(value, string_types): result, is_sec, changed_global = get_value_from_var_adapter_string( value, key=key, arg=arg, root_arg=root_arg, frecklet=frecklet, is_secret=is_secret, inventory=inventory, ) else: result = value is_sec = is_secret changed_global = False return result, is_sec, changed_global