mainline package

Submodules

mainline.catalog module

class mainline.catalog.Catalog(*args, **kwargs)[source]

Bases: mainline.catalog.ICatalog, mainline.catalog.ProviderMapping

class mainline.catalog.CatalogMeta[source]

Bases: abc.ABCMeta

Meta class used to populate providers from attributes of Catalog subclass declarations.

class mainline.catalog.ICatalog[source]

Bases: object

Inherit from this class to note that you support the ICatalog interface

class mainline.catalog.ProviderMapping(*args, **kwargs)[source]

Bases: mainline.utils.ProxyMutableMapping

Mixin to provide mapping interface on providers

update(arg, allow_overwrite=False)[source]

Updates our providers from either an ICatalog subclass/instance or a mapping. If arg is an ICatalog, we update from it’s ._providers attribute.

Parameters:
  • arg (ICatalog or collections.Mapping) – Di/Catalog/Mapping to update from.
  • allow_overwrite (bool) – If True, allow overwriting existing keys

mainline.di module

class mainline.di.Di(providers_factory=<class 'mainline.catalog.Catalog'>, dependencies_factory=<class 'dict'>)[source]

Bases: mainline.catalog.ICatalog

Dependency injection container.

class Catalog(*args, **kwargs)

Bases: mainline.catalog.ICatalog, mainline.catalog.ProviderMapping

class Di.Provider(factory, scope=<class 'mainline.scope.NoneScope'>, key='')

Bases: mainline.provider.IFactoryProvider

has_instance()
provide(*args, **kwargs)
scopes = <ScopeRegistry {<class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'global': <class 'mainline.scope.GlobalScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, 'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'process': <class 'mainline.scope.ProcessScope'>}>
set_instance(instance)
Di.auto_inject(*args, **kwargs)[source]

Decorator that magically inspects the argspec of the wrapped upon call, injecting provider instances as names match. It’s recommended to use inject() where possible and not this heap of black magic.

Positional arguments are added as dependencies to wrapped.

Keyword arguments are handled similarly to inject(), being a mapping of keyword argument name to provider key.

Any specified provider keys are added as dependencies for the wrapped.

If dependencies are associated with the wrapped, only those are checked for match in the argspec.

Parameters:
  • keys (tuple) – Provider keys to inject as positional arguments
  • kwargs (dict) – Mapping of keyword argument name to provider keys to inject as keyword arguments
Returns:

decorator

Return type:

decorator

Di.dependencies

Public attributes for dependency mapping

Di.depends_on(*keys)[source]

Decorator that marks the wrapped as depending on specified provider keys.

Parameters:keys (tuple) – Provider keys to mark as dependencies for wrapped
Returns:decorator
Return type:decorator
Di.get_deps(obj)[source]

Returns dependencies for key.

Parameters:key (object) – Dependent holder key
Returns:Dependencies
Return type:set
Di.get_missing_deps(obj)[source]

Returns missing dependencies for provider key. Missing meaning no instance can be provided at this time.

Parameters:key (object) – Provider key
Returns:Missing dependencies
Return type:list
Di.inject(*args, **kwargs)[source]

Decorator that injects the specified arguments when the wrapped is called. Argspec is modified on the wrapped accordingly.

Positional arguments are injected in the order they are given.

Keyword arguments are injected with the given key name, eg omg=’whoa’ would inject as omg=<whoa instance>.

Any specified provider keys are added as dependencies for the wrapped.

Parameters:
  • keys (tuple) – Provider keys to inject as positional arguments
  • kwargs (dict) – Mapping of keyword argument name to provider keys to inject as keyword arguments
Returns:

decorator

Return type:

decorator

Di.inject_classproperty(key, name=None, replace_on_access=False)[source]

Decorator that injects the specified key as a classproperty.

If replace_on_access is True, then it replaces itself with the instance on first lookup.

Parameters:
  • key (object) – Provider key
  • name (str) – Name of classproperty, defaults to key
  • replace_on_access (bool) – If True, replace the classproperty with the actual value on first lookup
Di.iresolve(*keys)[source]

Iterates over resolved instances for given provider keys.

Parameters:keys (tuple) – Provider keys
Returns:Iterator of resolved instances
Return type:generator
static Di.provider(factory=<object object>, scope=<class 'mainline.scope.NoneScope'>)

Decorator to create a provider using the given factory, and scope. Can also be used in a non-decorator manner.

Parameters:scope (object or callable) – Scope key, factory, or instance
Returns:decorator
Return type:decorator
Di.providers

Public attribute for provider mapping

Di.register_factory(key, factory=<object object>, scope=<class 'mainline.scope.NoneScope'>, allow_overwrite=False)[source]

Creates and registers a provider using the given key, factory, and scope. Can also be used as a decorator.

Parameters:
  • key (object) – Provider key
  • factory (callable) – Factory callable
  • scope (object or callable) – Scope key, factory, or instance
Returns:

Factory (or None if we’re creating a provider without a factory)

Return type:

callable or None

Di.resolve(*keys)[source]

Returns resolved instances for given provider keys.

If only one positional argument is given, only one is returned.

