mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-23 01:56:58 +03:00
67 lines
1.6 KiB
Cython
67 lines
1.6 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
|
|
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
|
|
|
|
|
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
|
|
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
|
|
|
|
|
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)
|