def reset_last_overriding(cls): """Reset last overriding provider for each container providers. :rtype: None """ if not cls.overridden: raise errors.Error('Container {0} is not overridden'.format(cls)) cls.overridden = cls.overridden[:-1] for provider in six.itervalues(cls.providers): provider.reset_last_overriding()
def override(self, overriding): """Override current container by overriding container. :param overriding: Overriding container. :type overriding: :py:class:`DynamicContainer` :raise: :py:exc:`dependency_injector.errors.Error` if trying to override container by itself :rtype: None """ if overriding is self: raise errors.Error('Container {0} could not be overridden ' 'with itself'.format(self)) self.overridden += (overriding, ) for name, provider in six.iteritems(overriding.providers): try: getattr(self, name).override(provider) except AttributeError: pass
def override(cls, overriding): """Override current container by overriding container. :param overriding: Overriding container. :type overriding: :py:class:`DeclarativeContainer` :raise: :py:exc:`dependency_injector.errors.Error` if trying to override container by itself or its subclasses :rtype: None """ if issubclass(cls, overriding): raise errors.Error('Container {0} could not be overridden ' 'with itself or its subclasses'.format(cls)) cls.overridden += (overriding, ) for name, provider in six.iteritems(overriding.cls_providers): try: getattr(cls, name).override(provider) except AttributeError: pass
def decorator(callback_or_cls): """Dependency injection decorator.""" if isinstance(callback_or_cls, six.class_types): cls = callback_or_cls cls_init = utils.fetch_cls_init(cls) if not cls_init: raise errors.Error( 'Class {0}.{1} has no __init__() '.format( cls.__module__, cls.__name__) + 'method and could not be decorated with @inject decorator') cls.__init__ = decorator(cls_init) return cls callback = callback_or_cls if hasattr(callback, '__INJECT_DECORATED__'): callback.args += arg_injections callback.kwargs.update(kwarg_injections) return callback @six.wraps(callback) def decorated(*args, **kwargs): """Decorated with dependency injection callback.""" if decorated.args: args = tuple(arg.provide_injection() for arg in decorated.args) + args for name, arg in six.iteritems(decorated.kwargs): if name not in kwargs: kwargs[name] = arg.provide_injection() return callback(*args, **kwargs) decorated.__INJECT_DECORATED__ = True decorated.origin = callback decorated.args = arg_injections decorated.kwargs = kwarg_injections return decorated
def as_view(provider, name=None): """Transform class-based view provider to view function.""" if isinstance(provider, providers.Factory): def view(*args, **kwargs): self = provider() return self.dispatch_request(*args, **kwargs) assert name, 'Argument "endpoint" is required for class-based views' view.__name__ = name elif isinstance(provider, providers.Callable): def view(*args, **kwargs): return provider(*args, **kwargs) view.__name__ = provider.provides.__name__ else: raise errors.Error('Undefined provider type') view.__doc__ = provider.provides.__doc__ view.__module__ = provider.provides.__module__ if isinstance(provider.provides, type): view.view_class = provider.provides if hasattr(provider.provides, 'decorators'): for decorator in provider.provides.decorators: view = decorator(view) if hasattr(provider.provides, 'methods'): view.methods = provider.provides.methods if hasattr(provider.provides, 'provide_automatic_options'): view.provide_automatic_options = provider.provides.provide_automatic_options return view
def _check_provider_type(cls, provider): if not isinstance(provider, cls.provider_type): raise errors.Error('{0} can contain only {1} ' 'instances'.format(cls, cls.provider_type))