2020-09-10 05:23:28 +03:00
|
|
|
"""Providers module."""
|
2017-03-25 23:38:48 +03:00
|
|
|
|
2021-01-11 03:26:15 +03:00
|
|
|
try:
|
|
|
|
import asyncio
|
|
|
|
except ImportError:
|
|
|
|
asyncio = None
|
|
|
|
|
2021-01-21 18:51:37 +03:00
|
|
|
import functools
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
cimport cython
|
|
|
|
|
|
|
|
|
|
|
|
# Base providers
|
|
|
|
cdef class Provider(object):
|
|
|
|
cdef tuple __overridden
|
2017-04-06 12:34:04 +03:00
|
|
|
cdef Provider __last_overriding
|
2021-03-21 04:41:39 +03:00
|
|
|
cdef tuple __overrides
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef int __async_mode
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
2018-07-24 23:09:56 +03:00
|
|
|
cpdef void _copy_overridings(self, Provider copied, dict memo)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Object(Provider):
|
|
|
|
cdef object __provides
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2021-02-07 22:13:23 +03:00
|
|
|
cdef class Self(Provider):
|
|
|
|
cdef object __container
|
|
|
|
cdef tuple __alt_names
|
|
|
|
|
|
|
|
|
2019-10-09 17:45:14 +03:00
|
|
|
cdef class Delegate(Provider):
|
|
|
|
cdef object __provides
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
2017-12-21 23:47:31 +03:00
|
|
|
cdef class Dependency(Provider):
|
2020-07-22 19:14:26 +03:00
|
|
|
cdef object __instance_of
|
2021-01-29 21:49:40 +03:00
|
|
|
cdef object __default
|
2021-02-09 16:57:31 +03:00
|
|
|
cdef object __parent
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
2017-12-21 23:47:31 +03:00
|
|
|
cdef class ExternalDependency(Dependency):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-22 00:55:32 +03:00
|
|
|
cdef class DependenciesContainer(Object):
|
|
|
|
cdef dict __providers
|
2021-02-10 15:57:04 +03:00
|
|
|
cdef object __parent
|
2018-01-22 00:55:32 +03:00
|
|
|
|
|
|
|
cpdef object _override_providers(self, object container)
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
# Callable providers
|
|
|
|
cdef class Callable(Provider):
|
|
|
|
cdef object __provides
|
|
|
|
|
|
|
|
cdef tuple __args
|
|
|
|
cdef int __args_len
|
|
|
|
|
|
|
|
cdef tuple __kwargs
|
|
|
|
cdef int __kwargs_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class DelegatedCallable(Callable):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-04-06 18:17:06 +03:00
|
|
|
cdef class AbstractCallable(Callable):
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2017-07-09 23:29:33 +03:00
|
|
|
cdef class CallableDelegate(Delegate):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-10-18 19:39:19 +03:00
|
|
|
# Coroutine providers
|
|
|
|
cdef class Coroutine(Callable):
|
|
|
|
pass
|
|
|
|
|
2020-06-15 00:32:12 +03:00
|
|
|
|
2018-10-18 19:39:19 +03:00
|
|
|
cdef class DelegatedCoroutine(Coroutine):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
cdef class AbstractCoroutine(Coroutine):
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class CoroutineDelegate(Delegate):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
# Configuration providers
|
2020-08-04 01:01:20 +03:00
|
|
|
cdef class ConfigurationOption(Provider):
|
|
|
|
cdef tuple __name
|
2021-01-28 16:40:43 +03:00
|
|
|
cdef Configuration __root
|
2020-08-04 01:01:20 +03:00
|
|
|
cdef dict __children
|
2021-01-16 16:53:40 +03:00
|
|
|
cdef bint __required
|
2020-08-04 01:01:20 +03:00
|
|
|
cdef object __cache
|
|
|
|
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
cdef class TypedConfigurationOption(Callable):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-24 20:58:03 +03:00
|
|
|
cdef class Configuration(Object):
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef str __name
|
2021-01-16 16:53:40 +03:00
|
|
|
cdef bint __strict
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef dict __children
|
2020-08-04 01:01:20 +03:00
|
|
|
cdef object __weakref__
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Factory providers
|
|
|
|
cdef class Factory(Provider):
|
|
|
|
cdef Callable __instantiator
|
|
|
|
|
|
|
|
cdef tuple __attributes
|
|
|
|
cdef int __attributes_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class DelegatedFactory(Factory):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-04-06 18:17:06 +03:00
|
|
|
cdef class AbstractFactory(Factory):
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2017-07-09 23:13:10 +03:00
|
|
|
cdef class FactoryDelegate(Delegate):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-10-04 01:28:31 +03:00
|
|
|
cdef class FactoryAggregate(Provider):
|
|
|
|
cdef dict __factories
|
|
|
|
|
2017-10-13 05:43:22 +03:00
|
|
|
cdef Factory __get_factory(self, str factory_name)
|
2017-10-04 01:28:31 +03:00
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
# Singleton providers
|
|
|
|
cdef class BaseSingleton(Provider):
|
|
|
|
cdef Factory __instantiator
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef object __storage
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Singleton(BaseSingleton):
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class DelegatedSingleton(Singleton):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
cdef class ThreadSafeSingleton(BaseSingleton):
|
2017-04-18 23:01:20 +03:00
|
|
|
cdef object __storage_lock
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
cdef class ThreadLocalSingleton(BaseSingleton):
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-04-06 18:17:06 +03:00
|
|
|
cdef class AbstractSingleton(BaseSingleton):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-07-09 23:34:14 +03:00
|
|
|
cdef class SingletonDelegate(Delegate):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-06-15 00:32:12 +03:00
|
|
|
# Miscellaneous providers
|
|
|
|
|
|
|
|
cdef class List(Provider):
|
|
|
|
cdef tuple __args
|
|
|
|
cdef int __args_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2020-10-22 21:49:39 +03:00
|
|
|
cdef class Dict(Provider):
|
|
|
|
cdef tuple __kwargs
|
|
|
|
cdef int __kwargs_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2020-10-25 03:56:32 +03:00
|
|
|
cdef class Resource(Provider):
|
2021-03-20 20:16:51 +03:00
|
|
|
cdef object __provides
|
2020-10-25 03:56:32 +03:00
|
|
|
cdef bint __initialized
|
|
|
|
cdef object __shutdowner
|
|
|
|
cdef object __resource
|
|
|
|
|
|
|
|
cdef tuple __args
|
|
|
|
cdef int __args_len
|
|
|
|
|
|
|
|
cdef tuple __kwargs
|
|
|
|
cdef int __kwargs_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2020-06-23 05:45:16 +03:00
|
|
|
cdef class Container(Provider):
|
2020-10-09 22:16:27 +03:00
|
|
|
cdef object __container_cls
|
|
|
|
cdef dict __overriding_providers
|
|
|
|
cdef object __container
|
2021-02-10 16:52:13 +03:00
|
|
|
cdef object __parent
|
2020-06-23 05:45:16 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
2020-06-29 23:32:12 +03:00
|
|
|
cdef class Selector(Provider):
|
|
|
|
cdef object __selector
|
|
|
|
cdef dict __providers
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
2020-08-21 04:52:12 +03:00
|
|
|
# Provided instance
|
|
|
|
|
|
|
|
cdef class ProvidedInstance(Provider):
|
2021-03-20 20:16:51 +03:00
|
|
|
cdef object __provides
|
2020-08-21 04:52:12 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class AttributeGetter(Provider):
|
2021-03-20 20:16:51 +03:00
|
|
|
cdef object __provides
|
|
|
|
cdef object __name
|
2020-08-21 04:52:12 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class ItemGetter(Provider):
|
2021-03-20 20:16:51 +03:00
|
|
|
cdef object __provides
|
|
|
|
cdef object __name
|
2020-08-21 04:52:12 +03:00
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class MethodCaller(Provider):
|
2021-03-20 20:16:51 +03:00
|
|
|
cdef object __provides
|
2020-08-21 04:52:12 +03:00
|
|
|
cdef tuple __args
|
|
|
|
cdef int __args_len
|
|
|
|
cdef tuple __kwargs
|
|
|
|
cdef int __kwargs_len
|
|
|
|
|
|
|
|
cpdef object _provide(self, tuple args, dict kwargs)
|
|
|
|
|
2020-06-29 23:32:12 +03:00
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
# Injections
|
|
|
|
cdef class Injection(object):
|
|
|
|
cdef object __value
|
|
|
|
cdef int __is_provider
|
|
|
|
cdef int __is_delegated
|
|
|
|
cdef int __call
|
|
|
|
|
|
|
|
|
|
|
|
cdef class PositionalInjection(Injection):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
cdef class NamedInjection(Injection):
|
|
|
|
cdef object __name
|
|
|
|
|
|
|
|
|
|
|
|
cpdef tuple parse_positional_injections(tuple args)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef tuple parse_named_injections(dict kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
# Utils
|
2021-03-03 16:28:10 +03:00
|
|
|
cdef class OverridingContext(object):
|
|
|
|
cdef Provider __overridden
|
|
|
|
cdef Provider __overriding
|
|
|
|
|
|
|
|
|
|
|
|
cdef class BaseSingletonResetContext(object):
|
|
|
|
cdef object __singleton
|
|
|
|
|
|
|
|
|
|
|
|
cdef class SingletonResetContext(BaseSingletonResetContext):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
cdef class SingletonFullResetContext(BaseSingletonResetContext):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef object CLASS_TYPES
|
|
|
|
|
|
|
|
|
|
|
|
cpdef bint is_provider(object instance)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef object ensure_is_provider(object instance)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef bint is_delegated(object instance)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef str represent_provider(object provider, object provides)
|
|
|
|
|
|
|
|
|
2021-02-09 16:57:31 +03:00
|
|
|
cpdef bint is_container_instance(object instance)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef bint is_container_class(object instance)
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
cpdef object deepcopy(object instance, dict memo=*)
|
|
|
|
|
|
|
|
|
|
|
|
# Inline helper functions
|
|
|
|
cdef inline object __get_name(NamedInjection self):
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
|
|
|
|
cdef inline object __get_value(Injection self):
|
|
|
|
if self.__call == 0:
|
|
|
|
return self.__value
|
|
|
|
return self.__value()
|
|
|
|
|
|
|
|
|
2020-08-06 23:33:06 +03:00
|
|
|
cdef inline object __get_value_kwargs(Injection self, dict kwargs):
|
|
|
|
if self.__call == 0:
|
|
|
|
return self.__value
|
|
|
|
return self.__value(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
cdef inline tuple __separate_prefixed_kwargs(dict kwargs):
|
|
|
|
cdef dict plain_kwargs = {}
|
|
|
|
cdef dict prefixed_kwargs = {}
|
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
if '__' not in key:
|
|
|
|
plain_kwargs[key] = value
|
|
|
|
continue
|
|
|
|
|
|
|
|
index = key.index('__')
|
|
|
|
prefix, name = key[:index], key[index+2:]
|
|
|
|
|
|
|
|
if prefix not in prefixed_kwargs:
|
|
|
|
prefixed_kwargs[prefix] = {}
|
|
|
|
prefixed_kwargs[prefix][name] = value
|
|
|
|
|
|
|
|
return plain_kwargs, prefixed_kwargs
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
@cython.boundscheck(False)
|
|
|
|
@cython.wraparound(False)
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef inline object __provide_positional_args(
|
2020-08-06 23:33:06 +03:00
|
|
|
tuple args,
|
|
|
|
tuple inj_args,
|
|
|
|
int inj_args_len,
|
|
|
|
):
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef int index
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef list positional_args = []
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef list future_args = []
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef PositionalInjection injection
|
|
|
|
|
|
|
|
if inj_args_len == 0:
|
|
|
|
return args
|
|
|
|
|
|
|
|
for index in range(inj_args_len):
|
|
|
|
injection = <PositionalInjection>inj_args[index]
|
2021-01-11 03:26:15 +03:00
|
|
|
value = __get_value(injection)
|
|
|
|
positional_args.append(value)
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if __is_future_or_coroutine(value):
|
|
|
|
future_args.append((index, value))
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
positional_args.extend(args)
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if future_args:
|
|
|
|
return __combine_future_injections(positional_args, future_args)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
return positional_args
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cython.boundscheck(False)
|
|
|
|
@cython.wraparound(False)
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef inline object __provide_keyword_args(
|
2020-08-06 23:33:06 +03:00
|
|
|
dict kwargs,
|
|
|
|
tuple inj_kwargs,
|
|
|
|
int inj_kwargs_len,
|
|
|
|
):
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef int index
|
|
|
|
cdef object name
|
2020-08-06 23:33:06 +03:00
|
|
|
cdef object value
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef dict prefixed = {}
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef list future_kwargs = []
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef NamedInjection kw_injection
|
|
|
|
|
|
|
|
if len(kwargs) == 0:
|
|
|
|
for index in range(inj_kwargs_len):
|
|
|
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
|
|
|
name = __get_name(kw_injection)
|
2021-01-11 03:26:15 +03:00
|
|
|
value = __get_value(kw_injection)
|
|
|
|
kwargs[name] = value
|
2021-02-17 17:56:39 +03:00
|
|
|
if __is_future_or_coroutine(value):
|
|
|
|
future_kwargs.append((name, value))
|
2017-03-25 23:38:48 +03:00
|
|
|
else:
|
2020-08-06 23:33:06 +03:00
|
|
|
kwargs, prefixed = __separate_prefixed_kwargs(kwargs)
|
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
for index in range(inj_kwargs_len):
|
|
|
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
|
|
|
name = __get_name(kw_injection)
|
2020-08-06 23:33:06 +03:00
|
|
|
|
|
|
|
if name in kwargs:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if name in prefixed:
|
|
|
|
value = __get_value_kwargs(kw_injection, prefixed[name])
|
|
|
|
else:
|
|
|
|
value = __get_value(kw_injection)
|
|
|
|
|
|
|
|
kwargs[name] = value
|
2021-02-17 17:56:39 +03:00
|
|
|
if __is_future_or_coroutine(value):
|
|
|
|
future_kwargs.append((name, value))
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if future_kwargs:
|
|
|
|
return __combine_future_injections(kwargs, future_kwargs)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef inline object __combine_future_injections(object injections, list future_injections):
|
2021-01-11 03:26:15 +03:00
|
|
|
future_result = asyncio.Future()
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
injections_ready = asyncio.gather(*[value for _, value in future_injections])
|
|
|
|
injections_ready.add_done_callback(
|
2021-01-21 19:20:21 +03:00
|
|
|
functools.partial(
|
|
|
|
__async_prepare_args_kwargs_callback,
|
|
|
|
future_result,
|
2021-02-17 17:56:39 +03:00
|
|
|
injections,
|
|
|
|
future_injections,
|
2021-01-21 19:20:21 +03:00
|
|
|
),
|
|
|
|
)
|
2021-02-17 17:56:39 +03:00
|
|
|
asyncio.ensure_future(injections_ready)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
return future_result
|
|
|
|
|
|
|
|
|
2021-01-21 19:20:21 +03:00
|
|
|
cdef inline void __async_prepare_args_kwargs_callback(
|
|
|
|
object future_result,
|
|
|
|
object args,
|
2021-02-17 17:56:39 +03:00
|
|
|
object future_args_kwargs,
|
2021-01-21 19:20:21 +03:00
|
|
|
object future,
|
|
|
|
):
|
2021-02-16 20:26:23 +03:00
|
|
|
try:
|
2021-02-17 17:56:39 +03:00
|
|
|
result = future.result()
|
|
|
|
for value, (key, _) in zip(result, future_args_kwargs):
|
2021-02-16 20:26:23 +03:00
|
|
|
args[key] = value
|
|
|
|
except Exception as exception:
|
|
|
|
future_result.set_exception(exception)
|
|
|
|
else:
|
|
|
|
future_result.set_result(args)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
@cython.boundscheck(False)
|
|
|
|
@cython.wraparound(False)
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef inline object __provide_attributes(tuple attributes, int attributes_len):
|
2017-03-25 23:38:48 +03:00
|
|
|
cdef NamedInjection attr_injection
|
2021-01-11 03:26:15 +03:00
|
|
|
cdef dict attribute_injections = {}
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef list future_attributes = []
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2017-03-25 23:38:48 +03:00
|
|
|
for index in range(attributes_len):
|
|
|
|
attr_injection = <NamedInjection>attributes[index]
|
2021-01-11 03:26:15 +03:00
|
|
|
name = __get_name(attr_injection)
|
|
|
|
value = __get_value(attr_injection)
|
|
|
|
attribute_injections[name] = value
|
2021-02-17 17:56:39 +03:00
|
|
|
if __is_future_or_coroutine(value):
|
|
|
|
future_attributes.append((name, value))
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if future_attributes:
|
|
|
|
return __combine_future_injections(attribute_injections, future_attributes)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
return attribute_injections
|
|
|
|
|
|
|
|
|
|
|
|
cdef inline object __async_inject_attributes(future_instance, future_attributes):
|
|
|
|
future_result = asyncio.Future()
|
|
|
|
|
2021-01-21 19:20:21 +03:00
|
|
|
attributes_ready = asyncio.gather(future_instance, future_attributes)
|
|
|
|
attributes_ready.add_done_callback(
|
|
|
|
functools.partial(
|
|
|
|
__async_inject_attributes_callback,
|
|
|
|
future_result,
|
|
|
|
),
|
|
|
|
)
|
2021-01-11 03:26:15 +03:00
|
|
|
asyncio.ensure_future(attributes_ready)
|
|
|
|
|
|
|
|
return future_result
|
|
|
|
|
2021-01-21 19:20:21 +03:00
|
|
|
|
|
|
|
cdef inline void __async_inject_attributes_callback(object future_result, object future):
|
2021-02-16 20:26:23 +03:00
|
|
|
try:
|
|
|
|
instance, attributes = future.result()
|
|
|
|
|
|
|
|
for name, value in attributes.items():
|
|
|
|
setattr(instance, name, value)
|
|
|
|
except Exception as exception:
|
|
|
|
future_result.set_exception(exception)
|
|
|
|
else:
|
|
|
|
future_result.set_result(instance)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef inline void __inject_attributes(object instance, dict attributes):
|
|
|
|
for name, value in attributes.items():
|
|
|
|
setattr(instance, name, value)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
2020-08-21 04:52:12 +03:00
|
|
|
cdef inline object __call(
|
|
|
|
object call,
|
|
|
|
tuple context_args,
|
|
|
|
tuple injection_args,
|
|
|
|
int injection_args_len,
|
2021-01-11 03:26:15 +03:00
|
|
|
dict context_kwargs,
|
2020-08-21 04:52:12 +03:00
|
|
|
tuple injection_kwargs,
|
|
|
|
int injection_kwargs_len,
|
|
|
|
):
|
2021-01-11 03:26:15 +03:00
|
|
|
args = __provide_positional_args(
|
2020-08-21 04:52:12 +03:00
|
|
|
context_args,
|
|
|
|
injection_args,
|
|
|
|
injection_args_len,
|
|
|
|
)
|
2021-01-11 03:26:15 +03:00
|
|
|
kwargs = __provide_keyword_args(
|
|
|
|
context_kwargs,
|
2020-08-21 04:52:12 +03:00
|
|
|
injection_kwargs,
|
|
|
|
injection_kwargs_len,
|
|
|
|
)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
is_future_args = __is_future_or_coroutine(args)
|
|
|
|
is_future_kwargs = __is_future_or_coroutine(kwargs)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if is_future_args or is_future_kwargs:
|
|
|
|
future_args = args if is_future_args else __future_result(args)
|
|
|
|
future_kwargs = kwargs if is_future_kwargs else __future_result(kwargs)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
future_result = asyncio.Future()
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
args_kwargs_ready = asyncio.gather(future_args, future_kwargs)
|
2021-01-21 19:20:21 +03:00
|
|
|
args_kwargs_ready.add_done_callback(
|
|
|
|
functools.partial(
|
|
|
|
__async_call_callback,
|
|
|
|
future_result,
|
|
|
|
call,
|
|
|
|
),
|
|
|
|
)
|
2021-01-11 03:26:15 +03:00
|
|
|
asyncio.ensure_future(args_kwargs_ready)
|
|
|
|
|
|
|
|
return future_result
|
|
|
|
|
|
|
|
return call(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2021-01-21 19:20:21 +03:00
|
|
|
cdef inline void __async_call_callback(object future_result, object call, object future):
|
2021-02-16 20:26:23 +03:00
|
|
|
try:
|
|
|
|
args, kwargs = future.result()
|
|
|
|
result = call(*args, **kwargs)
|
|
|
|
except Exception as exception:
|
|
|
|
future_result.set_exception(exception)
|
|
|
|
else:
|
2021-02-17 17:56:39 +03:00
|
|
|
if __is_future_or_coroutine(result):
|
2021-02-16 20:26:23 +03:00
|
|
|
result = asyncio.ensure_future(result)
|
|
|
|
result.add_done_callback(functools.partial(__async_result_callback, future_result))
|
|
|
|
return
|
|
|
|
future_result.set_result(result)
|
2020-08-21 04:52:12 +03:00
|
|
|
|
|
|
|
|
2021-01-21 18:51:37 +03:00
|
|
|
cdef inline object __async_result_callback(object future_result, object future):
|
2021-02-16 20:26:23 +03:00
|
|
|
try:
|
|
|
|
result = future.result()
|
|
|
|
except Exception as exception:
|
|
|
|
future_result.set_exception(exception)
|
|
|
|
else:
|
|
|
|
future_result.set_result(result)
|
2021-01-21 18:51:37 +03:00
|
|
|
|
|
|
|
|
2020-08-21 04:52:12 +03:00
|
|
|
cdef inline object __callable_call(Callable self, tuple args, dict kwargs):
|
|
|
|
return __call(
|
|
|
|
self.__provides,
|
|
|
|
args,
|
|
|
|
self.__args,
|
|
|
|
self.__args_len,
|
|
|
|
kwargs,
|
|
|
|
self.__kwargs,
|
|
|
|
self.__kwargs_len,
|
|
|
|
)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef inline object __factory_call(Factory self, tuple args, dict kwargs):
|
|
|
|
cdef object instance
|
|
|
|
|
|
|
|
instance = __callable_call(self.__instantiator, args, kwargs)
|
|
|
|
|
|
|
|
if self.__attributes_len > 0:
|
2021-01-11 03:26:15 +03:00
|
|
|
attributes = __provide_attributes(self.__attributes, self.__attributes_len)
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
is_future_instance = __is_future_or_coroutine(instance)
|
|
|
|
is_future_attributes = __is_future_or_coroutine(attributes)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
if is_future_instance or is_future_attributes:
|
|
|
|
future_instance = instance if is_future_instance else __future_result(instance)
|
|
|
|
future_attributes = attributes if is_future_attributes else __future_result(attributes)
|
|
|
|
return __async_inject_attributes(future_instance, future_attributes)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
__inject_attributes(instance, attributes)
|
2017-03-25 23:38:48 +03:00
|
|
|
|
|
|
|
return instance
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef inline bint __is_future_or_coroutine(object instance):
|
|
|
|
if asyncio is None:
|
|
|
|
return False
|
|
|
|
return asyncio.isfuture(instance) or asyncio.iscoroutine(instance)
|
2021-01-11 03:26:15 +03:00
|
|
|
|
|
|
|
|
2021-02-17 17:56:39 +03:00
|
|
|
cdef inline object __future_result(object instance):
|
|
|
|
future_result = asyncio.Future()
|
|
|
|
future_result.set_result(instance)
|
|
|
|
return future_result
|