from typing import Iterable


def read_nested_dict_value(nested_dict, nested_keys: Iterable, default_value=None, error_on_miss=False):
    """
    Read a value from a series of nested dictionaries, such as returned by ruamel.yaml

    :param nested_dict: The nested dictionaries (configuration map) to read from
    :param nested_keys: An Iterable containing the sequence of keys to follow to get the value
    :param default_value: The value to return if the sequence of keys does not specify a valid
                          path in the nested dictionary (and error_on_miss is False), defaults to None
    :param error_on_miss: A boolean flag (default false) that if set causes this function to raise
                          a KeyError if the sequence of keys does not specify a valid path in the
                          nested dictionary
    :returns: The value located at the end of the path of keys,
              loosely nested_dict[nested_keys[1]][nested_keys[2]]...
    :raises KeyError: If error_on_miss is True and the sequence of keys does note specify a valid
                      path in the nested dictionary
    """
    head_key, *tail_keys = nested_keys
    if head_key not in nested_dict:
        # Can't find the next key, error or return the default value depending on our error handling
        if error_on_miss:
            # Should I actually just try and read from the dictionary and let that raise the exception?
            raise KeyError
        return default_value
    if not tail_keys:
        # Base case, no more layers to go down, return the value we have here
        return nested_dict[head_key]
    # Time to recurse down an extra layer
    return read_nested_dict_value(nested_dict[head_key], tail_keys, default_value, error_on_miss)