mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-12 09:30:46 +03:00
Add injections extension
This commit is contained in:
parent
d594f8018a
commit
7559959f35
63
dependency_injector/injections.pxd
Normal file
63
dependency_injector/injections.pxd
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
"""Dependency injector injections.
|
||||||
|
|
||||||
|
Cython optimized code.
|
||||||
|
"""
|
||||||
|
|
||||||
|
cpdef tuple parse_positional_injections(tuple args)
|
||||||
|
cpdef tuple parse_named_injections(dict kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
cdef class Injection:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
cdef class PositionalInjection(Injection):
|
||||||
|
cdef object __value
|
||||||
|
cdef int __is_provider
|
||||||
|
cdef int __is_delegated
|
||||||
|
cdef int __call
|
||||||
|
|
||||||
|
cdef inline object get_value(self):
|
||||||
|
if self.__call == 0:
|
||||||
|
return self.__value
|
||||||
|
return self.__value()
|
||||||
|
|
||||||
|
|
||||||
|
cdef class NamedInjection(Injection):
|
||||||
|
cdef object __name
|
||||||
|
cdef object __value
|
||||||
|
cdef int __is_provider
|
||||||
|
cdef int __is_delegated
|
||||||
|
cdef int __call
|
||||||
|
|
||||||
|
cdef inline object get_name(self):
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
cdef inline object get_value(self):
|
||||||
|
if self.__call == 0:
|
||||||
|
return self.__value
|
||||||
|
return self.__value()
|
||||||
|
|
||||||
|
|
||||||
|
cdef inline tuple __provide_positional_args(tuple inj_args,
|
||||||
|
int inj_args_len,
|
||||||
|
tuple args):
|
||||||
|
cdef PositionalInjection injection
|
||||||
|
if inj_args_len > 0:
|
||||||
|
positional_args = list()
|
||||||
|
for index in range(inj_args_len):
|
||||||
|
injection = <PositionalInjection>inj_args[index]
|
||||||
|
positional_args.append(injection.get_value())
|
||||||
|
positional_args.extend(args)
|
||||||
|
args = positional_args
|
||||||
|
return args
|
||||||
|
|
||||||
|
cdef inline dict __provide_keyword_args(tuple inj_kwargs,
|
||||||
|
int inj_kwargs_len,
|
||||||
|
dict kwargs):
|
||||||
|
cdef NamedInjection kw_injection
|
||||||
|
if inj_kwargs_len > 0:
|
||||||
|
for index in range(inj_kwargs_len):
|
||||||
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||||
|
kwargs[kw_injection.get_name()] = kw_injection.get_value()
|
||||||
|
return kwargs
|
66
dependency_injector/injections.pyx
Normal file
66
dependency_injector/injections.pyx
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
"""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)
|
Loading…
Reference in New Issue
Block a user