import inspect


def _ExtractArgs(func, kargs):
  """Extracts applicable arguments from kargs, according to func's signature.

  This function tries to read argument spec from func, and look up values from
  kargs by the argument names defined for func.

  Arguments
    func: A callable object.
    kargs: A dict object with arguments.

  Returns:
    A dict that can be safely applied to func by func(**kargs)
  """
  spec = inspect.getfullargspec(func)
  if spec.varkw is None:
    # if the function accepts ** arguments, we can just pass everything into it
    # so we only need to filter kargs if spec.keywords is None
    kargs = {k: v for (k, v) in kargs.items() if k in spec.args}
  return kargs