glorpen.di API Documentation

glorpen.di

Dependency injection component.

glorpen.di.__version__

Current package version.

class glorpen.di.Container[source]

Alias to glorpen.di.container.Container.

glorpen.di.container

class glorpen.di.container.Alias(target)[source]

Alias for service.

class glorpen.di.container.Container[source]

Implementation of DIC container.

add_alias(service, alias)[source]

Adds an alias for given service

add_parameter(name, value)[source]

Adds a key-value parameter.

add_service(name)[source]

Adds service definition to this container.

name argument should be a class, import path, or string if Service.implementation() will be used.

Returns:
Service
get(svc)[source]

Gets service instance.

Raises:
UnkownServiceException
get_definition(svc)[source]

Returns definition for given service name.

get_parameter(name)[source]

Gets parameter.

Raises:
UnkownParameterException
set_scope_hierarchy(*scopes)[source]

Sets used scopes hierarchy.

Arguments should be scopes sorted from widest to narrowest. A service in wider scope cannot request service from narrower one.

Default is: [glorpen.di.scopes.ScopeSingleton, glorpen.di.scopes.ScopePrototype].

Args:
classes or instances of glorpen.di.scopes.ScopeBase
class glorpen.di.container.Deffered(service=None, method=None, param=None)[source]

Class for marking values for lazy resolving.

Values are resolved by Container upon service creation.

resolve(getter, param_getter)[source]

Given (service) getter and param_getter, returns resolved value.

class glorpen.di.container.Kwargs(**kwargs)[source]

Simply wraps given kwargs for later use.

classmethod merge(*args)[source]

Merges iterable arguments, can be dict or Kwargs instance. @return: Resulting dict

class glorpen.di.container.Service(name_or_impl)[source]

Service definition.

When filling arguments for constructor and method calls you can use:

  • my_var__svc=MyClass - will inject service MyClass to my_var
  • my_var__param=”my.param” - will inject parameter named “my.param” to my_var

Implementation value for service can be:

  • class instance
  • string with import path
  • callable
call(method, kwargs=None, **kwargs_inline)[source]

Adds a method call after service creation with given arguments.

Returns:
Service
call_with_signature(method, kwargs=None, **kwargs_inline)[source]

Adds a method call after service creation with given arguments. Arguments detected from function signature are added if not already present.

Returns:
Service
configurator(service=None, method=None, callable=None, kwargs=None, **kwargs_inline)[source]

Adds service or callable as configurator of this service.

Args:
service + method, callable: given service method/callable will be called with instance of this service.
Returns:
Service
factory(service=None, method=None, callable=None, kwargs=None, **kwargs_inline)[source]

Sets factory callable.

Returns:
Service
implementation(v)[source]

Sets service implementation (callable).

Returns:
Service
kwargs(**kwargs)[source]

Adds service constructor arguments.

Returns:
Service
kwargs_from_signature()[source]

Adds arguments found in class signature, based on provided function hints.

Returns:
Service
kwargs_modifier(service=None, method=None, callable=None, kwargs=None, **kwargs_inline)[source]

Adds service or callable as constructor arguments modifier of this service.

Args:
service + method, callable: given service method/callable will be called with kwargs of this service.
Returns:
Service
scope(scope_cls)[source]

Sets service scope.

Returns:
Service
set(**kwargs)[source]

Will setattr() given arguments on service.

Returns:
Service
glorpen.di.container.fluid(f)[source]

Decorator for applying fluid pattern to class methods and to disallow calling when instance is marked as frozen.

glorpen.di.container.normalize_name(o)[source]

Gets object “name” for use with Container.

Args:
o (object): Object to get name for
Returns:
str
Raises:
Exception

glorpen.di.scopes

Built-in scopes.

class glorpen.di.scopes.ScopeBase[source]

Base class for all scopes.

class glorpen.di.scopes.ScopePrototype[source]

Scope that creates new instance of given service each time it is requested.

class glorpen.di.scopes.ScopeSingleton[source]

Scope that creates instance of given service only once.

glorpen.di.exceptions

Exceptions used by glorpen.di.

exception glorpen.di.exceptions.ContainerException[source]

Base exception

exception glorpen.di.exceptions.InjectionException(svc_name, cls, method_name=None)[source]

Raised when service or its method cannot be created or called

exception glorpen.di.exceptions.InvalidAliasTargetException(name)[source]

Raised when adding alias to alias or not exisiting service

exception glorpen.di.exceptions.RecursionException(s_def, requester_chain)[source]

Raised when service definition is requiring itself.

exception glorpen.di.exceptions.ScopeWideningException(s_def, requester_chain)[source]

Raised when Service A depends on Service B from narrower scope.

exception glorpen.di.exceptions.ServiceAlreadyCreated(svc_name)[source]

Raised when service definition is changed but service is already created by glorpen.di.container.Container.

exception glorpen.di.exceptions.UnknownParameterException(name)[source]

Raised when requesting parameter which is not registered in glorpen.di.container.Container.

exception glorpen.di.exceptions.UnknownScopeException(scope, svc_name)[source]

Raised when service has scope not assigned to glorpen.di.container.Container by glorpen.di.container.Container.set_scope_hierarchy().

exception glorpen.di.exceptions.UnknownServiceException(svc_name)[source]

Raised when requesting service name which is not registered in glorpen.di.container.Container.