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]

Update 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 Provider(factory, scope=<class 'mainline.scope.NoneScope'>, key='')

Bases: mainline.provider.IFactoryProvider

has_instance()
provide(*args, **kwargs)
scopes = <ScopeRegistry {'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'global': <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, 'process': <class 'mainline.scope.ProcessScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, 'context': <class 'mainline.scope.ContextScope'>, <class 'mainline.scope.ContextScope'>: <class 'mainline.scope.ContextScope'>}>
set_instance(instance)
ai(*args, **kwargs)

Decorator that magically inspects the argspec of the wrapped upon call, injecting provider instances as names match.

Performance wise, this is currently slower than inject_exact as more is done each runtime to determine what needs to be injected.

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 args are added as dependencies for the wrapped.

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

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

decorator

Return type:

decorator

auto_inject(*args, **kwargs)[source]

Decorator that magically inspects the argspec of the wrapped upon call, injecting provider instances as names match.

Performance wise, this is currently slower than inject_exact as more is done each runtime to determine what needs to be injected.

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 args are added as dependencies for the wrapped.

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

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

decorator

Return type:

decorator

dependencies

Public attribute for dependency mapping

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
f(key, factory=<object object>, scope=<class 'mainline.scope.NoneScope'>, allow_overwrite=False)

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

get_deps(obj)[source]

Returns dependencies for key.

Parameters:key (object) – Dependent holder key
Returns:Dependencies
Return type:set
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
i(*args, **kwargs)

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 args are added as dependencies for the wrapped.

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

decorator

Return type:

decorator

inject(*args, **kwargs)

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 args are added as dependencies for the wrapped.

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

decorator

Return type:

decorator

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
inject_exact(*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 args are added as dependencies for the wrapped.

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

decorator

Return type:

decorator

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 p(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
static 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
providers

Public attribute for provider mapping

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

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
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
scopes = <ScopeRegistry {'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'global': <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, 'process': <class 'mainline.scope.ProcessScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, 'context': <class 'mainline.scope.ContextScope'>, <class 'mainline.scope.ContextScope'>: <class 'mainline.scope.ContextScope'>}>
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
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

Injects inferred deps into a callable’s args and kwargs at execution time.

Deps are inferred based on the callable’s argspec (ie the names of the arguments), and/or function annotations.

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

Bases: mainline.injection.Injector

Base injector for callables.

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

Bases: mainline.injection.Injector

Injects requested dep into a classproperty.

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

Bases: object

Base injector abc.

decorate(wrapped)[source]
exception mainline.injection.NotFound[source]

Bases: Exception

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

Injects requested deps into a callable’s args and kwargs at execution time, taking callable’s argspec into account.

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 {'none': <class 'mainline.scope.NoneScope'>, <class 'mainline.scope.NoneScope'>: <class 'mainline.scope.NoneScope'>, 'global': <class 'mainline.scope.GlobalScope'>, <class 'mainline.scope.GlobalScope'>: <class 'mainline.scope.GlobalScope'>, 'process': <class 'mainline.scope.ProcessScope'>, <class 'mainline.scope.ProcessScope'>: <class 'mainline.scope.ProcessScope'>, 'thread': <class 'mainline.scope.ThreadScope'>, <class 'mainline.scope.ThreadScope'>: <class 'mainline.scope.ThreadScope'>, 'context': <class 'mainline.scope.ContextScope'>, <class 'mainline.scope.ContextScope'>: <class 'mainline.scope.ContextScope'>}>
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.ContextScope(*args, **kwargs)[source]

Bases: mainline.scope.IScope

name = 'context'
register = True
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 builtins.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]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
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, fancy_repr=True, dictify_repr=False)[source]

Bases: collections.abc.MutableMapping

Proxies access to an existing dict-like object.

>>> a = dict(whoa=True, hello=[1,2,3], why='always')
>>> b = ProxyMutableMapping(a)

Nice reprs:

>>> b
<ProxyMutableMapping {...}>

Setting works as you’d expect:

>>> a['nice'] = b['nice'] = False
>>> a['whoa'] = b['whoa'] = 'yeee'

Checking that the changes are in fact being performed on the proxied object:

>>> b == a
True
class mainline.utils.classproperty(f)[source]

Bases: object

Module contents

mainline: simple python dependency injection.