2016-11-04 12:12:37 +03:00
|
|
|
"""Dependency injector utils.
|
|
|
|
|
|
|
|
Powered by Cython.
|
|
|
|
"""
|
|
|
|
|
|
|
|
cimport cpython.version
|
|
|
|
|
|
|
|
from dependency_injector cimport errors
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2016-06-01 18:53:35 +03:00
|
|
|
import copy as _copy
|
2016-04-10 17:14:11 +03:00
|
|
|
import types
|
2015-09-04 02:33:15 +03:00
|
|
|
import threading
|
|
|
|
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-09-04 02:33:15 +03:00
|
|
|
GLOBAL_LOCK = threading.RLock()
|
2015-11-25 15:41:03 +03:00
|
|
|
"""Dependency injector global reentrant lock.
|
|
|
|
|
|
|
|
:type: :py:class:`threading.RLock`
|
|
|
|
"""
|
2015-09-04 02:33:15 +03:00
|
|
|
|
2016-11-04 12:12:37 +03:00
|
|
|
if cpython.version.PY_MAJOR_VERSION < 3: # pragma: no cover
|
|
|
|
CLASS_TYPES = (type, types.ClassType)
|
|
|
|
|
2016-06-01 18:53:35 +03:00
|
|
|
_copy._deepcopy_dispatch[types.MethodType] = \
|
2016-04-10 17:14:11 +03:00
|
|
|
lambda obj, memo: type(obj)(obj.im_func,
|
2016-06-01 18:53:35 +03:00
|
|
|
_copy.deepcopy(obj.im_self, memo),
|
2016-04-10 17:14:11 +03:00
|
|
|
obj.im_class)
|
2016-11-04 12:12:37 +03:00
|
|
|
else: # pragma: no cover
|
|
|
|
CLASS_TYPES = (type,)
|
2016-04-10 17:14:11 +03:00
|
|
|
|
2015-09-04 02:33:15 +03:00
|
|
|
|
2016-11-04 12:12:37 +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 12:12:37 +03:00
|
|
|
return (not isinstance(instance, CLASS_TYPES) and
|
|
|
|
getattr(instance, '__IS_PROVIDER__', False) is True)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
2016-11-04 12:12:37 +03:00
|
|
|
cpdef object ensure_is_provider(object instance):
|
2015-10-19 12:12:38 +03:00
|
|
|
"""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`
|
2015-10-19 12:12:38 +03:00
|
|
|
"""
|
2015-03-13 18:46:03 +03:00
|
|
|
if not is_provider(instance):
|
2016-05-29 16:39:39 +03:00
|
|
|
raise errors.Error('Expected provider instance, '
|
|
|
|
'got {0}'.format(str(instance)))
|
2015-03-13 18:46:03 +03:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2016-11-04 12:12:37 +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 12:12:37 +03:00
|
|
|
return (not isinstance(instance, CLASS_TYPES) and
|
|
|
|
getattr(instance, '__IS_DELEGATED__', False) is True)
|
2016-11-04 11:41:40 +03:00
|
|
|
|
|
|
|
|
2016-11-04 12:12:37 +03:00
|
|
|
cpdef bint is_container(object instance):
|
2016-05-30 23:34:14 +03:00
|
|
|
"""Check if instance is container instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2016-11-04 12:12:37 +03:00
|
|
|
return getattr(instance, '__IS_CONTAINER__', False) is True
|
2016-05-30 23:34:14 +03:00
|
|
|
|
|
|
|
|
2016-11-04 12:12:37 +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)))
|
2016-03-01 16:42:06 +03:00
|
|
|
|
|
|
|
|
2016-11-04 12:12:37 +03:00
|
|
|
cpdef object deepcopy(object instance, dict memo=None):
|
2016-06-01 18:53:35 +03:00
|
|
|
"""Make full copy of instance."""
|
|
|
|
return _copy.deepcopy(instance, memo)
|
2016-11-04 12:12:37 +03:00
|
|
|
|