2015-03-11 13:18:42 +00:00
|
|
|
"""Utils module."""
|
|
|
|
|
2015-09-03 23:33:15 +00:00
|
|
|
import threading
|
|
|
|
|
2015-08-31 21:36:26 +00:00
|
|
|
import six
|
2015-03-12 10:01:56 +00:00
|
|
|
|
2015-03-13 15:46:03 +00:00
|
|
|
from .errors import Error
|
|
|
|
|
2015-03-11 13:18:42 +00:00
|
|
|
|
2015-09-03 23:33:15 +00:00
|
|
|
GLOBAL_LOCK = threading.RLock()
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Dependency injector global reentrant lock.
|
|
|
|
|
|
|
|
:type: :py:class:`threading.RLock`
|
|
|
|
"""
|
2015-09-03 23:33:15 +00:00
|
|
|
|
|
|
|
|
2015-03-11 13:18:42 +00:00
|
|
|
def is_provider(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is provider instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-08-31 21:36:26 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_PROVIDER__') and
|
|
|
|
getattr(instance, '__IS_PROVIDER__') is True)
|
2015-03-11 13:18:42 +00:00
|
|
|
|
|
|
|
|
2015-03-13 15:46:03 +00:00
|
|
|
def ensure_is_provider(instance):
|
2015-10-19 09:12:38 +00:00
|
|
|
"""Check if instance is provider instance and return it.
|
|
|
|
|
2015-11-25 12:41:03 +00: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 09:12:38 +00:00
|
|
|
"""
|
2015-03-13 15:46:03 +00:00
|
|
|
if not is_provider(instance):
|
|
|
|
raise Error('Expected provider instance, '
|
2015-03-18 10:48:19 +00:00
|
|
|
'got {0}'.format(str(instance)))
|
2015-03-13 15:46:03 +00:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2015-03-11 13:18:42 +00:00
|
|
|
def is_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is injection instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-08-31 21:36:26 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_INJECTION__') and
|
|
|
|
getattr(instance, '__IS_INJECTION__') is True)
|
2015-03-11 13:18:42 +00:00
|
|
|
|
|
|
|
|
2015-03-22 23:04:18 +00:00
|
|
|
def ensure_is_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is injection instance and return it.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if provided instance is
|
|
|
|
not injection.
|
|
|
|
|
|
|
|
:rtype: :py:class:`dependency_injector.injections.Injection`
|
|
|
|
"""
|
2015-03-22 23:04:18 +00:00
|
|
|
if not is_injection(instance):
|
|
|
|
raise Error('Expected injection instance, '
|
|
|
|
'got {0}'.format(str(instance)))
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2015-10-14 11:30:01 +00:00
|
|
|
def is_arg_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is positional argument injection instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-10-14 11:30:01 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_ARG_INJECTION__') and
|
2015-10-14 11:30:01 +00:00
|
|
|
getattr(instance, '__IS_ARG_INJECTION__', False) is True)
|
|
|
|
|
|
|
|
|
2015-03-22 23:04:18 +00:00
|
|
|
def is_kwarg_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is keyword argument injection instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-08-31 21:36:26 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_KWARG_INJECTION__') and
|
2015-07-22 07:53:16 +00:00
|
|
|
getattr(instance, '__IS_KWARG_INJECTION__', False) is True)
|
2015-03-11 13:18:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_attribute_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is attribute injection instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-08-31 21:36:26 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_ATTRIBUTE_INJECTION__') and
|
2015-07-22 07:53:16 +00:00
|
|
|
getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True)
|
2015-03-11 13:18:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_method_injection(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is method injection instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-08-31 21:36:26 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_METHOD_INJECTION__') and
|
2015-07-22 07:53:16 +00:00
|
|
|
getattr(instance, '__IS_METHOD_INJECTION__', False) is True)
|
2015-07-26 22:44:20 +00:00
|
|
|
|
|
|
|
|
2015-10-07 10:36:28 +00:00
|
|
|
def is_catalog(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is catalog instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-11-10 17:38:18 +00:00
|
|
|
return (hasattr(instance, '__IS_CATALOG__') and
|
2015-10-07 10:36:28 +00:00
|
|
|
getattr(instance, '__IS_CATALOG__', False) is True)
|
|
|
|
|
|
|
|
|
2015-11-10 17:38:18 +00:00
|
|
|
def is_dynamic_catalog(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is dynamic catalog instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-11-10 17:38:18 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and is_catalog(instance))
|
|
|
|
|
|
|
|
|
|
|
|
def is_declarative_catalog(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is declarative catalog instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-11-10 17:38:18 +00:00
|
|
|
return (isinstance(instance, six.class_types) and is_catalog(instance))
|
|
|
|
|
|
|
|
|
2015-10-19 09:12:38 +00:00
|
|
|
def is_catalog_bundle(instance):
|
2015-11-25 12:41:03 +00:00
|
|
|
"""Check if instance is catalog bundle instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2015-10-19 09:12:38 +00:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-10-23 15:07:52 +00:00
|
|
|
hasattr(instance, '__IS_CATALOG_BUNDLE__') and
|
2015-10-19 09:12:38 +00:00
|
|
|
getattr(instance, '__IS_CATALOG_BUNDLE__', False) is True)
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_is_catalog_bundle(instance):
|
|
|
|
"""Check if instance is catalog bundle instance and return it.
|
|
|
|
|
2015-11-25 12:41:03 +00:00
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if provided instance
|
|
|
|
is not catalog bundle.
|
|
|
|
|
|
|
|
:rtype: :py:class:`dependency_injector.catalogs.CatalogBundle`
|
2015-10-19 09:12:38 +00:00
|
|
|
"""
|
|
|
|
if not is_catalog_bundle(instance):
|
|
|
|
raise Error('Expected catalog bundle instance, '
|
|
|
|
'got {0}'.format(str(instance)))
|
|
|
|
return instance
|