import re


def _use_associated_image(key, **kwargs):
    """
    Check if an associated image key should be used.  If a list of images to
    keep was specified, it must match at least one of the regex in that list.
    If a list of images to exclude was specified, it must not any regex in that
    list.  The exclude list takes priority.
    """
    if kwargs.get('_exclude_associated'):
        for exp in kwargs['_exclude_associated']:
            if re.match(exp, key):
                return False
    if kwargs.get('_keep_associated'):
        for exp in kwargs['_keep_associated']:
            if re.match(exp, key):
                return True
        return False
    return True