import re


def convert_keys(input_value):
    """
    Convert all of the keys in a dict recursively from CamelCase to snake_case.
    Also strips leading and trailing whitespace from string values.
    :param input_value:
    :return:
    """
    retn = None

    if isinstance(input_value, list):
        retn = []
        for list_item in input_value:
            if isinstance(list_item, (dict, list)):
                retn.append(convert_keys(list_item))
            else:
                if isinstance(list_item, str):
                    retn.append(list_item.strip())

                else:
                    retn.append(list_item)

    elif isinstance(input_value, dict):
        retn = dict()
        for k, v in input_value.items():
            new_key_s = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", k)
            new_key = re.sub("([a-z0-9])([A-Z])", r"\1_\2", new_key_s).lower()
            if isinstance(v, (dict, list)):
                retn[new_key] = convert_keys(v)

            else:
                if isinstance(v, str):
                    retn[new_key] = v.strip()
                else:
                    retn[new_key] = v

    return retn