python-dependency-injector/src/dependency_injector/providers/utils.pyx

115 lines
3.1 KiB
Cython
Raw Normal View History

2016-11-04 16:48:26 +03:00
"""Dependency injector provider utils.
2016-11-04 12:12:37 +03:00
2016-11-04 16:48:26 +03:00
Powered by Cython.
"""
import sys
import types
2015-09-04 02:33:15 +03:00
import threading
2016-11-04 16:48:26 +03:00
from dependency_injector.errors import Error
2016-11-04 19:35:53 +03:00
from .base cimport Provider
2016-11-04 16:48:26 +03:00
if sys.version_info[0] == 3: # pragma: no cover
2016-11-04 19:35:53 +03:00
CLASS_TYPES = (type,)
2016-11-04 16:48:26 +03:00
else: # pragma: no cover
2016-11-04 19:35:53 +03:00
CLASS_TYPES = (type, types.ClassType)
cdef 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.overridden
assert not provider.overridden
"""
def __init__(self, Provider overridden, Provider 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()
2016-11-04 16:48:26 +03:00
2015-09-04 02:33:15 +03:00
2016-11-04 16:48:26 +03:00
cpdef bint is_provider(object instance):
2015-11-25 15:41:03 +03:00
"""Check if instance is provider instance.
:param instance: Instance to be checked.
:type instance: object
:rtype: bool
"""
2016-11-04 19:35:53 +03:00
return (not isinstance(instance, CLASS_TYPES) and
2016-11-04 12:12:37 +03:00
getattr(instance, '__IS_PROVIDER__', False) is True)
2016-11-04 16:48:26 +03:00
cpdef object ensure_is_provider(object instance):
"""Check if instance is provider instance and return it.
2015-11-25 15:41:03 +03:00
:param instance: Instance to be checked.
:type instance: object
:raise: :py:exc:`dependency_injector.errors.Error` if provided instance is
not provider.
:rtype: :py:class:`dependency_injector.providers.Provider`
"""
if not is_provider(instance):
2016-11-04 14:06:47 +03:00
raise Error('Expected provider instance, '
'got {0}'.format(str(instance)))
return instance
2016-11-04 16:48:26 +03:00
cpdef bint is_delegated(object instance):
2016-11-04 11:41:40 +03:00
"""Check if instance is delegated provider.
:param instance: Instance to be checked.
:type instance: object
:rtype: bool
"""
2016-11-04 19:35:53 +03:00
return (not isinstance(instance, CLASS_TYPES) and
2016-11-04 12:12:37 +03:00
getattr(instance, '__IS_DELEGATED__', False) is True)
2016-11-04 11:41:40 +03:00
2016-11-04 16:48:26 +03:00
cpdef str represent_provider(object provider, object provides):
2015-12-11 12:18:09 +03:00
"""Return string representation of provider.
:param provider: Provider object
:type provider: :py:class:`dependency_injector.providers.Provider`
:param provides: Object that provider provides
:type provider: object
:return: String representation of provider
:rtype: str
"""
return '<{provider}({provides}) at {address}>'.format(
provider='.'.join((provider.__class__.__module__,
provider.__class__.__name__)),
provides=repr(provides) if provides is not None else '',
address=hex(id(provider)))