2016-04-08 18:49:23 +03:00
|
|
|
"""Dependency injector catalog utils."""
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
2016-04-10 17:14:11 +03:00
|
|
|
from dependency_injector.utils import _copy_providers
|
2016-04-10 16:52:37 +03:00
|
|
|
from dependency_injector.errors import UndefinedProviderError
|
|
|
|
|
2016-04-08 18:49:23 +03:00
|
|
|
|
|
|
|
def copy(catalog):
|
|
|
|
""":py:class:`DeclarativeCatalog` copying decorator.
|
|
|
|
|
2016-04-10 16:52:37 +03:00
|
|
|
This decorator copy all providers from provided catalog to decorated one.
|
|
|
|
If one of the decorated catalog providers matches to source catalog
|
|
|
|
providers by name, it would be replaced by reference.
|
|
|
|
|
2016-04-08 18:49:23 +03:00
|
|
|
:param catalog: Catalog that should be copied by decorated catalog.
|
|
|
|
:type catalog: :py:class:`dependency_injector.catalogs.DeclarativeCatalog`
|
|
|
|
|
|
|
|
:return: Declarative catalog's copying decorator.
|
|
|
|
:rtype:
|
2016-04-10 16:52:37 +03:00
|
|
|
callable(:py:class:`DeclarativeCatalog`)
|
2016-04-08 18:49:23 +03:00
|
|
|
"""
|
2016-04-10 16:52:37 +03:00
|
|
|
def decorator(copied_catalog):
|
|
|
|
"""Copying decorator.
|
2016-04-08 18:49:23 +03:00
|
|
|
|
2016-04-10 16:52:37 +03:00
|
|
|
:param copied_catalog: Decorated catalog.
|
|
|
|
:type copied_catalog: :py:class:`DeclarativeCatalog`
|
2016-04-08 18:49:23 +03:00
|
|
|
|
|
|
|
:return: Decorated catalog.
|
|
|
|
:rtype:
|
2016-04-10 16:52:37 +03:00
|
|
|
:py:class:`DeclarativeCatalog`
|
2016-04-08 18:49:23 +03:00
|
|
|
"""
|
|
|
|
memo = dict()
|
2016-04-10 16:52:37 +03:00
|
|
|
for name, provider in six.iteritems(copied_catalog.cls_providers):
|
|
|
|
try:
|
|
|
|
source_provider = catalog.get_provider(name)
|
|
|
|
except UndefinedProviderError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
memo[id(source_provider)] = provider
|
|
|
|
|
2016-04-10 17:14:11 +03:00
|
|
|
copied_catalog.bind_providers(_copy_providers(catalog.providers, memo),
|
2016-04-10 16:52:37 +03:00
|
|
|
force=True)
|
|
|
|
|
|
|
|
return copied_catalog
|
2016-04-08 18:49:23 +03:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def override(catalog):
|
|
|
|
""":py:class:`DeclarativeCatalog` overriding decorator.
|
|
|
|
|
|
|
|
:param catalog: Catalog that should be overridden by decorated catalog.
|
2016-04-10 16:52:37 +03:00
|
|
|
:type catalog: :py:class:`DeclarativeCatalog`
|
2016-04-08 18:49:23 +03:00
|
|
|
|
|
|
|
:return: Declarative catalog's overriding decorator.
|
|
|
|
:rtype: callable(:py:class:`DeclarativeCatalog`)
|
|
|
|
"""
|
|
|
|
def decorator(overriding_catalog):
|
|
|
|
"""Overriding decorator."""
|
|
|
|
catalog.override(overriding_catalog)
|
|
|
|
return overriding_catalog
|
|
|
|
return decorator
|