Ejemplo n.º 1
0
def _build_arg_mapping(atom_name, reqs, rebind_args, function, do_infer,
                       ignore_list=None):
    """Builds an input argument mapping for a given function.

    Given a function, its requirements and a rebind mapping this helper
    function will build the correct argument mapping for the given function as
    well as verify that the final argument mapping does not have missing or
    extra arguments (where applicable).
    """

    # build a list of required arguments based on function signature
    req_args = reflection.get_callable_args(function, required_only=True)
    all_args = reflection.get_callable_args(function, required_only=False)

    # remove arguments that are part of ignore_list
    if ignore_list:
        for arg in ignore_list:
            if arg in req_args:
                req_args.remove(arg)
    else:
        ignore_list = []

    required = {}
    # add reqs to required mappings
    if reqs:
        if isinstance(reqs, six.string_types):
            required.update({reqs: reqs})
        else:
            required.update((a, a) for a in reqs)

    # add req_args to required mappings if do_infer is set
    if do_infer:
        required.update((a, a) for a in req_args)

    # update required mappings based on rebind_args
    required.update(_build_rebind_dict(req_args, rebind_args))

    if do_infer:
        opt_args = set(all_args) - set(required) - set(ignore_list)
        optional = dict((a, a) for a in opt_args)
    else:
        optional = {}

    if not reflection.accepts_kwargs(function):
        extra_args = set(required) - set(all_args)
        if extra_args:
            extra_args_str = ', '.join(sorted(extra_args))
            raise ValueError('Extra arguments given to atom %s: %s'
                             % (atom_name, extra_args_str))

    # NOTE(imelnikov): don't use set to preserve order in error message
    missing_args = [arg for arg in req_args if arg not in required]
    if missing_args:
        raise ValueError('Missing arguments for atom %s: %s'
                         % (atom_name, ' ,'.join(missing_args)))
    return required, optional
Ejemplo n.º 2
0
 def execute(self, wrapper, *_args, **_kwargs):
     """Invoke saved callable with saved args."""
     if not ('provided' in reflection.get_callable_args(self._func)
             or reflection.accepts_kwargs(self._func)):
         _kwargs.pop('provided', None)
     if self._logspec:
         # Execute the log method (the first element in the list) with its
         # arguments (the remaining elements in the list).
         self._logspec[0](*self._logspec[1:])
     return self._func(wrapper, *_args, **_kwargs)
Ejemplo n.º 3
0
 def _execute(wrapper):
     update_needed = False
     for task in self._tasks:
         kwargs = task.save_kwargs
         if ('provided' in reflection.get_callable_args(task.execute)
                 or reflection.accepts_kwargs(task.execute)):
             kwargs['provided'] = self.provided
         ret = task.execute(wrapper, *task.save_args, **kwargs)
         if task.flag_update and ret:
             update_needed = True
         if task.provides is not None:
             self.provided[task.provides] = ret
     if update_needed:
         wrapper = wrapper.update(timeout=self.update_timeout)
     return wrapper
Ejemplo n.º 4
0
def _build_arg_mapping(atom_name, reqs, rebind_args, function, do_infer,
                       ignore_list=None):
    """Builds an input argument mapping for a given function.

    Given a function, its requirements and a rebind mapping this helper
    function will build the correct argument mapping for the given function as
    well as verify that the final argument mapping does not have missing or
    extra arguments (where applicable).
    """
    atom_args = reflection.get_callable_args(function, required_only=True)
    if ignore_list:
        for arg in ignore_list:
            if arg in atom_args:
                atom_args.remove(arg)

    result = {}
    if reqs:
        result.update((a, a) for a in reqs)
    if do_infer:
        result.update((a, a) for a in atom_args)
    result.update(_build_rebind_dict(atom_args, rebind_args))

    if not reflection.accepts_kwargs(function):
        all_args = reflection.get_callable_args(function, required_only=False)
        extra_args = set(result) - set(all_args)
        if extra_args:
            extra_args_str = ', '.join(sorted(extra_args))
            raise ValueError('Extra arguments given to atom %s: %s'
                             % (atom_name, extra_args_str))

    # NOTE(imelnikov): don't use set to preserve order in error message
    missing_args = [arg for arg in atom_args if arg not in result]
    if missing_args:
        raise ValueError('Missing arguments for atom %s: %s'
                         % (atom_name, ' ,'.join(missing_args)))
    return result
Ejemplo n.º 5
0
 def test_with_kwargs(self):
     self.assertEqual(True, reflection.accepts_kwargs(function_with_kwargs))
Ejemplo n.º 6
0
 def test_no_kwargs(self):
     self.assertEqual(False, reflection.accepts_kwargs(mere_function))
