2015-01-10 12:24:25 +03:00
|
|
|
"""
|
|
|
|
Injections module.
|
|
|
|
"""
|
|
|
|
|
2015-01-11 16:03:45 +03:00
|
|
|
from inspect import isclass
|
|
|
|
from functools import wraps
|
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
|
|
|
class Injection(object):
|
|
|
|
"""
|
|
|
|
Base injection class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, injectable):
|
|
|
|
"""
|
|
|
|
Initializer.
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.injectable = injectable
|
|
|
|
|
2015-01-11 16:03:45 +03:00
|
|
|
@property
|
|
|
|
def value(self):
|
2015-01-10 12:24:25 +03:00
|
|
|
"""
|
2015-01-11 16:03:45 +03:00
|
|
|
Returns injectable value.
|
2015-01-10 12:24:25 +03:00
|
|
|
"""
|
2015-01-11 16:03:45 +03:00
|
|
|
if hasattr(self.injectable, '__is_objects_provider__'):
|
|
|
|
return self.injectable()
|
|
|
|
return self.injectable
|
2015-01-10 12:24:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
class InitArg(Injection):
|
|
|
|
"""
|
|
|
|
Init argument injection.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Attribute(Injection):
|
|
|
|
"""
|
|
|
|
Attribute injection.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Method(Injection):
|
|
|
|
"""
|
|
|
|
Method injection.
|
|
|
|
"""
|
2015-01-11 16:03:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def inject(injection):
|
|
|
|
"""
|
|
|
|
Injection decorator.
|
|
|
|
"""
|
|
|
|
def decorator(callback_or_cls):
|
|
|
|
if isclass(callback_or_cls):
|
|
|
|
cls = callback_or_cls
|
|
|
|
if isinstance(injection, Attribute):
|
|
|
|
setattr(cls, injection.name, injection.injectable)
|
|
|
|
elif isinstance(injection, InitArg):
|
|
|
|
cls.__init__ = decorator(cls.__init__)
|
|
|
|
return cls
|
|
|
|
else:
|
|
|
|
callback = callback_or_cls
|
|
|
|
|
|
|
|
@wraps(callback)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
if injection.name not in kwargs:
|
|
|
|
kwargs[injection.name] = injection.value
|
|
|
|
return callback(*args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
return decorator
|