from typing import OrderedDict


def get_text(text, text_key, axis=None):
    """ Uses text_key on text if it is a dictionary, pulling out the targeted
    text. Either way, the resulting text (given directly or pulled out a
    dictionary) is type-checked to ensure <str> or <unicode>

    Parameters
    ----------
    text : <dict>, <OrderedDict>, <str> or <unicode>
    text_key : <str>

    Returns
    ----------
    <str>
    """

    if text is None:
        text = ''

    if isinstance(text, str):
        return text

    elif isinstance(text, (dict, OrderedDict)):

        if axis is None:

            if isinstance(text_key, str):
                if text_key in text:
                    return text[text_key]
            else:
                for key in text_key:
                    if key in text:
                        return text[key]
        else:
            if axis in list(text_key.keys()):
                for key in text_key[axis]:
                    if key in text:
                        return text[key]

        raise KeyError(
            "No matching text key from the list {} was not found in the"
            " text object: {}".format(text_key, text)
        )

    else:
        raise TypeError(
            "The value set into a 'text' object must either be"
            " <str> or <unicode>, or <dict> or <collections.OrderedDict>"
            " of <str> or <unicode>. Found: {}".format(text)
        )