mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Refactor dependency_injector.providers package internals
This commit is contained in:
parent
f595b7f670
commit
3a7b9c1e98
|
@ -6,8 +6,6 @@ from dependency_injector.providers.base import (
|
||||||
Static,
|
Static,
|
||||||
StaticProvider,
|
StaticProvider,
|
||||||
ExternalDependency,
|
ExternalDependency,
|
||||||
OverridingContext,
|
|
||||||
override,
|
|
||||||
)
|
)
|
||||||
from dependency_injector.providers.callable import (
|
from dependency_injector.providers.callable import (
|
||||||
Callable,
|
Callable,
|
||||||
|
@ -29,6 +27,10 @@ from dependency_injector.providers.config import (
|
||||||
Config,
|
Config,
|
||||||
ChildConfig,
|
ChildConfig,
|
||||||
)
|
)
|
||||||
|
from dependency_injector.providers.utils import (
|
||||||
|
OverridingContext,
|
||||||
|
override,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
from dependency_injector.providers.utils import OverridingContext
|
||||||
from dependency_injector.errors import Error
|
from dependency_injector.errors import Error
|
||||||
|
|
||||||
from dependency_injector.utils import (
|
from dependency_injector.utils import (
|
||||||
is_provider,
|
is_provider,
|
||||||
ensure_is_provider,
|
ensure_is_provider,
|
||||||
|
@ -356,67 +356,3 @@ class ExternalDependency(Provider):
|
||||||
return represent_provider(provider=self, provides=self.instance_of)
|
return represent_provider(provider=self, provides=self.instance_of)
|
||||||
|
|
||||||
__repr__ = __str__
|
__repr__ = __str__
|
||||||
|
|
||||||
|
|
||||||
class OverridingContext(object):
|
|
||||||
"""Provider overriding context.
|
|
||||||
|
|
||||||
:py:class:`OverridingContext` is used by :py:meth:`Provider.override` for
|
|
||||||
implemeting ``with`` contexts. When :py:class:`OverridingContext` is
|
|
||||||
closed, overriding that was created in this context is dropped also.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
with provider.override(another_provider):
|
|
||||||
assert provider.is_overridden
|
|
||||||
assert not provider.is_overridden
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, overridden, overriding):
|
|
||||||
"""Initializer.
|
|
||||||
|
|
||||||
:param overridden: Overridden provider.
|
|
||||||
:type overridden: :py:class:`Provider`
|
|
||||||
|
|
||||||
:param overriding: Overriding provider.
|
|
||||||
:type overriding: :py:class:`Provider`
|
|
||||||
"""
|
|
||||||
self.overridden = overridden
|
|
||||||
self.overriding = overriding
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
"""Do nothing."""
|
|
||||||
return self.overriding
|
|
||||||
|
|
||||||
def __exit__(self, *_):
|
|
||||||
"""Exit overriding context."""
|
|
||||||
self.overridden.reset_last_overriding()
|
|
||||||
|
|
||||||
|
|
||||||
def override(overridden):
|
|
||||||
"""Decorator for overriding providers.
|
|
||||||
|
|
||||||
This decorator overrides ``overridden`` provider by decorated one.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@Factory
|
|
||||||
class SomeClass(object):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@override(SomeClass)
|
|
||||||
@Factory
|
|
||||||
class ExtendedSomeClass(SomeClass.cls):
|
|
||||||
pass
|
|
||||||
|
|
||||||
:param overridden: Provider that should be overridden.
|
|
||||||
:type overridden: :py:class:`Provider`
|
|
||||||
|
|
||||||
:return: Overriding provider.
|
|
||||||
:rtype: :py:class:`Provider`
|
|
||||||
"""
|
|
||||||
def decorator(overriding):
|
|
||||||
overridden.override(overriding)
|
|
||||||
return overriding
|
|
||||||
return decorator
|
|
||||||
|
|
|
@ -3,14 +3,11 @@
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from dependency_injector.providers.base import Provider
|
from dependency_injector.providers.base import Provider
|
||||||
|
|
||||||
from dependency_injector.injections import (
|
from dependency_injector.injections import (
|
||||||
_parse_args_injections,
|
_parse_args_injections,
|
||||||
_parse_kwargs_injections,
|
_parse_kwargs_injections,
|
||||||
)
|
)
|
||||||
|
|
||||||
from dependency_injector.utils import represent_provider
|
from dependency_injector.utils import represent_provider
|
||||||
|
|
||||||
from dependency_injector.errors import Error
|
from dependency_injector.errors import Error
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,8 @@
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from dependency_injector.providers.base import Provider
|
from dependency_injector.providers.base import Provider
|
||||||
|
|
||||||
from dependency_injector.errors import Error
|
|
||||||
|
|
||||||
from dependency_injector.utils import represent_provider
|
from dependency_injector.utils import represent_provider
|
||||||
|
from dependency_injector.errors import Error
|
||||||
|
|
||||||
|
|
||||||
@six.python_2_unicode_compatible
|
@six.python_2_unicode_compatible
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
"""Dependency injector creational providers."""
|
"""Dependency injector creational providers."""
|
||||||
|
|
||||||
from dependency_injector.providers.callable import Callable
|
from dependency_injector.providers.callable import Callable
|
||||||
|
|
||||||
from dependency_injector.utils import (
|
from dependency_injector.utils import (
|
||||||
is_attribute_injection,
|
is_attribute_injection,
|
||||||
is_method_injection,
|
is_method_injection,
|
||||||
GLOBAL_LOCK,
|
GLOBAL_LOCK,
|
||||||
)
|
)
|
||||||
|
|
||||||
from dependency_injector.errors import Error
|
from dependency_injector.errors import Error
|
||||||
|
|
||||||
|
|
||||||
|
|
65
dependency_injector/providers/utils.py
Normal file
65
dependency_injector/providers/utils.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
"""Dependency injector provider utils."""
|
||||||
|
|
||||||
|
|
||||||
|
class OverridingContext(object):
|
||||||
|
"""Provider overriding context.
|
||||||
|
|
||||||
|
:py:class:`OverridingContext` is used by :py:meth:`Provider.override` for
|
||||||
|
implemeting ``with`` contexts. When :py:class:`OverridingContext` is
|
||||||
|
closed, overriding that was created in this context is dropped also.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
with provider.override(another_provider):
|
||||||
|
assert provider.is_overridden
|
||||||
|
assert not provider.is_overridden
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, overridden, overriding):
|
||||||
|
"""Initializer.
|
||||||
|
|
||||||
|
:param overridden: Overridden provider.
|
||||||
|
:type overridden: :py:class:`Provider`
|
||||||
|
|
||||||
|
:param overriding: Overriding provider.
|
||||||
|
:type overriding: :py:class:`Provider`
|
||||||
|
"""
|
||||||
|
self.overridden = overridden
|
||||||
|
self.overriding = overriding
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Do nothing."""
|
||||||
|
return self.overriding
|
||||||
|
|
||||||
|
def __exit__(self, *_):
|
||||||
|
"""Exit overriding context."""
|
||||||
|
self.overridden.reset_last_overriding()
|
||||||
|
|
||||||
|
|
||||||
|
def override(overridden):
|
||||||
|
"""Decorator for overriding providers.
|
||||||
|
|
||||||
|
This decorator overrides ``overridden`` provider by decorated one.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
@Factory
|
||||||
|
class SomeClass(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@override(SomeClass)
|
||||||
|
@Factory
|
||||||
|
class ExtendedSomeClass(SomeClass.cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
:param overridden: Provider that should be overridden.
|
||||||
|
:type overridden: :py:class:`Provider`
|
||||||
|
|
||||||
|
:return: Overriding provider.
|
||||||
|
:rtype: :py:class:`Provider`
|
||||||
|
"""
|
||||||
|
def decorator(overriding):
|
||||||
|
overridden.override(overriding)
|
||||||
|
return overriding
|
||||||
|
return decorator
|
Loading…
Reference in New Issue
Block a user