Add set_provides for Callable, Factory, and Singleton providers

This commit is contained in:
Roman Mogylatov 2021-03-06 08:46:13 -05:00
parent b457f54299
commit b347efc62f
2 changed files with 6519 additions and 6150 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1022,18 +1022,10 @@ cdef class Callable(Provider):
some_function.add_kwargs(keyword_argument1=3, keyword_argument=4) some_function.add_kwargs(keyword_argument1=3, keyword_argument=4)
""" """
def __init__(self, provides, *args, **kwargs): def __init__(self, provides=None, *args, **kwargs):
"""Initializer. """Initialize provider."""
self.__provides = None
:param provides: Wrapped callable. self.set_provides(provides)
:type provides: callable
"""
if not callable(provides):
raise Error('Provider {0} expected to get callable, '
'got {0}'.format('.'.join((self.__class__.__module__,
self.__class__.__name__)),
provides))
self.__provides = provides
self.__args = tuple() self.__args = tuple()
self.__args_len = 0 self.__args_len = 0
@ -1072,9 +1064,18 @@ cdef class Callable(Provider):
@property @property
def provides(self): def provides(self):
"""Return wrapped callable.""" """Return provider's provides."""
return self.__provides return self.__provides
def set_provides(self, provides):
"""Set provider's provides."""
if provides and not callable(provides):
raise Error(
f'Provider {self.__class__.__qualname__} expected to get callable, '
f'got {provides} instead'
)
self.__provides = provides
@property @property
def args(self): def args(self):
"""Return positional argument injections.""" """Return positional argument injections."""
@ -2087,18 +2088,12 @@ cdef class Factory(Provider):
provided_type = None provided_type = None
def __init__(self, provides, *args, **kwargs): def __init__(self, provides=None, *args, **kwargs):
"""Initializer. """Initialize provider."""
self.__instantiator = Callable()
:param provides: Provided type. self.set_provides(provides)
:type provides: type self.set_args(*args)
""" self.set_kwargs(**kwargs)
if (self.__class__.provided_type and
not issubclass(provides, self.__class__.provided_type)):
raise Error('{0} can provide only {1} instances'.format(
self.__class__, self.__class__.provided_type))
self.__instantiator = Callable(provides, *args, **kwargs)
self.__attributes = tuple() self.__attributes = tuple()
self.__attributes_len = 0 self.__attributes_len = 0
@ -2139,9 +2134,20 @@ cdef class Factory(Provider):
@property @property
def provides(self): def provides(self):
"""Return provided type.""" """Return provider's provides."""
return self.__instantiator.provides return self.__instantiator.provides
def set_provides(self, provides):
"""Set provider's provides."""
if (provides
and self.__class__.provided_type and
not issubclass(provides, self.__class__.provided_type)):
raise Error(
f'{self.__class__.__qualname__} can provide only '
f'{self.__class__.provided_type} instances'
)
self.__instantiator.set_provides(provides)
@property @property
def args(self): def args(self):
"""Return positional argument injections.""" """Return positional argument injections."""
@ -2449,19 +2455,12 @@ cdef class BaseSingleton(Provider):
provided_type = None provided_type = None
def __init__(self, provides, *args, **kwargs): def __init__(self, provides=None, *args, **kwargs):
"""Initializer. """Initialize provider."""
self.__instantiator = Factory()
:param provides: Provided type. self.set_provides(provides)
:type provides: type self.set_args(*args)
""" self.set_kwargs(**kwargs)
if (self.__class__.provided_type and
not issubclass(provides, self.__class__.provided_type)):
raise Error('{0} can provide only {1} instances'.format(
self.__class__, self.__class__.provided_type))
self.__instantiator = Factory(provides, *args, **kwargs)
super(BaseSingleton, self).__init__() super(BaseSingleton, self).__init__()
def __str__(self): def __str__(self):
@ -2498,9 +2497,20 @@ cdef class BaseSingleton(Provider):
@property @property
def provides(self): def provides(self):
"""Return provided type.""" """Return provider's provides."""
return self.__instantiator.provides return self.__instantiator.provides
def set_provides(self, provides):
"""Set provider's provides."""
if (provides
and self.__class__.provided_type and
not issubclass(provides, self.__class__.provided_type)):
raise Error(
f'{self.__class__.__qualname__} can provide only '
f'{self.__class__.provided_type} instances'
)
self.__instantiator.set_provides(provides)
@property @property
def args(self): def args(self):
"""Return positional argument injections.""" """Return positional argument injections."""