python-dependency-injector/dependency_injector/injections.pyx

79 lines
2.0 KiB
Cython
Raw Normal View History

2016-10-31 12:31:33 +03:00
"""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()
2016-10-31 12:31:33 +03:00
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
2016-11-02 12:24:46 +03:00
def get_value(self):
"""Return injection value."""
return self.__get_value()
2016-10-31 12:31:33 +03:00
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()
2016-10-31 12:31:33 +03:00
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
2016-11-02 12:24:46 +03:00
def get_name(self):
"""Return injection value."""
return self.__get_name()
def get_value(self):
"""Return injection value."""
return self.__get_value()
2016-10-31 12:31:33 +03:00
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)