diff --git a/dependency_injector/catalogs/__init__.py b/dependency_injector/catalogs/__init__.py index 7b2baae2..a7405e76 100644 --- a/dependency_injector/catalogs/__init__.py +++ b/dependency_injector/catalogs/__init__.py @@ -1,30 +1,16 @@ """Dependency injector catalogs package.""" from dependency_injector.catalogs.bundle import CatalogBundle - from dependency_injector.catalogs.dynamic import DynamicCatalog - from dependency_injector.catalogs.declarative import ( DeclarativeCatalogMetaClass, DeclarativeCatalog, AbstractCatalog, ) - - -def override(catalog): - """:py:class:`DeclarativeCatalog` overriding decorator. - - :param catalog: Catalog that should be overridden by decorated catalog. - :type catalog: :py:class:`DeclarativeCatalog` | :py:class:`DynamicCatalog` - - :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 +from dependency_injector.catalogs.utils import ( + copy, + override +) __all__ = ( @@ -33,5 +19,6 @@ __all__ = ( 'DeclarativeCatalogMetaClass', 'DeclarativeCatalog', 'AbstractCatalog', + 'copy', 'override', ) diff --git a/dependency_injector/catalogs/declarative.py b/dependency_injector/catalogs/declarative.py index c1ad3310..0ef0da13 100644 --- a/dependency_injector/catalogs/declarative.py +++ b/dependency_injector/catalogs/declarative.py @@ -4,13 +4,11 @@ import six from dependency_injector.catalogs.dynamic import DynamicCatalog from dependency_injector.catalogs.bundle import CatalogBundle - from dependency_injector.utils import ( is_provider, is_catalog, is_declarative_catalog, ) - from dependency_injector.errors import ( Error, UndefinedProviderError, diff --git a/dependency_injector/catalogs/dynamic.py b/dependency_injector/catalogs/dynamic.py index 402a090b..d8e1618f 100644 --- a/dependency_injector/catalogs/dynamic.py +++ b/dependency_injector/catalogs/dynamic.py @@ -3,13 +3,11 @@ import six from dependency_injector.catalogs.bundle import CatalogBundle - from dependency_injector.utils import ( is_provider, ensure_is_provider, ensure_is_catalog_bundle, ) - from dependency_injector.errors import ( Error, UndefinedProviderError, diff --git a/dependency_injector/catalogs/utils.py b/dependency_injector/catalogs/utils.py new file mode 100644 index 00000000..fc7f30dd --- /dev/null +++ b/dependency_injector/catalogs/utils.py @@ -0,0 +1,59 @@ +"""Dependency injector catalog utils.""" + +import six + +from copy import deepcopy + + +def copy(catalog): + """:py:class:`DeclarativeCatalog` copying decorator. + + :param catalog: Catalog that should be copied by decorated catalog. + :type catalog: :py:class:`dependency_injector.catalogs.DeclarativeCatalog` + | :py:class:`dependency_injector.catalogs.DynamicCatalog` + + :return: Declarative catalog's copying decorator. + :rtype: + callable(:py:class:`dependency_injector.catalogs.DeclarativeCatalog`) + """ + def decorator(overriding_catalog): + """Overriding decorator. + + :param catalog: Decorated catalog. + :type catalog: + :py:class:`dependency_injector.catalogs.DeclarativeCatalog` + + :return: Decorated catalog. + :rtype: + :py:class:`dependency_injector.catalogs.DeclarativeCatalog` + """ + memo = dict() + + for name, provider in six.iteritems(overriding_catalog.providers): + memo[id(catalog.get_provider(name))] = provider + + dynamic_catalog_copy = deepcopy(catalog._catalog, memo) + + print dynamic_catalog_copy.providers + + for name, provider in six.iteritems(dynamic_catalog_copy.providers): + overriding_catalog.bind_provider(name, provider) + + return overriding_catalog + return decorator + + +def override(catalog): + """:py:class:`DeclarativeCatalog` overriding decorator. + + :param catalog: Catalog that should be overridden by decorated catalog. + :type catalog: :py:class:`DeclarativeCatalog` | :py:class:`DynamicCatalog` + + :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