Ejemplo n.º 7
0
def _build_arg_mapping(atom_name,
                       reqs,
                       rebind_args,
                       function,
                       do_infer,
                       ignore_list=None):
    """Builds an input argument mapping for a given function.

    Given a function, its requirements and a rebind mapping this helper
    function will build the correct argument mapping for the given function as
    well as verify that the final argument mapping does not have missing or
    extra arguments (where applicable).
    """

    # Build a list of required arguments based on function signature.
    req_args = reflection.get_callable_args(function, required_only=True)
    all_args = reflection.get_callable_args(function, required_only=False)

    # Remove arguments that are part of ignore list.
    if ignore_list:
        for arg in ignore_list:
            if arg in req_args:
                req_args.remove(arg)
    else:
        ignore_list = []

    # Build the required names.
    required = OrderedDict()

    # Add required arguments to required mappings if inference is enabled.
    if do_infer:
        required.update((a, a) for a in req_args)

    # Add additional manually provided requirements to required mappings.
    if reqs:
        if isinstance(reqs, six.string_types):
            required.update({reqs: reqs})
        else:
            required.update((a, a) for a in reqs)

    # Update required mappings values based on rebinding of arguments names.
    required.update(_build_rebind_dict(req_args, rebind_args))

    # Determine if there are optional arguments that we may or may not take.
    if do_infer:
        opt_args = sets.OrderedSet(all_args)
        opt_args = opt_args - set(
            itertools.chain(six.iterkeys(required), iter(ignore_list)))
        optional = OrderedDict((a, a) for a in opt_args)
    else:
        optional = OrderedDict()

    # Check if we are given some extra arguments that we aren't able to accept.
    if not reflection.accepts_kwargs(function):
        extra_args = sets.OrderedSet(six.iterkeys(required))
        extra_args -= all_args
        if extra_args:
            raise ValueError('Extra arguments given to atom %s: %s' %
                             (atom_name, list(extra_args)))

    # NOTE(imelnikov): don't use set to preserve order in error message
    missing_args = [arg for arg in req_args if arg not in required]
    if missing_args:
        raise ValueError('Missing arguments for atom %s: %s' %
                         (atom_name, missing_args))
    return required, optional
Ejemplo n.º 8
0
 def test_with_kwargs(self):
     self.assertEqual(True, reflection.accepts_kwargs(function_with_kwargs))
Ejemplo n.º 9
0
 def test_no_kwargs(self):
     self.assertEqual(False, reflection.accepts_kwargs(mere_function))
Ejemplo n.º 10
0
def _build_arg_mapping(atom_name, reqs, rebind_args, function, do_infer,
                       ignore_list=None):
    """Builds an input argument mapping for a given function.

    Given a function, its requirements and a rebind mapping this helper
    function will build the correct argument mapping for the given function as
    well as verify that the final argument mapping does not have missing or
    extra arguments (where applicable).
    """

    # Build a list of required arguments based on function signature.
    req_args = reflection.get_callable_args(function, required_only=True)
    all_args = reflection.get_callable_args(function, required_only=False)

    # Remove arguments that are part of ignore list.
    if ignore_list:
        for arg in ignore_list:
            if arg in req_args:
                req_args.remove(arg)
    else:
        ignore_list = []

    # Build the required names.
    required = collections.OrderedDict()

    # Add required arguments to required mappings if inference is enabled.
    if do_infer:
        required.update((a, a) for a in req_args)

    # Add additional manually provided requirements to required mappings.
    if reqs:
        if isinstance(reqs, six.string_types):
            required.update({reqs: reqs})
        else:
            required.update((a, a) for a in reqs)

    # Update required mappings values based on rebinding of arguments names.
    required.update(_build_rebind_dict(req_args, rebind_args))

    # Determine if there are optional arguments that we may or may not take.
    if do_infer:
        opt_args = sets.OrderedSet(all_args)
        opt_args = opt_args - set(itertools.chain(six.iterkeys(required),
                                                  iter(ignore_list)))
        optional = collections.OrderedDict((a, a) for a in opt_args)
    else:
        optional = collections.OrderedDict()

    # Check if we are given some extra arguments that we aren't able to accept.
    if not reflection.accepts_kwargs(function):
        extra_args = sets.OrderedSet(six.iterkeys(required))
        extra_args -= all_args
        if extra_args:
            raise ValueError('Extra arguments given to atom %s: %s'
                             % (atom_name, list(extra_args)))

    # NOTE(imelnikov): don't use set to preserve order in error message
    missing_args = [arg for arg in req_args if arg not in required]
    if missing_args:
        raise ValueError('Missing arguments for atom %s: %s'
                         % (atom_name, missing_args))
    return required, optional
Ejemplo n.º 11
0
 def test_with_kwargs(self):
     self.assertTrue(reflection.accepts_kwargs(function_with_kwargs))