mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Refactor dependency_injector.catalogs package internals
This commit is contained in:
parent
a07bb582ec
commit
f595b7f670
|
@ -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',
|
||||
)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
59
dependency_injector/catalogs/utils.py
Normal file
59
dependency_injector/catalogs/utils.py
Normal 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
|
Loading…
Reference in New Issue
Block a user