Refactor dependency_injector.catalogs package internals

This commit is contained in:
Roman Mogilatov 2016-04-08 18:49:23 +03:00
parent a07bb582ec
commit f595b7f670
4 changed files with 64 additions and 22 deletions

View File

@ -1,30 +1,16 @@
"""Dependency injector catalogs package.""" """Dependency injector catalogs package."""
from dependency_injector.catalogs.bundle import CatalogBundle from dependency_injector.catalogs.bundle import CatalogBundle
from dependency_injector.catalogs.dynamic import DynamicCatalog from dependency_injector.catalogs.dynamic import DynamicCatalog
from dependency_injector.catalogs.declarative import ( from dependency_injector.catalogs.declarative import (
DeclarativeCatalogMetaClass, DeclarativeCatalogMetaClass,
DeclarativeCatalog, DeclarativeCatalog,
AbstractCatalog, AbstractCatalog,
) )
from dependency_injector.catalogs.utils import (
copy,
def override(catalog): override
""":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
__all__ = ( __all__ = (
@ -33,5 +19,6 @@ __all__ = (
'DeclarativeCatalogMetaClass', 'DeclarativeCatalogMetaClass',
'DeclarativeCatalog', 'DeclarativeCatalog',
'AbstractCatalog', 'AbstractCatalog',
'copy',
'override', 'override',
) )

View File

@ -4,13 +4,11 @@ import six
from dependency_injector.catalogs.dynamic import DynamicCatalog from dependency_injector.catalogs.dynamic import DynamicCatalog
from dependency_injector.catalogs.bundle import CatalogBundle from dependency_injector.catalogs.bundle import CatalogBundle
from dependency_injector.utils import ( from dependency_injector.utils import (
is_provider, is_provider,
is_catalog, is_catalog,
is_declarative_catalog, is_declarative_catalog,
) )
from dependency_injector.errors import ( from dependency_injector.errors import (
Error, Error,
UndefinedProviderError, UndefinedProviderError,

View File

@ -3,13 +3,11 @@
import six import six
from dependency_injector.catalogs.bundle import CatalogBundle from dependency_injector.catalogs.bundle import CatalogBundle
from dependency_injector.utils import ( from dependency_injector.utils import (
is_provider, is_provider,
ensure_is_provider, ensure_is_provider,
ensure_is_catalog_bundle, ensure_is_catalog_bundle,
) )
from dependency_injector.errors import ( from dependency_injector.errors import (
Error, Error,
UndefinedProviderError, UndefinedProviderError,

View File

@ -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