2015-03-11 16:18:42 +03:00
|
|
|
"""Utils module."""
|
|
|
|
|
2015-09-04 02:33:15 +03:00
|
|
|
import threading
|
2015-10-14 14:30:01 +03:00
|
|
|
import itertools
|
2015-09-04 02:33:15 +03:00
|
|
|
|
2015-09-01 00:36:26 +03:00
|
|
|
import six
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
from .errors import Error
|
|
|
|
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-09-04 02:33:15 +03:00
|
|
|
GLOBAL_LOCK = threading.RLock()
|
|
|
|
|
|
|
|
|
2015-03-11 16:18:42 +03:00
|
|
|
def is_provider(instance):
|
|
|
|
"""Check if instance is provider instance."""
|
2015-09-01 00:36:26 +03:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-07-22 10:53:16 +03:00
|
|
|
getattr(instance, '__IS_PROVIDER__', False) is True)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
def ensure_is_provider(instance):
|
2015-10-19 12:12:38 +03:00
|
|
|
"""Check if instance is provider instance and return it.
|
|
|
|
|
|
|
|
:raise: Error if provided instance is not provider.
|
|
|
|
"""
|
2015-03-13 18:46:03 +03:00
|
|
|
if not is_provider(instance):
|
|
|
|
raise Error('Expected provider instance, '
|
2015-03-18 13:48:19 +03:00
|
|
|
'got {0}'.format(str(instance)))
|
2015-03-13 18:46:03 +03:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2015-03-11 16:18:42 +03:00
|
|
|
def is_injection(instance):
|
|
|
|
"""Check if instance is injection instance."""
|
2015-09-01 00:36:26 +03:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-07-22 10:53:16 +03:00
|
|
|
getattr(instance, '__IS_INJECTION__', False) is True)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
def ensure_is_injection(instance):
|
|
|
|
"""Check if instance is injection instance, otherwise raise and error."""
|
|
|
|
if not is_injection(instance):
|
|
|
|
raise Error('Expected injection instance, '
|
|
|
|
'got {0}'.format(str(instance)))
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2015-10-14 14:30:01 +03:00
|
|
|
def is_arg_injection(instance):
|
|
|
|
"""Check if instance is positional argument injection instance."""
|
|
|
|
return (not isinstance(instance, six.class_types) and
|
|
|
|
getattr(instance, '__IS_ARG_INJECTION__', False) is True)
|
|
|
|
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
def is_kwarg_injection(instance):
|
|
|
|
"""Check if instance is keyword argument injection instance."""
|
2015-09-01 00:36:26 +03:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-07-22 10:53:16 +03:00
|
|
|
getattr(instance, '__IS_KWARG_INJECTION__', False) is True)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_attribute_injection(instance):
|
|
|
|
"""Check if instance is attribute injection instance."""
|
2015-09-01 00:36:26 +03:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-07-22 10:53:16 +03:00
|
|
|
getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_method_injection(instance):
|
|
|
|
"""Check if instance is method injection instance."""
|
2015-09-01 00:36:26 +03:00
|
|
|
return (not isinstance(instance, six.class_types) and
|
2015-07-22 10:53:16 +03:00
|
|
|
getattr(instance, '__IS_METHOD_INJECTION__', False) is True)
|
2015-07-27 01:44:20 +03:00
|
|
|
|
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
def is_catalog(instance):
|
|
|
|
"""Check if instance is catalog instance."""
|
|
|
|
return (isinstance(instance, six.class_types) and
|
|
|
|
getattr(instance, '__IS_CATALOG__', False) is True)
|
|
|
|
|
|
|
|
|
2015-10-19 12:12:38 +03:00
|
|
|
def is_catalog_bundle(instance):
|
|
|
|
"""Check if instance is catalog bundle instance."""
|
|
|
|
return (not isinstance(instance, six.class_types) and
|
|
|
|
getattr(instance, '__IS_CATALOG_BUNDLE__', False) is True)
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_is_catalog_bundle(instance):
|
|
|
|
"""Check if instance is catalog bundle instance and return it.
|
|
|
|
|
|
|
|
:raise: Error if provided instance is not catalog bundle.
|
|
|
|
"""
|
|
|
|
if not is_catalog_bundle(instance):
|
|
|
|
raise Error('Expected catalog bundle instance, '
|
|
|
|
'got {0}'.format(str(instance)))
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2015-10-14 14:30:01 +03:00
|
|
|
def get_injectable_args(context_args, arg_injections):
|
|
|
|
"""Return tuple of positional args, patched with injections."""
|
|
|
|
return itertools.chain((arg.value for arg in arg_injections), context_args)
|
|
|
|
|
|
|
|
|
|
|
|
def get_injectable_kwargs(context_kwargs, kwarg_injections):
|
|
|
|
"""Return dictionary of keyword args, patched with injections."""
|
|
|
|
kwargs = dict((kwarg.name, kwarg.value)
|
|
|
|
for kwarg in kwarg_injections)
|
|
|
|
kwargs.update(context_kwargs)
|
|
|
|
return kwargs
|