python-dependency-injector/dependency_injector/injections.pyx
2016-11-02 12:49:48 +02:00

79 lines
2.0 KiB
Cython

"""Dependency injector injections.
Cython optimized code.
"""
# TODO: replace to cimport
from .utils import is_provider
cdef class Injection:
"""Abstract injection class."""
cdef class PositionalInjection(Injection):
"""Positional injection class."""
def __init__(self, value):
"""Initializer."""
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = 0 # TODO: use utils.is_delegated()
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
def get_value(self):
"""Return injection value."""
return self.__get_value()
cdef class NamedInjection(Injection):
"""Keyword injection class."""
def __init__(self, name, value):
"""Initializer."""
self.__name = name
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = 0 # TODO: use utils.is_delegated()
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
def get_name(self):
"""Return injection value."""
return self.__get_name()
def get_value(self):
"""Return injection value."""
return self.__get_value()
cpdef tuple parse_positional_injections(tuple args):
"""Parse positional injections."""
cdef list injections = list()
cdef int args_len = len(args)
cdef object arg
cdef int index
cdef PositionalInjection injection
for index in range(args_len):
arg = args[index]
injection = PositionalInjection(arg)
injections.append(injection)
return tuple(injections)
cpdef tuple parse_named_injections(dict kwargs):
"""Parse named injections."""
cdef list injections = list()
cdef object name
cdef object arg
cdef NamedInjection injection
for name, arg in kwargs.items():
injection = NamedInjection(name, arg)
injections.append(injection)
return tuple(injections)