2015-03-11 16:18:42 +03:00
|
|
|
"""Utils module."""
|
|
|
|
|
2015-03-18 13:57:12 +03:00
|
|
|
from six import class_types
|
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
|
|
|
|
|
|
|
def is_provider(instance):
|
|
|
|
"""Check if instance is provider instance."""
|
2015-03-18 13:57:12 +03:00
|
|
|
return (not isinstance(instance, class_types) and
|
2015-03-12 13:01:56 +03:00
|
|
|
hasattr(instance, '__IS_OBJECTS_PROVIDER__'))
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
def ensure_is_provider(instance):
|
|
|
|
"""Check if instance is provider instance, otherwise raise and error."""
|
|
|
|
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-03-18 13:57:12 +03:00
|
|
|
return (not isinstance(instance, class_types) and
|
2015-03-12 13:01:56 +03:00
|
|
|
hasattr(instance, '__IS_OBJECTS_INJECTION__'))
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def is_kwarg_injection(instance):
|
|
|
|
"""Check if instance is keyword argument injection instance."""
|
2015-03-18 13:57:12 +03:00
|
|
|
return (not isinstance(instance, class_types) and
|
2015-03-23 02:04:18 +03:00
|
|
|
hasattr(instance, '__IS_OBJECTS_KWARG_INJECTION__'))
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_attribute_injection(instance):
|
|
|
|
"""Check if instance is attribute injection instance."""
|
2015-03-18 13:57:12 +03:00
|
|
|
return (not isinstance(instance, class_types) and
|
2015-03-12 13:01:56 +03:00
|
|
|
hasattr(instance, '__IS_OBJECTS_ATTRIBUTE_INJECTION__'))
|
2015-03-11 16:18:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_method_injection(instance):
|
|
|
|
"""Check if instance is method injection instance."""
|
2015-03-18 13:57:12 +03:00
|
|
|
return (not isinstance(instance, class_types) and
|
2015-03-12 13:01:56 +03:00
|
|
|
hasattr(instance, '__IS_OBJECTS_METHOD_INJECTION__'))
|