def extract_substructure(base, substructure):
    """
    Assuming that substructure is a possibly nested dictionary, return a new
    dictionary with the same keys (and subkeys) as substructure, but extract
    the leaf values from base.

    This is used to extract and verify a configuration from a yaml blob.
    """
    if (type(substructure) is not dict and
            type(base) is not dict):
        return base
    if type(base) is not dict:
        raise MissingConfigError(
            "Found non-dict value {} when expecting a sub-configuration "
            "{}.".format(repr(base), repr(substructure)))
    if type(substructure) is not dict:
        raise MissingConfigError(
            "Found dict value {} when expecting a simple configuration value "
            "{}.".format(repr(base), repr(substructure)))
    try:
        subdict = []
        for key, value in substructure.iteritems():
            if type(value) is Optional:
                base_val = base.get(key, value.default)
            elif _is_optional(value):
                base_val = base.get(key, {})
            else:
                base_val = base[key]
            subdict.append((key, extract_substructure(base_val, value)))
        return dict(subdict)
    except __HOLE__ as e:
        raise MissingConfigError(
            "Missing key {} in configuration".format(e.args[0]))
dataset/ETHPy150Open ClusterHQ/flocker/flocker/common/configuration.py/extract_substructure