Ejemplo n.º 1
0
        def injection_wrapper(*args, **kwargs):
            context = cdi_context.get_context()
            # arguments injected
            additional_args = []

            # iterate over the positional arguments of the function definition
            i = len(args)
            while i < len(arg_names):
                arg_name = arg_names[i]

                # stop when we do not have a parameter for the positional argument
                # or stop at the first keyword argument
                if arg_name not in params or arg_name in kwargs:
                    break

                # this parameter will be injected into the *args
                additional_args.append(context.get_instance(params[arg_name]))
                i += 1

            if additional_args:
                args += tuple(additional_args)

            # a list of all positional args that we have injected
            used_args = arg_names[:i]
            for name, cls in params.items():
                if not name in kwargs and not name in used_args:
                    kwargs[name] = context.get_instance(cls)
            return func(*args, **kwargs)
Ejemplo n.º 2
0
 def __get__(self, instance, owner):
     try:
         obj = instance or owner
         context = cdi_context.get_context()
         if not self._has_cached(obj, bool(instance), context):
             injected = context.get_instance(self._cls)
             self._set_cached(obj, bool(instance), context, injected)
         return self._get_cached(obj, bool(instance), context)
     except AttributeError:
         return None