mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-16 03:23:13 +03:00
Add is_delegated() method to utils
This commit is contained in:
parent
55cde4729d
commit
78cb4296cc
|
@ -3,8 +3,7 @@
|
||||||
Cython optimized code.
|
Cython optimized code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cpdef tuple parse_positional_injections(tuple args)
|
cimport cython
|
||||||
cpdef tuple parse_named_injections(dict kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
cdef class Injection:
|
cdef class Injection:
|
||||||
|
@ -39,25 +38,44 @@ cdef class NamedInjection(Injection):
|
||||||
return self.__value()
|
return self.__value()
|
||||||
|
|
||||||
|
|
||||||
|
@cython.boundscheck(False)
|
||||||
|
@cython.wraparound(False)
|
||||||
cdef inline tuple __provide_positional_args(tuple inj_args,
|
cdef inline tuple __provide_positional_args(tuple inj_args,
|
||||||
int inj_args_len,
|
int inj_args_len,
|
||||||
tuple args):
|
tuple args):
|
||||||
|
cdef int index
|
||||||
|
cdef list positional_args
|
||||||
cdef PositionalInjection injection
|
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
|
|
||||||
|
|
||||||
|
if inj_args_len == 0:
|
||||||
|
return args
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
return positional_args
|
||||||
|
|
||||||
|
|
||||||
|
@cython.boundscheck(False)
|
||||||
|
@cython.wraparound(False)
|
||||||
cdef inline dict __provide_keyword_args(tuple inj_kwargs,
|
cdef inline dict __provide_keyword_args(tuple inj_kwargs,
|
||||||
int inj_kwargs_len,
|
int inj_kwargs_len,
|
||||||
dict kwargs):
|
dict kwargs):
|
||||||
|
cdef int index
|
||||||
cdef NamedInjection kw_injection
|
cdef NamedInjection kw_injection
|
||||||
if inj_kwargs_len > 0:
|
|
||||||
for index in range(inj_kwargs_len):
|
if inj_kwargs_len == 0:
|
||||||
kw_injection = <NamedInjection>inj_kwargs[index]
|
return kwargs
|
||||||
kwargs[kw_injection.get_name()] = kw_injection.get_value()
|
|
||||||
|
for index in range(inj_kwargs_len):
|
||||||
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||||
|
kwargs[kw_injection.get_name()] = kw_injection.get_value()
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
|
cpdef tuple parse_positional_injections(tuple args)
|
||||||
|
cpdef tuple parse_named_injections(dict kwargs)
|
||||||
|
|
|
@ -3,8 +3,12 @@
|
||||||
Cython optimized code.
|
Cython optimized code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: replace to cimport
|
cimport cython
|
||||||
from .utils import is_provider
|
|
||||||
|
from .utils import (
|
||||||
|
is_provider,
|
||||||
|
is_delegated,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
cdef class Injection:
|
cdef class Injection:
|
||||||
|
@ -18,13 +22,17 @@ cdef class PositionalInjection(Injection):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.__value = value
|
self.__value = value
|
||||||
self.__is_provider = <int>is_provider(value)
|
self.__is_provider = <int>is_provider(value)
|
||||||
self.__is_delegated = 0 # TODO: use utils.is_delegated()
|
self.__is_delegated = <int>is_delegated(value)
|
||||||
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
"""Return injection value."""
|
"""Return injection value."""
|
||||||
return self.__get_value()
|
return self.__get_value()
|
||||||
|
|
||||||
|
def get_original_value(self):
|
||||||
|
"""Return original value."""
|
||||||
|
return self.__value
|
||||||
|
|
||||||
|
|
||||||
cdef class NamedInjection(Injection):
|
cdef class NamedInjection(Injection):
|
||||||
"""Keyword injection class."""
|
"""Keyword injection class."""
|
||||||
|
@ -34,7 +42,7 @@ cdef class NamedInjection(Injection):
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__value = value
|
self.__value = value
|
||||||
self.__is_provider = <int>is_provider(value)
|
self.__is_provider = <int>is_provider(value)
|
||||||
self.__is_delegated = 0 # TODO: use utils.is_delegated()
|
self.__is_delegated = <int>is_delegated(value)
|
||||||
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
|
@ -45,14 +53,20 @@ cdef class NamedInjection(Injection):
|
||||||
"""Return injection value."""
|
"""Return injection value."""
|
||||||
return self.__get_value()
|
return self.__get_value()
|
||||||
|
|
||||||
|
def get_original_value(self):
|
||||||
|
"""Return original value."""
|
||||||
|
return self.__value
|
||||||
|
|
||||||
|
|
||||||
|
@cython.boundscheck(False)
|
||||||
|
@cython.wraparound(False)
|
||||||
cpdef tuple parse_positional_injections(tuple args):
|
cpdef tuple parse_positional_injections(tuple args):
|
||||||
"""Parse positional injections."""
|
"""Parse positional injections."""
|
||||||
cdef list injections = list()
|
cdef list injections = list()
|
||||||
cdef int args_len = len(args)
|
cdef int args_len = len(args)
|
||||||
|
|
||||||
cdef object arg
|
|
||||||
cdef int index
|
cdef int index
|
||||||
|
cdef object arg
|
||||||
cdef PositionalInjection injection
|
cdef PositionalInjection injection
|
||||||
|
|
||||||
for index in range(args_len):
|
for index in range(args_len):
|
||||||
|
@ -63,6 +77,8 @@ cpdef tuple parse_positional_injections(tuple args):
|
||||||
return tuple(injections)
|
return tuple(injections)
|
||||||
|
|
||||||
|
|
||||||
|
@cython.boundscheck(False)
|
||||||
|
@cython.wraparound(False)
|
||||||
cpdef tuple parse_named_injections(dict kwargs):
|
cpdef tuple parse_named_injections(dict kwargs):
|
||||||
"""Parse named injections."""
|
"""Parse named injections."""
|
||||||
cdef list injections = list()
|
cdef list injections = list()
|
||||||
|
|
|
@ -52,6 +52,19 @@ def ensure_is_provider(instance):
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
def is_delegated(instance):
|
||||||
|
"""Check if instance is delegated provider.
|
||||||
|
|
||||||
|
:param instance: Instance to be checked.
|
||||||
|
:type instance: object
|
||||||
|
|
||||||
|
:rtype: bool
|
||||||
|
"""
|
||||||
|
return (not isinstance(instance, six.class_types) and
|
||||||
|
hasattr(instance, '__IS_DELEGATED__') and
|
||||||
|
getattr(instance, '__IS_DELEGATED__') is True)
|
||||||
|
|
||||||
|
|
||||||
def is_container(instance):
|
def is_container(instance):
|
||||||
"""Check if instance is container instance.
|
"""Check if instance is container instance.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user