-def get_nesting(yaml_files):
- """return bad, files, heat, depths
- bad - list of error messages.
- files - dict: key is filename, value is dict of nested files.
- This is the tree.
- heat - dict,: key is filename, value is Heat instance.
- depths - dict: key is filename, value is a depth tuple
-
- level: 0 1 2 3
- file: template -> nested -> nested -> nested
- depth: 3 2 1 0
- """
- bad = []
- files = {}
- heat = {}
- depths = {}
- for yaml_file in yaml_files:
- dirname, basename = path.split(yaml_file)
- h = Heat(filepath=yaml_file)
- heat[basename] = h
- files[basename] = get_dict_of_nested_files(h.yml, dirname)
- for filename in files:
- depths[filename] = _get_nesting_depth_start(0, filename, files, [])
- for depth in depths[filename]:
- if depth[0] > MAX_DEPTH:
- bad.append("{} {}".format(filename, str(depth[1])))
- return bad, files, heat, depths
-
-
-def _get_nesting_depth_start(depth, filename, files, context):
- depths = []
- for rid, nf in files[filename].items():
- depths.append(_get_nesting_depth(1, nf, files, context))
- return depths
-
-
-def _get_nesting_depth(depth, filename, files, context):
- """Return a depth tuple (max_depth, current_context).
- `context` is the list of filenames.
- `depth` is the length of `context`.
- Finds the max_depth of all the resources of `filename`.
- current_context is the updated list of filenames
- and max_depth is its length.
- """
- max_depth = depth + 1
- current_context = context + [filename]
- if depth <= MAX_DEPTH:
- nested_filenames = files.get(filename, {})
- if nested_filenames:
- max_depth, current_context = max(
- _get_nesting_depth(depth + 1, nested_filename, files, current_context)
- for nested_filename in nested_filenames.values()
- )
- return max_depth, current_context
-
-