import copy


def merge_left_with_defaults(defaults, loaded_config):
    """
    Merge two configurations, with one of them overriding the other.
    Args:
        defaults (dict): A configuration of defaults
        loaded_config (dict): A configuration, as loaded from disk.

    Returns (dict):
        A merged configuration, with loaded_config preferred over defaults.
    """
    result = defaults.copy()

    if loaded_config is None:
        return result

    # copy defaults or override them
    for k, v in result.items():
        if isinstance(v, dict):
            if k in loaded_config:
                result[k] = merge_left_with_defaults(v, loaded_config[k])
            else:
                result[k] = copy.deepcopy(v)
        elif k in loaded_config:
            result[k] = loaded_config[k]

    # copy things with no defaults
    for k, v in loaded_config.items():
        if k not in result:
            result[k] = v

    return result