From 3a7b9c1e98a2b11158088f4ea8068efcb9fb8002 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 8 Apr 2016 18:50:40 +0300 Subject: [PATCH] Refactor dependency_injector.providers package internals --- dependency_injector/providers/__init__.py | 6 +- dependency_injector/providers/base.py | 66 +-------------------- dependency_injector/providers/callable.py | 3 - dependency_injector/providers/config.py | 4 +- dependency_injector/providers/creational.py | 2 - dependency_injector/providers/utils.py | 65 ++++++++++++++++++++ 6 files changed, 71 insertions(+), 75 deletions(-) create mode 100644 dependency_injector/providers/utils.py diff --git a/dependency_injector/providers/__init__.py b/dependency_injector/providers/__init__.py index 4d927de8..99324b3c 100644 --- a/dependency_injector/providers/__init__.py +++ b/dependency_injector/providers/__init__.py @@ -6,8 +6,6 @@ from dependency_injector.providers.base import ( Static, StaticProvider, ExternalDependency, - OverridingContext, - override, ) from dependency_injector.providers.callable import ( Callable, @@ -29,6 +27,10 @@ from dependency_injector.providers.config import ( Config, ChildConfig, ) +from dependency_injector.providers.utils import ( + OverridingContext, + override, +) __all__ = ( diff --git a/dependency_injector/providers/base.py b/dependency_injector/providers/base.py index 093e02fe..ba21300d 100644 --- a/dependency_injector/providers/base.py +++ b/dependency_injector/providers/base.py @@ -2,8 +2,8 @@ import six +from dependency_injector.providers.utils import OverridingContext from dependency_injector.errors import Error - from dependency_injector.utils import ( is_provider, ensure_is_provider, @@ -356,67 +356,3 @@ class ExternalDependency(Provider): return represent_provider(provider=self, provides=self.instance_of) __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 diff --git a/dependency_injector/providers/callable.py b/dependency_injector/providers/callable.py index 65b9abf8..1a21f6ab 100644 --- a/dependency_injector/providers/callable.py +++ b/dependency_injector/providers/callable.py @@ -3,14 +3,11 @@ import six from dependency_injector.providers.base import Provider - from dependency_injector.injections import ( _parse_args_injections, _parse_kwargs_injections, ) - from dependency_injector.utils import represent_provider - from dependency_injector.errors import Error diff --git a/dependency_injector/providers/config.py b/dependency_injector/providers/config.py index c3b004fa..f629f4df 100644 --- a/dependency_injector/providers/config.py +++ b/dependency_injector/providers/config.py @@ -3,10 +3,8 @@ import six from dependency_injector.providers.base import Provider - -from dependency_injector.errors import Error - from dependency_injector.utils import represent_provider +from dependency_injector.errors import Error @six.python_2_unicode_compatible diff --git a/dependency_injector/providers/creational.py b/dependency_injector/providers/creational.py index 322cc519..06a2419c 100644 --- a/dependency_injector/providers/creational.py +++ b/dependency_injector/providers/creational.py @@ -1,13 +1,11 @@ """Dependency injector creational providers.""" from dependency_injector.providers.callable import Callable - from dependency_injector.utils import ( is_attribute_injection, is_method_injection, GLOBAL_LOCK, ) - from dependency_injector.errors import Error diff --git a/dependency_injector/providers/utils.py b/dependency_injector/providers/utils.py new file mode 100644 index 00000000..40b9278e --- /dev/null +++ b/dependency_injector/providers/utils.py @@ -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