import inspect
import logging


def verify_parameters(code, kwargs, err_message):
    """
    Used to verify the that the parameters in kwarg match the signature of the code.
    :param code: The code fragment's that has the signature to check.
    :param kwargs: The kwargs to look for
    :param err_message: An error message to show if the signature doesn't match.
    :return:
    """
    params = inspect.signature(code).parameters
    verified = True

    # The kwargs should all be in the function.  Anything left should have a default
    param_keys = set(params.keys())
    param_keys.discard('self')

    # Make sure they have all the ones we asked for.
    missing = set(kwargs.keys()) - set(param_keys)
    for param_name in missing:
        logging.error(f"Missing param '{param_name}' on function {err_message}")
        verified = False

    remaining = set(param_keys) - set(kwargs.keys())
    for param_name in remaining:
        if params[param_name].default == inspect.Parameter.empty:
            logging.error(f"Param '{param_name}' not passed for {err_message}")
            verified = False

    return verified