Ejemplo n.º 1
0
def _build_arg_mapping(task_name, reqs, rebind_args, function, do_infer):
    """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).
    """
    task_args = reflection.get_callable_args(function, required_only=True)
    result = {}
    if reqs:
        result.update((a, a) for a in reqs)
    if do_infer:
        result.update((a, a) for a in task_args)
    result.update(_build_rebind_dict(task_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 task %s: %s'
                             % (task_name, extra_args_str))

    # NOTE(imelnikov): don't use set to preserve order in error message
    missing_args = [arg for arg in task_args if arg not in result]
    if missing_args:
        raise ValueError('Missing arguments for task %s: %s'
                         % (task_name, ' ,'.join(missing_args)))
    return result
Ejemplo n.º 2
0
    def test_decorators_work(self):
        @lock_utils.locked
        def locked_fun(x, y):
            pass

        result = reflection.get_callable_args(locked_fun)
        self.assertEqual(["x", "y"], result)
Ejemplo n.º 3
0
 def cleanup(self, on_expired_callback=None):
     """Delete out-dated keys & values from the cache."""
     with self._lock:
         expired_values = [(k, v) for k, v in six.iteritems(self._data)
                           if v.expired]
         for (k, _v) in expired_values:
             del self._data[k]
     if on_expired_callback is not None:
         arg_c = len(reflection.get_callable_args(on_expired_callback))
         for (k, v) in expired_values:
             if arg_c == 2:
                 on_expired_callback(k, v)
             else:
                 on_expired_callback(v)
Ejemplo n.º 4
0
 def test_class_with_call(self):
     result = reflection.get_callable_args(CallableClass())
     self.assertEqual(['i', 'j'], result)
Ejemplo n.º 5
0
 def test_class_constructor(self):
     result = reflection.get_callable_args(ClassWithInit)
     self.assertEqual(['k', 'l'], result)
Ejemplo n.º 6
0
 def test_class_method(self):
     result = reflection.get_callable_args(Class.class_method)
     self.assertEqual(['g', 'h'], result)
Ejemplo n.º 7
0
 def test_instance_method(self):
     result = reflection.get_callable_args(Class().method)
     self.assertEqual(['c', 'd'], result)
Ejemplo n.º 8
0
 def test_method(self):
     result = reflection.get_callable_args(Class.method)
     self.assertEqual(['self', 'c', 'd'], result)
Ejemplo n.º 9
0
 def test_required_only(self):
     result = reflection.get_callable_args(function_with_defs,
                                           required_only=True)
     self.assertEqual(['a', 'b'], result)
Ejemplo n.º 10
0
 def test_function_with_defaults(self):
     result = reflection.get_callable_args(function_with_defs)
     self.assertEqual(['a', 'b', 'optional'], result)
Ejemplo n.º 11
0
 def test_mere_function(self):
     result = reflection.get_callable_args(mere_function)
     self.assertEqual(['a', 'b'], result)
Ejemplo n.º 12
0
 def test_method(self):
     result = reflection.get_callable_args(Class.method)
     self.assertEqual(["self", "c", "d"], result)
Ejemplo n.º 13
0
 def test_function_with_defaults(self):
     result = reflection.get_callable_args(function_with_defs)
     self.assertEqual(["a", "b", "optional"], result)
Ejemplo n.º 14
0
 def test_required_only(self):
     result = reflection.get_callable_args(function_with_defs,
                                           required_only=True)
     self.assertEqual(['a', 'b'], result)
Ejemplo n.º 15
0
 def test_function_with_defaults(self):
     result = reflection.get_callable_args(function_with_defs)
     self.assertEqual(['a', 'b', 'optional'], result)
Ejemplo n.º 16
0
 def test_mere_function(self):
     result = reflection.get_callable_args(mere_function)
     self.assertEqual(['a', 'b'], result)