mainline package¶
Submodules¶
mainline.catalog module¶
- class mainline.catalog.Catalog(*args, **kwargs)[source]¶
Bases:
ICatalog,ProviderMapping
- class mainline.catalog.CatalogMeta(class_name, bases, attributes)[source]¶
Bases:
ABCMetaMeta class used to populate providers from attributes of Catalog subclass declarations.
- class mainline.catalog.ICatalog[source]¶
Bases:
objectInherit from this class to note that you support the ICatalog interface
- class mainline.catalog.ProviderMapping(*args, **kwargs)[source]¶
Bases:
ProxyMutableMappingMixin to provide mapping interface on providers.
mainline.di module¶
- class mainline.di.Di(providers_factory=<class 'mainline.catalog.Catalog'>, dependencies_factory=<class 'dict'>)[source]¶
Bases:
ICatalogDependency injection container.
- class Catalog(*args, **kwargs)¶
Bases:
ICatalog,ProviderMapping
- class Provider(factory, scope=<class 'mainline.scope.NoneScope'>, key='')¶
Bases:
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.
- 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.
- property 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.
- get_missing_deps(obj)[source]¶
Returns missing dependencies for provider key. Missing meaning no instance can be provided at this time.
- 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.
- 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.
- 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.
- 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.
- 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
- property 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.
- resolve(*keys)[source]¶
Returns resolved instances for given provider keys.
If only one positional argument is given, only one is returned.
- 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.
mainline.exceptions module¶
mainline.injection module¶
- class mainline.injection.AutoSpecInjector(di, *args, **kwargs)[source]¶
Bases:
CallableInjectorInjects 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.
- class mainline.injection.CallableInjector(di, *args, **kwargs)[source]¶
Bases:
InjectorBase injector for callables.
- class mainline.injection.ClassPropertyInjector(di, key, name=None, replace_on_access=False)[source]¶
Bases:
InjectorInjects requested dep into a classproperty.
- mainline.injection.OBJECT_INIT(self, /, *args, **kwargs)¶
Initialize self. See help(type(self)) for accurate signature.
- class mainline.injection.SpecInjector(di, *args, **kwargs)[source]¶
Bases:
CallableInjectorInjects requested deps into a callable’s args and kwargs at execution time, taking callable’s argspec into account.
mainline.provider module¶
- class mainline.provider.IFactoryProvider(factory=None)[source]¶
Bases:
IProvider- property providable¶
- class mainline.provider.Provider(factory, scope=<class 'mainline.scope.NoneScope'>, key='')[source]¶
Bases:
IFactoryProvider- 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'>}>¶
- 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:
IScope- name = 'context'¶
- register = True¶
- class mainline.scope.GlobalScope(*args, **kwargs)[source]¶
Bases:
IScope- name = 'global'¶
- register = True¶
- class mainline.scope.IScope(*args, **kwargs)[source]¶
Bases:
ProxyMutableMapping- instances = None¶
- name = None¶
- register = False¶
- class mainline.scope.NamespacedProxyScope(namespace, scope, *args, **kwargs)[source]¶
Bases:
ProxyScope- property name¶
- class mainline.scope.NoneScope(*args, **kwargs)[source]¶
Bases:
IScope- name = 'none'¶
- register = True¶
- class mainline.scope.ProcessScope(*args, **kwargs)[source]¶
Bases:
IScope- name = 'process'¶
- register = True¶
- class mainline.scope.ScopeRegistry[source]¶
Bases:
ProxyMutableMapping
- class mainline.scope.SingletonScope(*args, **kwargs)[source]¶
Bases:
GlobalScopeAlias for GlobalScope
- class mainline.scope.ThreadScope(*args, **kwargs)[source]¶
Bases:
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(self, /, *args, **kwargs)¶
Initialize self. See help(type(self)) for accurate signature.
- class mainline.utils.ProxyMutableMapping(mapping, fancy_repr=True, dictify_repr=False)[source]¶
Bases:
MutableMappingProxies 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
Module contents¶
mainline: simple python dependency injection.