Refactor inject decorator

This commit is contained in:
Roman Mogilatov 2016-03-01 12:25:54 +02:00
parent c8fda3da8d
commit 9f2a521888
1 changed files with 15 additions and 9 deletions

View File

@ -225,18 +225,11 @@ def inject(*args, **kwargs):
"""Dependency injection decorator."""
if isinstance(callback_or_cls, six.class_types):
cls = callback_or_cls
try:
cls_init = six.get_unbound_function(cls.__init__)
assert cls_init is not _OBJECT_INIT
except (AttributeError, AssertionError):
raise Error(
'Class {0}.{1} has no __init__() '.format(cls.__module__,
cls.__name__) +
'method and could not be decorated with @inject decorator')
cls.__init__ = decorator(cls_init)
cls.__init__ = decorator(_fetch_cls_init(cls))
return cls
callback = callback_or_cls
if hasattr(callback, 'injections'):
callback.args += arg_injections
callback.kwargs += kwarg_injections
@ -263,6 +256,19 @@ def inject(*args, **kwargs):
return decorator
def _fetch_cls_init(cls):
"""Return reference to the class.__init__() method if it is defined."""
try:
cls_init = six.get_unbound_function(cls.__init__)
assert cls_init is not _OBJECT_INIT
except (AttributeError, AssertionError):
raise Error(
'Class {0}.{1} has no __init__() '.format(cls.__module__,
cls.__name__) +
'method and could not be decorated with @inject decorator')
return cls_init
def _parse_args_injections(args):
"""Parse positional argument injections according to current syntax."""
return tuple(Arg(arg) if not is_injection(arg) else arg