2020-09-10 05:23:28 +03:00
|
|
|
"""Containers module."""
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2021-09-30 22:03:19 +03:00
|
|
|
import contextlib
|
2021-10-04 03:35:48 +03:00
|
|
|
import copy as copy_module
|
2021-03-20 20:16:51 +03:00
|
|
|
import json
|
2020-10-09 22:16:27 +03:00
|
|
|
import sys
|
2021-09-30 22:03:19 +03:00
|
|
|
import importlib
|
|
|
|
import inspect
|
2021-11-01 04:00:28 +03:00
|
|
|
import warnings
|
2020-10-09 22:16:27 +03:00
|
|
|
|
2021-01-11 03:26:15 +03:00
|
|
|
try:
|
|
|
|
import asyncio
|
|
|
|
except ImportError:
|
|
|
|
asyncio = None
|
|
|
|
|
2021-03-20 20:16:51 +03:00
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
yaml = None
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
import six
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
from . import providers, errors
|
2021-02-18 16:17:05 +03:00
|
|
|
from .providers cimport __is_future_or_coroutine
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
if sys.version_info[:2] >= (3, 6):
|
|
|
|
from .wiring import wire, unwire
|
|
|
|
else:
|
|
|
|
def wire(*args, **kwargs):
|
2022-01-17 04:32:42 +03:00
|
|
|
raise NotImplementedError("Wiring requires Python 3.6 or above")
|
2020-10-09 22:16:27 +03:00
|
|
|
|
|
|
|
def unwire(*args, **kwargs):
|
2022-01-17 04:32:42 +03:00
|
|
|
raise NotImplementedError("Wiring requires Python 3.6 or above")
|
2020-10-09 22:16:27 +03:00
|
|
|
|
2021-11-01 04:00:28 +03:00
|
|
|
if sys.version_info[:2] == (3, 5):
|
|
|
|
warnings.warn(
|
|
|
|
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. "
|
|
|
|
"This does not mean that there will be any immediate breaking changes, "
|
|
|
|
"but tests will no longer be executed on Python 3.5, and bugs will not be addressed.",
|
|
|
|
category=DeprecationWarning,
|
|
|
|
)
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
class WiringConfiguration:
|
|
|
|
"""Container wiring configuration."""
|
|
|
|
|
|
|
|
def __init__(self, modules=None, packages=None, from_package=None, auto_wire=True):
|
|
|
|
self.modules = [*modules] if modules else []
|
|
|
|
self.packages = [*packages] if packages else []
|
|
|
|
self.from_package = from_package
|
|
|
|
self.auto_wire = auto_wire
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo=None):
|
|
|
|
return self.__class__(self.modules, self.packages, self.from_package, self.auto_wire)
|
|
|
|
|
|
|
|
|
2021-02-03 23:33:16 +03:00
|
|
|
class Container(object):
|
|
|
|
"""Abstract container."""
|
|
|
|
|
|
|
|
|
|
|
|
class DynamicContainer(Container):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Dynamic inversion of control container.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
services = DynamicContainer()
|
|
|
|
services.auth = providers.Factory(AuthService)
|
|
|
|
services.users = providers.Factory(UsersService,
|
|
|
|
auth_service=services.auth)
|
|
|
|
|
|
|
|
.. py:attribute:: providers
|
|
|
|
|
|
|
|
Read-only dictionary of all providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
|
|
|
|
.. py:attribute:: overridden
|
|
|
|
|
|
|
|
Tuple of overriding containers.
|
|
|
|
|
|
|
|
:type: tuple[:py:class:`DynamicContainer`]
|
|
|
|
|
|
|
|
.. py:attribute:: provider_type
|
|
|
|
|
|
|
|
Type of providers that could be placed in container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
|
|
|
|
|
|
|
__IS_CONTAINER__ = True
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2021-02-01 17:42:21 +03:00
|
|
|
self.provider_type = providers.Provider
|
2020-10-09 22:16:27 +03:00
|
|
|
self.providers = {}
|
2017-03-26 16:40:41 +03:00
|
|
|
self.overridden = tuple()
|
2021-02-13 17:16:38 +03:00
|
|
|
self.parent = None
|
2020-10-09 22:16:27 +03:00
|
|
|
self.declarative_parent = None
|
2021-10-04 03:35:48 +03:00
|
|
|
self.wiring_config = WiringConfiguration()
|
2020-10-09 22:16:27 +03:00
|
|
|
self.wired_to_modules = []
|
|
|
|
self.wired_to_packages = []
|
2021-02-07 22:13:23 +03:00
|
|
|
self.__self__ = providers.Self(self)
|
2017-03-26 16:40:41 +03:00
|
|
|
super(DynamicContainer, self).__init__()
|
|
|
|
|
2020-06-23 05:45:16 +03:00
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
"""Create and return full copy of container."""
|
|
|
|
copied = memo.get(id(self))
|
|
|
|
if copied is not None:
|
|
|
|
return copied
|
|
|
|
|
|
|
|
copied = self.__class__()
|
2021-02-07 22:13:23 +03:00
|
|
|
memo[id(self)] = copied
|
|
|
|
|
|
|
|
copied.__self__ = providers.deepcopy(self.__self__, memo)
|
|
|
|
for name in copied.__self__.alt_names:
|
|
|
|
copied.set_provider(name, copied.__self__)
|
|
|
|
|
2021-03-21 04:41:39 +03:00
|
|
|
copied.provider_type = providers.Provider
|
|
|
|
copied.overridden = providers.deepcopy(self.overridden, memo)
|
2021-10-04 03:35:48 +03:00
|
|
|
copied.wiring_config = copy_module.deepcopy(self.wiring_config, memo)
|
2021-03-21 04:41:39 +03:00
|
|
|
copied.declarative_parent = self.declarative_parent
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
for name, provider in providers.deepcopy(self.providers, memo).items():
|
2021-02-07 22:13:23 +03:00
|
|
|
copied.set_provider(name, provider)
|
2020-06-23 19:47:48 +03:00
|
|
|
|
2021-02-13 17:16:38 +03:00
|
|
|
copied.parent = providers.deepcopy(self.parent, memo)
|
|
|
|
|
2020-06-23 05:45:16 +03:00
|
|
|
return copied
|
|
|
|
|
2021-08-05 23:52:08 +03:00
|
|
|
def __setattr__(self, name, value):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Set instance attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be added into providers
|
|
|
|
dictionary.
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param name: Attribute name
|
2021-08-05 23:52:08 +03:00
|
|
|
:type name: object
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param value: Attribute value
|
2017-03-26 16:40:41 +03:00
|
|
|
:type value: object
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2021-02-13 17:16:38 +03:00
|
|
|
if isinstance(value, providers.Provider) \
|
|
|
|
and not isinstance(value, providers.Self) \
|
2022-01-17 04:32:42 +03:00
|
|
|
and name != "parent":
|
2017-03-26 16:40:41 +03:00
|
|
|
_check_provider_type(self, value)
|
2021-02-13 17:16:38 +03:00
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
self.providers[name] = value
|
2021-02-13 17:16:38 +03:00
|
|
|
|
|
|
|
if isinstance(value, providers.CHILD_PROVIDERS):
|
|
|
|
value.assign_parent(self)
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
super(DynamicContainer, self).__setattr__(name, value)
|
|
|
|
|
2021-08-05 23:52:08 +03:00
|
|
|
def __delattr__(self, name):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Delete instance attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be deleted from providers
|
|
|
|
dictionary.
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param name: Attribute name
|
2021-08-05 23:52:08 +03:00
|
|
|
:type name: object
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if name in self.providers:
|
|
|
|
del self.providers[name]
|
|
|
|
super(DynamicContainer, self).__delattr__(name)
|
|
|
|
|
2021-01-15 15:20:37 +03:00
|
|
|
@property
|
|
|
|
def dependencies(self):
|
|
|
|
"""Return dependency providers dictionary.
|
|
|
|
|
|
|
|
Dependency providers can be both of :py:class:`dependency_injector.providers.Dependency` and
|
|
|
|
:py:class:`dependency_injector.providers.DependenciesContainer`.
|
|
|
|
|
|
|
|
:rtype:
|
|
|
|
dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
name: provider
|
|
|
|
for name, provider in self.providers.items()
|
2021-02-01 17:42:21 +03:00
|
|
|
if isinstance(provider, (providers.Dependency, providers.DependenciesContainer))
|
2021-01-15 15:20:37 +03:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
def traverse(self, types=None):
|
|
|
|
"""Return providers traversal generator."""
|
|
|
|
yield from providers.traverse(*self.providers.values(), types=types)
|
|
|
|
|
2017-12-25 00:03:13 +03:00
|
|
|
def set_providers(self, **providers):
|
|
|
|
"""Set container providers.
|
|
|
|
|
|
|
|
:param providers: Dictionary of providers
|
|
|
|
:type providers:
|
2021-08-05 23:52:08 +03:00
|
|
|
dict[object, :py:class:`dependency_injector.providers.Provider`]
|
2017-12-25 00:03:13 +03:00
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
for name, provider in six.iteritems(providers):
|
|
|
|
setattr(self, name, provider)
|
|
|
|
|
2021-02-07 22:13:23 +03:00
|
|
|
def set_provider(self, name, provider):
|
|
|
|
"""Set container provider.
|
|
|
|
|
|
|
|
:param name: Provider name
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:param provider: Provider
|
|
|
|
:type provider: :py:class:`dependency_injector.providers.Provider`
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
setattr(self, name, provider)
|
|
|
|
|
2017-03-26 22:38:26 +03:00
|
|
|
def override(self, object overriding):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Override current container by overriding container.
|
|
|
|
|
|
|
|
:param overriding: Overriding container.
|
|
|
|
:type overriding: :py:class:`DynamicContainer`
|
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if trying to
|
|
|
|
override container by itself
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if overriding is self:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("Container {0} could not be overridden "
|
|
|
|
"with itself".format(self))
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
self.overridden += (overriding,)
|
|
|
|
|
|
|
|
for name, provider in six.iteritems(overriding.providers):
|
|
|
|
try:
|
|
|
|
getattr(self, name).override(provider)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2017-12-25 00:03:13 +03:00
|
|
|
def override_providers(self, **overriding_providers):
|
|
|
|
"""Override container providers.
|
|
|
|
|
|
|
|
:param overriding_providers: Dictionary of providers
|
|
|
|
:type overriding_providers:
|
|
|
|
dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2021-10-07 04:36:41 +03:00
|
|
|
overridden_providers = []
|
2017-12-25 00:03:13 +03:00
|
|
|
for name, overriding_provider in six.iteritems(overriding_providers):
|
|
|
|
container_provider = getattr(self, name)
|
|
|
|
container_provider.override(overriding_provider)
|
2021-10-07 04:36:41 +03:00
|
|
|
overridden_providers.append(container_provider)
|
|
|
|
return ProvidersOverridingContext(self, overridden_providers)
|
2017-12-25 00:03:13 +03:00
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
def reset_last_overriding(self):
|
|
|
|
"""Reset last overriding provider for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if not self.overridden:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("Container {0} is not overridden".format(self))
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
self.overridden = self.overridden[:-1]
|
|
|
|
|
|
|
|
for provider in six.itervalues(self.providers):
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
def reset_override(self):
|
|
|
|
"""Reset all overridings for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
self.overridden = tuple()
|
|
|
|
|
|
|
|
for provider in six.itervalues(self.providers):
|
|
|
|
provider.reset_override()
|
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
def is_auto_wiring_enabled(self):
|
|
|
|
"""Check if auto wiring is needed."""
|
|
|
|
return self.wiring_config.auto_wire is True
|
|
|
|
|
2021-09-30 22:03:19 +03:00
|
|
|
def wire(self, modules=None, packages=None, from_package=None):
|
2020-10-09 22:16:27 +03:00
|
|
|
"""Wire container providers with provided packages and modules.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2021-10-04 03:35:48 +03:00
|
|
|
if modules is None and self.wiring_config.modules:
|
|
|
|
modules = self.wiring_config.modules
|
|
|
|
if packages is None and self.wiring_config.packages:
|
|
|
|
packages = self.wiring_config.packages
|
|
|
|
|
2021-09-30 22:03:19 +03:00
|
|
|
modules = [*modules] if modules else []
|
|
|
|
packages = [*packages] if packages else []
|
|
|
|
|
|
|
|
if _any_relative_string_imports_in(modules) or _any_relative_string_imports_in(packages):
|
|
|
|
if from_package is None:
|
2021-10-04 03:35:48 +03:00
|
|
|
if self.wiring_config.from_package is not None:
|
|
|
|
from_package = self.wiring_config.from_package
|
|
|
|
elif self.declarative_parent is not None \
|
|
|
|
and (self.wiring_config.modules or self.wiring_config.packages):
|
|
|
|
with contextlib.suppress(Exception):
|
|
|
|
from_package = _resolve_package_name_from_cls(self.declarative_parent)
|
|
|
|
else:
|
|
|
|
with contextlib.suppress(Exception):
|
|
|
|
from_package = _resolve_calling_package_name()
|
2021-09-30 22:03:19 +03:00
|
|
|
|
|
|
|
modules = _resolve_string_imports(modules, from_package)
|
|
|
|
packages = _resolve_string_imports(packages, from_package)
|
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
if not modules and not packages:
|
|
|
|
return
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
wire(
|
|
|
|
container=self,
|
|
|
|
modules=modules,
|
|
|
|
packages=packages,
|
|
|
|
)
|
|
|
|
|
|
|
|
if modules:
|
|
|
|
self.wired_to_modules.extend(modules)
|
|
|
|
if packages:
|
|
|
|
self.wired_to_packages.extend(packages)
|
|
|
|
|
|
|
|
def unwire(self):
|
|
|
|
"""Unwire container providers from previously wired packages and modules."""
|
|
|
|
unwire(
|
|
|
|
modules=self.wired_to_modules,
|
|
|
|
packages=self.wired_to_packages,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.wired_to_modules.clear()
|
|
|
|
self.wired_to_packages.clear()
|
|
|
|
|
2020-10-25 03:56:32 +03:00
|
|
|
def init_resources(self):
|
|
|
|
"""Initialize all container resources."""
|
2021-01-11 03:26:15 +03:00
|
|
|
futures = []
|
|
|
|
|
2021-02-01 17:54:36 +03:00
|
|
|
for provider in self.traverse(types=[providers.Resource]):
|
2021-01-11 03:26:15 +03:00
|
|
|
resource = provider.init()
|
|
|
|
|
2021-02-18 16:17:05 +03:00
|
|
|
if __is_future_or_coroutine(resource):
|
2021-01-11 03:26:15 +03:00
|
|
|
futures.append(resource)
|
|
|
|
|
|
|
|
if futures:
|
|
|
|
return asyncio.gather(*futures)
|
2020-10-25 03:56:32 +03:00
|
|
|
|
|
|
|
def shutdown_resources(self):
|
|
|
|
"""Shutdown all container resources."""
|
2021-07-24 23:32:44 +03:00
|
|
|
def _independent_resources(resources):
|
|
|
|
for resource in resources:
|
|
|
|
for other_resource in resources:
|
|
|
|
if not other_resource.initialized:
|
|
|
|
continue
|
|
|
|
if resource in other_resource.related:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield resource
|
2021-07-21 01:46:44 +03:00
|
|
|
|
|
|
|
async def _async_ordered_shutdown(resources):
|
2021-07-24 23:32:44 +03:00
|
|
|
while any(resource.initialized for resource in resources):
|
|
|
|
resources_to_shutdown = list(_independent_resources(resources))
|
2021-07-21 01:46:44 +03:00
|
|
|
if not resources_to_shutdown:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise RuntimeError("Unable to resolve resources shutdown order")
|
2021-07-21 01:46:44 +03:00
|
|
|
futures = []
|
|
|
|
for resource in resources_to_shutdown:
|
|
|
|
result = resource.shutdown()
|
|
|
|
if __is_future_or_coroutine(result):
|
|
|
|
futures.append(result)
|
|
|
|
await asyncio.gather(*futures)
|
|
|
|
|
|
|
|
def _sync_ordered_shutdown(resources):
|
2021-07-24 23:32:44 +03:00
|
|
|
while any(resource.initialized for resource in resources):
|
|
|
|
resources_to_shutdown = list(_independent_resources(resources))
|
2021-07-21 01:46:44 +03:00
|
|
|
if not resources_to_shutdown:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise RuntimeError("Unable to resolve resources shutdown order")
|
2021-07-21 01:46:44 +03:00
|
|
|
for resource in resources_to_shutdown:
|
|
|
|
resource.shutdown()
|
|
|
|
|
|
|
|
resources = list(self.traverse(types=[providers.Resource]))
|
2021-07-24 23:32:44 +03:00
|
|
|
if any(resource.is_async_mode_enabled() for resource in resources):
|
2021-07-21 01:46:44 +03:00
|
|
|
return _async_ordered_shutdown(resources)
|
|
|
|
else:
|
|
|
|
return _sync_ordered_shutdown(resources)
|
2020-10-09 22:16:27 +03:00
|
|
|
|
2021-10-24 04:46:50 +03:00
|
|
|
def load_config(self):
|
|
|
|
"""Load configuration."""
|
|
|
|
config: providers.Configuration
|
|
|
|
for config in self.traverse(types=[providers.Configuration]):
|
|
|
|
config.load()
|
|
|
|
|
2021-01-14 01:07:41 +03:00
|
|
|
def apply_container_providers_overridings(self):
|
2022-01-17 04:32:42 +03:00
|
|
|
"""Apply container providers overridings."""
|
2021-02-05 16:59:16 +03:00
|
|
|
for provider in self.traverse(types=[providers.Container]):
|
2021-01-14 01:07:41 +03:00
|
|
|
provider.apply_overridings()
|
|
|
|
|
2021-02-06 01:14:10 +03:00
|
|
|
def reset_singletons(self):
|
2021-02-06 02:17:44 +03:00
|
|
|
"""Reset container singletons."""
|
|
|
|
for provider in self.traverse(types=[providers.BaseSingleton]):
|
2021-02-06 01:14:10 +03:00
|
|
|
provider.reset()
|
2021-03-03 16:28:10 +03:00
|
|
|
return SingletonResetContext(self)
|
2021-02-06 01:14:10 +03:00
|
|
|
|
2021-02-15 02:47:15 +03:00
|
|
|
def check_dependencies(self):
|
|
|
|
"""Check if container dependencies are defined.
|
|
|
|
|
|
|
|
If any dependency is undefined, raises an error.
|
|
|
|
"""
|
|
|
|
undefined = [
|
|
|
|
dependency
|
|
|
|
for dependency in self.traverse(types=[providers.Dependency])
|
|
|
|
if not dependency.is_defined
|
|
|
|
]
|
|
|
|
|
|
|
|
if not undefined:
|
|
|
|
return
|
|
|
|
|
|
|
|
container_name = self.parent_name if self.parent_name else self.__class__.__name__
|
|
|
|
undefined_names = [
|
2022-01-17 04:32:42 +03:00
|
|
|
f"\"{dependency.parent_name if dependency.parent_name else dependency}\""
|
2021-02-15 02:47:15 +03:00
|
|
|
for dependency in undefined
|
|
|
|
]
|
|
|
|
raise errors.Error(
|
2022-01-17 04:32:42 +03:00
|
|
|
f"Container \"{container_name}\" has undefined dependencies: "
|
|
|
|
f"{', '.join(undefined_names)}",
|
2021-02-15 02:47:15 +03:00
|
|
|
)
|
|
|
|
|
2021-03-20 20:16:51 +03:00
|
|
|
def from_schema(self, schema):
|
|
|
|
"""Build container providers from schema."""
|
|
|
|
from .schema import build_schema
|
|
|
|
for name, provider in build_schema(schema).items():
|
|
|
|
self.set_provider(name, provider)
|
|
|
|
|
|
|
|
def from_yaml_schema(self, filepath, loader=None):
|
|
|
|
"""Build container providers from YAML schema.
|
|
|
|
|
|
|
|
You can specify type of loader as a second argument. By default, method
|
|
|
|
uses ``SafeLoader``.
|
|
|
|
"""
|
|
|
|
if yaml is None:
|
|
|
|
raise errors.Error(
|
2022-01-17 04:32:42 +03:00
|
|
|
"Unable to load yaml schema - PyYAML is not installed. "
|
|
|
|
"Install PyYAML or install Dependency Injector with yaml extras: "
|
|
|
|
"\"pip install dependency-injector[yaml]\""
|
2021-03-20 20:16:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if loader is None:
|
|
|
|
loader = yaml.SafeLoader
|
|
|
|
|
|
|
|
with open(filepath) as file:
|
|
|
|
schema = yaml.load(file, loader)
|
|
|
|
|
|
|
|
self.from_schema(schema)
|
|
|
|
|
|
|
|
def from_json_schema(self, filepath):
|
|
|
|
"""Build container providers from JSON schema."""
|
|
|
|
with open(filepath) as file:
|
|
|
|
schema = json.load(file)
|
|
|
|
self.from_schema(schema)
|
|
|
|
|
2021-02-13 17:16:38 +03:00
|
|
|
def resolve_provider_name(self, provider):
|
|
|
|
"""Try to resolve provider name."""
|
|
|
|
for provider_name, container_provider in self.providers.items():
|
|
|
|
if container_provider is provider:
|
|
|
|
return provider_name
|
|
|
|
else:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error(f"Can not resolve name for provider \"{provider}\"")
|
2021-02-13 17:16:38 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def parent_name(self):
|
|
|
|
"""Return parent name."""
|
|
|
|
if self.parent:
|
|
|
|
return self.parent.parent_name
|
|
|
|
|
|
|
|
if self.declarative_parent:
|
|
|
|
return self.declarative_parent.__name__
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def assign_parent(self, parent):
|
|
|
|
"""Assign parent."""
|
|
|
|
self.parent = parent
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
class DeclarativeContainerMetaClass(type):
|
|
|
|
"""Declarative inversion of control container meta class."""
|
|
|
|
|
2017-03-26 22:38:26 +03:00
|
|
|
def __new__(type mcs, str class_name, tuple bases, dict attributes):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Declarative container class factory."""
|
2021-02-07 22:13:23 +03:00
|
|
|
self = mcs.__fetch_self(attributes)
|
|
|
|
if self is None:
|
|
|
|
self = providers.Self()
|
|
|
|
|
2021-01-30 00:37:50 +03:00
|
|
|
containers = {
|
|
|
|
name: container
|
2021-01-13 01:49:48 +03:00
|
|
|
for name, container in six.iteritems(attributes)
|
|
|
|
if is_container(container)
|
2021-01-30 00:37:50 +03:00
|
|
|
}
|
2017-11-21 12:47:55 +03:00
|
|
|
|
2021-01-30 00:37:50 +03:00
|
|
|
cls_providers = {
|
|
|
|
name: provider
|
2021-01-13 01:49:48 +03:00
|
|
|
for name, provider in six.iteritems(attributes)
|
2021-02-07 22:13:23 +03:00
|
|
|
if isinstance(provider, providers.Provider) and not isinstance(provider, providers.Self)
|
2021-01-30 00:37:50 +03:00
|
|
|
}
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2021-01-30 00:37:50 +03:00
|
|
|
inherited_providers = {
|
|
|
|
name: provider
|
2021-01-13 01:49:48 +03:00
|
|
|
for base in bases
|
|
|
|
if is_container(base) and base is not DynamicContainer
|
|
|
|
for name, provider in six.iteritems(base.providers)
|
2021-01-30 00:37:50 +03:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
all_providers = {}
|
|
|
|
all_providers.update(inherited_providers)
|
|
|
|
all_providers.update(cls_providers)
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
wiring_config = attributes.get("wiring_config")
|
|
|
|
if wiring_config is None:
|
|
|
|
wiring_config = WiringConfiguration()
|
|
|
|
if wiring_config is not None and not isinstance(wiring_config, WiringConfiguration):
|
|
|
|
raise errors.Error(
|
|
|
|
"Wiring configuration should be an instance of WiringConfiguration, "
|
|
|
|
"instead got {0}".format(wiring_config)
|
|
|
|
)
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
attributes["containers"] = containers
|
|
|
|
attributes["inherited_providers"] = inherited_providers
|
|
|
|
attributes["cls_providers"] = cls_providers
|
|
|
|
attributes["providers"] = all_providers
|
|
|
|
attributes["wiring_config"] = wiring_config
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2017-03-26 22:38:26 +03:00
|
|
|
cls = <type>type.__new__(mcs, class_name, bases, attributes)
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2021-02-07 22:13:23 +03:00
|
|
|
self.set_container(cls)
|
|
|
|
cls.__self__ = self
|
2021-01-11 16:18:02 +03:00
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
_check_provider_type(cls, provider)
|
|
|
|
|
2021-02-13 17:16:38 +03:00
|
|
|
for provider in six.itervalues(cls.cls_providers):
|
|
|
|
if isinstance(provider, providers.CHILD_PROVIDERS):
|
|
|
|
provider.assign_parent(cls)
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
return cls
|
|
|
|
|
2021-08-06 00:01:53 +03:00
|
|
|
def __setattr__(cls, name, value):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Set class attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be added into providers
|
|
|
|
dictionary.
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param name: Attribute name
|
2021-08-06 00:01:53 +03:00
|
|
|
:type name: object
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param value: Attribute value
|
2017-03-26 16:40:41 +03:00
|
|
|
:type value: object
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2022-01-17 04:32:42 +03:00
|
|
|
if isinstance(value, providers.Provider) and name != "__self__":
|
2017-03-26 16:40:41 +03:00
|
|
|
_check_provider_type(cls, value)
|
2021-02-13 17:16:38 +03:00
|
|
|
|
|
|
|
if isinstance(value, providers.CHILD_PROVIDERS):
|
|
|
|
value.assign_parent(cls)
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
cls.providers[name] = value
|
|
|
|
cls.cls_providers[name] = value
|
|
|
|
super(DeclarativeContainerMetaClass, cls).__setattr__(name, value)
|
|
|
|
|
2021-08-06 00:01:53 +03:00
|
|
|
def __delattr__(cls, name):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Delete class attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be deleted from providers
|
|
|
|
dictionary.
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:param name: Attribute name
|
2021-08-06 00:01:53 +03:00
|
|
|
:type name: object
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if name in cls.providers and name in cls.cls_providers:
|
|
|
|
del cls.providers[name]
|
|
|
|
del cls.cls_providers[name]
|
|
|
|
super(DeclarativeContainerMetaClass, cls).__delattr__(name)
|
|
|
|
|
2021-01-15 15:20:37 +03:00
|
|
|
@property
|
|
|
|
def dependencies(cls):
|
|
|
|
"""Return dependency providers dictionary.
|
|
|
|
|
|
|
|
Dependency providers can be both of :py:class:`dependency_injector.providers.Dependency` and
|
|
|
|
:py:class:`dependency_injector.providers.DependenciesContainer`.
|
|
|
|
|
|
|
|
:rtype:
|
|
|
|
dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
name: provider
|
|
|
|
for name, provider in cls.providers.items()
|
2021-02-01 17:42:21 +03:00
|
|
|
if isinstance(provider, (providers.Dependency, providers.DependenciesContainer))
|
2021-01-15 15:20:37 +03:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
def traverse(cls, types=None):
|
|
|
|
"""Return providers traversal generator."""
|
|
|
|
yield from providers.traverse(*cls.providers.values(), types=types)
|
|
|
|
|
2021-02-13 17:16:38 +03:00
|
|
|
def resolve_provider_name(cls, provider):
|
|
|
|
"""Try to resolve provider name."""
|
|
|
|
for provider_name, container_provider in cls.providers.items():
|
|
|
|
if container_provider is provider:
|
|
|
|
return provider_name
|
|
|
|
else:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error(f"Can not resolve name for provider \"{provider}\"")
|
2021-02-13 17:16:38 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def parent_name(cls):
|
|
|
|
"""Return parent name."""
|
|
|
|
return cls.__name__
|
|
|
|
|
2021-02-07 22:13:23 +03:00
|
|
|
@staticmethod
|
|
|
|
def __fetch_self(attributes):
|
|
|
|
self = None
|
|
|
|
alt_names = []
|
|
|
|
|
|
|
|
for name, value in attributes.items():
|
|
|
|
if not isinstance(value, providers.Self):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self is not None and value is not self:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("Container can have only one \"Self\" provider")
|
2021-02-07 22:13:23 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
if name != "__self__":
|
2021-02-07 22:13:23 +03:00
|
|
|
alt_names.append(name)
|
|
|
|
|
|
|
|
self = value
|
|
|
|
|
|
|
|
if self:
|
|
|
|
self.set_alt_names(alt_names)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
@six.add_metaclass(DeclarativeContainerMetaClass)
|
2021-02-03 23:33:16 +03:00
|
|
|
class DeclarativeContainer(Container):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Declarative inversion of control container.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
class Services(DeclarativeContainer):
|
|
|
|
auth = providers.Factory(AuthService)
|
|
|
|
users = providers.Factory(UsersService,
|
|
|
|
auth_service=auth)
|
|
|
|
"""
|
|
|
|
|
|
|
|
__IS_CONTAINER__ = True
|
|
|
|
|
2021-02-01 17:42:21 +03:00
|
|
|
provider_type = providers.Provider
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Type of providers that could be placed in container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
|
|
|
|
|
|
|
instance_type = DynamicContainer
|
|
|
|
"""Type of container that is returned on instantiating declarative
|
|
|
|
container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
|
|
|
|
2017-11-21 12:47:55 +03:00
|
|
|
containers = dict()
|
|
|
|
"""Read-only dictionary of all nested containers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`DeclarativeContainer`]
|
|
|
|
"""
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
providers = dict()
|
|
|
|
"""Read-only dictionary of all providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
wiring_config = WiringConfiguration()
|
|
|
|
"""Wiring configuration.
|
|
|
|
|
|
|
|
:type: WiringConfiguration
|
|
|
|
"""
|
|
|
|
|
2021-10-24 04:46:50 +03:00
|
|
|
auto_load_config = True
|
|
|
|
"""Automatically load configuration when the container is created.
|
|
|
|
|
|
|
|
:type: bool
|
|
|
|
"""
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
cls_providers = dict()
|
|
|
|
"""Read-only dictionary of current container providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
|
|
|
inherited_providers = dict()
|
|
|
|
"""Read-only dictionary of inherited providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
|
|
|
overridden = tuple()
|
|
|
|
"""Tuple of overriding containers.
|
|
|
|
|
|
|
|
:type: tuple[:py:class:`DeclarativeContainer`]
|
|
|
|
"""
|
|
|
|
|
2021-01-11 16:18:02 +03:00
|
|
|
__self__ = None
|
|
|
|
"""Provider that provides current container.
|
|
|
|
|
|
|
|
:type: :py:class:`dependency_injector.providers.Provider`
|
|
|
|
"""
|
|
|
|
|
2017-12-25 00:03:13 +03:00
|
|
|
def __new__(cls, **overriding_providers):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
:return: Dynamic container with copy of all providers.
|
|
|
|
:rtype: :py:class:`DynamicContainer`
|
|
|
|
"""
|
2017-12-25 00:03:13 +03:00
|
|
|
container = cls.instance_type()
|
2017-03-26 16:40:41 +03:00
|
|
|
container.provider_type = cls.provider_type
|
2021-10-04 03:35:48 +03:00
|
|
|
container.wiring_config = copy_module.deepcopy(cls.wiring_config)
|
2020-10-09 22:16:27 +03:00
|
|
|
container.declarative_parent = cls
|
2021-02-07 22:13:23 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
copied_providers = providers.deepcopy({ **cls.providers, **{"@@self@@": cls.__self__}})
|
|
|
|
copied_self = copied_providers.pop("@@self@@")
|
2021-02-07 22:13:23 +03:00
|
|
|
copied_self.set_container(container)
|
|
|
|
|
|
|
|
container.__self__ = copied_self
|
|
|
|
for name in copied_self.alt_names:
|
|
|
|
container.set_provider(name, copied_self)
|
|
|
|
|
|
|
|
for name, provider in copied_providers.items():
|
|
|
|
container.set_provider(name, provider)
|
|
|
|
|
2021-10-24 04:46:50 +03:00
|
|
|
if cls.auto_load_config:
|
|
|
|
container.load_config()
|
|
|
|
|
2017-12-25 00:03:13 +03:00
|
|
|
container.override_providers(**overriding_providers)
|
2021-01-14 01:07:41 +03:00
|
|
|
container.apply_container_providers_overridings()
|
2021-02-07 22:13:23 +03:00
|
|
|
|
2021-10-04 03:35:48 +03:00
|
|
|
if container.is_auto_wiring_enabled():
|
|
|
|
container.wire()
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
return container
|
|
|
|
|
|
|
|
@classmethod
|
2017-03-26 22:38:26 +03:00
|
|
|
def override(cls, object overriding):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Override current container by overriding container.
|
|
|
|
|
|
|
|
:param overriding: Overriding container.
|
|
|
|
:type overriding: :py:class:`DeclarativeContainer`
|
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if trying to
|
|
|
|
override container by itself or its subclasses
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if issubclass(cls, overriding):
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("Container {0} could not be overridden "
|
|
|
|
"with itself or its subclasses".format(cls))
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
cls.overridden += (overriding,)
|
|
|
|
|
|
|
|
for name, provider in six.iteritems(overriding.cls_providers):
|
|
|
|
try:
|
|
|
|
getattr(cls, name).override(provider)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def reset_last_overriding(cls):
|
|
|
|
"""Reset last overriding provider for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if not cls.overridden:
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("Container {0} is not overridden".format(cls))
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
cls.overridden = cls.overridden[:-1]
|
|
|
|
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def reset_override(cls):
|
|
|
|
"""Reset all overridings for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
cls.overridden = tuple()
|
|
|
|
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_override()
|
|
|
|
|
|
|
|
|
2021-03-03 16:28:10 +03:00
|
|
|
class SingletonResetContext:
|
|
|
|
|
|
|
|
def __init__(self, container):
|
|
|
|
self._container = container
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self._container
|
|
|
|
|
|
|
|
def __exit__(self, *_):
|
|
|
|
self._container.reset_singletons()
|
|
|
|
|
|
|
|
|
2021-10-07 04:36:41 +03:00
|
|
|
|
|
|
|
class ProvidersOverridingContext:
|
|
|
|
|
|
|
|
def __init__(self, container, overridden_providers):
|
|
|
|
self._container = container
|
|
|
|
self._overridden_providers = overridden_providers
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self._container
|
|
|
|
|
|
|
|
def __exit__(self, *_):
|
|
|
|
for provider in self._overridden_providers:
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
|
2017-03-26 16:40:41 +03:00
|
|
|
def override(object container):
|
|
|
|
""":py:class:`DeclarativeContainer` overriding decorator.
|
|
|
|
|
|
|
|
:param container: Container that should be overridden by decorated
|
|
|
|
container.
|
|
|
|
:type container: :py:class:`DeclarativeContainer`
|
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:return: Declarative container overriding decorator.
|
2017-03-26 16:40:41 +03:00
|
|
|
:rtype: callable(:py:class:`DeclarativeContainer`)
|
|
|
|
"""
|
2017-03-26 22:38:26 +03:00
|
|
|
def _decorator(object overriding_container):
|
2017-03-26 16:40:41 +03:00
|
|
|
"""Overriding decorator."""
|
|
|
|
container.override(overriding_container)
|
|
|
|
return overriding_container
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
2021-08-12 04:18:37 +03:00
|
|
|
def copy(object base_container):
|
2017-03-26 16:40:41 +03:00
|
|
|
""":py:class:`DeclarativeContainer` copying decorator.
|
|
|
|
|
2021-02-18 16:25:22 +03:00
|
|
|
This decorator copies all providers from provided container to decorated one.
|
2017-03-26 16:40:41 +03:00
|
|
|
If one of the decorated container providers matches to source container
|
|
|
|
providers by name, it would be replaced by reference.
|
|
|
|
|
2021-08-12 04:18:37 +03:00
|
|
|
:param base_container: Container that should be copied by decorated container.
|
|
|
|
:type base_container: :py:class:`DeclarativeContainer`
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
:return: Declarative container copying decorator.
|
2017-03-26 16:40:41 +03:00
|
|
|
:rtype: callable(:py:class:`DeclarativeContainer`)
|
|
|
|
"""
|
2021-08-12 04:18:37 +03:00
|
|
|
def _get_memo_for_matching_names(new_providers, base_providers):
|
|
|
|
memo = {}
|
|
|
|
for new_provider_name, new_provider in six.iteritems(new_providers):
|
|
|
|
if new_provider_name not in base_providers:
|
2021-02-18 16:25:22 +03:00
|
|
|
continue
|
2021-08-12 04:18:37 +03:00
|
|
|
source_provider = base_providers[new_provider_name]
|
|
|
|
memo[id(source_provider)] = new_provider
|
2017-03-26 16:40:41 +03:00
|
|
|
|
2022-01-17 04:32:42 +03:00
|
|
|
if hasattr(new_provider, "providers") and hasattr(source_provider, "providers"):
|
2021-08-12 04:18:37 +03:00
|
|
|
sub_memo = _get_memo_for_matching_names(new_provider.providers, source_provider.providers)
|
|
|
|
memo.update(sub_memo)
|
2021-01-27 17:21:45 +03:00
|
|
|
return memo
|
|
|
|
|
2021-08-12 04:18:37 +03:00
|
|
|
def _decorator(new_container):
|
|
|
|
memo = {}
|
|
|
|
memo.update(_get_memo_for_matching_names(new_container.cls_providers, base_container.providers))
|
|
|
|
|
|
|
|
new_providers = {}
|
|
|
|
new_providers.update(providers.deepcopy(base_container.providers, memo))
|
|
|
|
new_providers.update(providers.deepcopy(new_container.cls_providers, memo))
|
2021-01-27 17:21:45 +03:00
|
|
|
|
2021-08-12 04:18:37 +03:00
|
|
|
for name, provider in six.iteritems(new_providers):
|
|
|
|
setattr(new_container, name, provider)
|
|
|
|
return new_container
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
|
|
cpdef bint is_container(object instance):
|
|
|
|
"""Check if instance is container instance.
|
|
|
|
|
|
|
|
:param instance: Instance to be checked.
|
|
|
|
:type instance: object
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2022-01-17 04:32:42 +03:00
|
|
|
return getattr(instance, "__IS_CONTAINER__", False) is True
|
2017-03-26 16:40:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
cpdef object _check_provider_type(object container, object provider):
|
|
|
|
if not isinstance(provider, container.provider_type):
|
2022-01-17 04:32:42 +03:00
|
|
|
raise errors.Error("{0} can contain only {1} "
|
|
|
|
"instances".format(container, container.provider_type))
|
2021-09-30 22:03:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
cpdef bint _any_relative_string_imports_in(object modules):
|
|
|
|
for module in modules:
|
|
|
|
if not isinstance(module, str):
|
|
|
|
continue
|
|
|
|
if module.startswith("."):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
cpdef list _resolve_string_imports(object modules, object from_package):
|
|
|
|
return [
|
|
|
|
importlib.import_module(module, from_package) if isinstance(module, str) else module
|
|
|
|
for module in modules
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
cpdef object _resolve_calling_package_name():
|
|
|
|
stack = inspect.stack()
|
|
|
|
pre_last_frame = stack[0]
|
|
|
|
module = inspect.getmodule(pre_last_frame[0])
|
|
|
|
return module.__package__
|
2021-10-04 03:35:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
cpdef object _resolve_package_name_from_cls(cls):
|
|
|
|
module = importlib.import_module(cls.__module__)
|
|
|
|
return module.__package__
|