Parameters:keys (tuple) – Provider keys
Returns:Resolved instance(s); if only one key given, otherwise list of them.
Return type:object or list
Di.resolve_deps(obj)[source]

Returns list of resolved dependencies for given obj.

Parameters:obj (object) – Object to lookup dependencies for
Returns:Resolved dependencies
Return type:list
Di.scopes = <ScopeRegistry {<class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'global': <class 'mainline.scope.GlobalScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, 'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'process': <class 'mainline.scope.ProcessScope'>}>
Di.set_instance(key, instance, default_scope=<class 'mainline.scope.GlobalScope'>)[source]

Sets instance under specified provider key. If a provider for specified key does not exist, one is created without a factory using the given scope.

Parameters:
  • key (object) – Provider key
  • instance (object) – Instance
  • default_scope (object or callable) – Scope key, factory, or instance
Di.update(catalog=None, dependencies=None, allow_overwrite=False)[source]

Convenience method to update this Di instance with the specified contents.

Parameters:
  • catalog (ICatalog or collections.Mapping) – ICatalog supporting class or mapping
  • dependencies (collections.Mapping) – Mapping of dependencies
  • allow_overwrite (bool) – If True, allow overwriting existing keys. Only applies to providers.

mainline.exceptions module

exception mainline.exceptions.DiError[source]

Bases: Exception

exception mainline.exceptions.UnprovidableError[source]

Bases: mainline.exceptions.DiError

exception mainline.exceptions.UnresolvableError[source]

Bases: mainline.exceptions.DiError

mainline.injection module

class mainline.injection.AutoSpecInjector(di, *args, **kwargs)[source]

Bases: mainline.injection.CallableInjector

decorate(wrapped)[source]
class mainline.injection.CallableInjector(di, *args, **kwargs)[source]

Bases: mainline.injection.Injector

class mainline.injection.ClassPropertyInjector(di, key, name=None, replace_on_access=False)[source]

Bases: mainline.injection.Injector

class mainline.injection.Injector(di)[source]

Bases: object

decorate(wrapped)[source]
mainline.injection.OBJECT_INIT

Initialize self. See help(type(self)) for accurate signature.

class mainline.injection.SpecInjector(di, *args, **kwargs)[source]

Bases: mainline.injection.CallableInjector

decorate(wrapped)[source]

mainline.provider module

class mainline.provider.IFactoryProvider(factory=None)[source]

Bases: mainline.provider.IProvider

has_factory()[source]
has_instance()[source]
providable
provide(*args, **kwargs)[source]
set_factory(factory)[source]
class mainline.provider.IProvider[source]

Bases: object

has_instance()[source]
providable
provide(*args, **kwargs)[source]
set_instance(instance)[source]
class mainline.provider.Provider(factory, scope=<class 'mainline.scope.NoneScope'>, key='')[source]

Bases: mainline.provider.IFactoryProvider

has_instance()[source]
provide(*args, **kwargs)[source]
scopes = <ScopeRegistry {<class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'global': <class 'mainline.scope.GlobalScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, 'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'process': <class 'mainline.scope.ProcessScope'>}>
set_instance(instance)[source]
mainline.provider.provider_factory(factory=<object object>, scope=<class 'mainline.scope.NoneScope'>)[source]

Decorator to create a provider using the given factory, and scope. Can also be used in a non-decorator manner.

Parameters:scope (object or callable) – Scope key, factory, or instance
Returns:decorator
Return type:decorator

mainline.scope module

class mainline.scope.GlobalScope(*args, **kwargs)[source]

Bases: mainline.scope.IScope

name = 'global'
register = True
class mainline.scope.IScope(*args, **kwargs)[source]

Bases: mainline.utils.ProxyMutableMapping

instances = None
instances_factory

alias of dict

name = None
register = False
class mainline.scope.NamespacedProxyScope(namespace, scope, *args, **kwargs)[source]

Bases: mainline.scope.ProxyScope

name
class mainline.scope.NoneScope(*args, **kwargs)[source]

Bases: mainline.scope.IScope

name = 'none'
register = True
class mainline.scope.ProcessScope(*args, **kwargs)[source]

Bases: mainline.scope.IScope

name = 'process'
register = True
class mainline.scope.ProxyScope(scope, *args, **kwargs)[source]

Bases: mainline.scope.IScope

class mainline.scope.ScopeRegistry[source]

Bases: mainline.utils.ProxyMutableMapping

classmethod is_scope(obj)[source]
classmethod is_scope_factory(obj)[source]
classmethod is_scope_instance(obj)[source]
register_factory(factory, name=None)[source]
resolve(scope_or_scope_factory, instantiate_factory=True)[source]
class mainline.scope.SingletonScope(*args, **kwargs)[source]

Bases: mainline.scope.GlobalScope

Alias for GlobalScope

class mainline.scope.ThreadScope(*args, **kwargs)[source]

Bases: mainline.scope.IScope

instances_factory()[source]
name = 'thread'
register = True

mainline.utils module

mainline.utils.OBJECT_INIT

Initialize self. See help(type(self)) for accurate signature.

class mainline.utils.ProxyMutableMapping(mapping)[source]

Bases: collections.abc.MutableMapping

class mainline.utils.classproperty(f)[source]

Bases: object

Module contents