2015-03-09 01:01:39 +03:00
|
|
|
"""Catalog module."""
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-09-01 00:36:26 +03:00
|
|
|
import six
|
2015-07-17 19:31:44 +03:00
|
|
|
|
2015-03-13 18:31:07 +03:00
|
|
|
from .errors import Error
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-07-17 19:31:44 +03:00
|
|
|
from .utils import is_provider
|
2015-10-07 13:36:28 +03:00
|
|
|
from .utils import is_catalog
|
2015-10-19 12:12:38 +03:00
|
|
|
from .utils import ensure_is_catalog_bundle
|
2015-07-17 19:31:44 +03:00
|
|
|
|
|
|
|
|
2015-11-04 17:32:04 +03:00
|
|
|
@six.python_2_unicode_compatible
|
2015-10-16 23:54:51 +03:00
|
|
|
class CatalogBundle(object):
|
|
|
|
"""Bundle of catalog providers."""
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-10-16 23:54:51 +03:00
|
|
|
catalog = None
|
2015-11-10 11:42:29 +03:00
|
|
|
""":type: DeclarativeCatalog"""
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-10-19 12:12:38 +03:00
|
|
|
__IS_CATALOG_BUNDLE__ = True
|
2015-10-16 23:54:51 +03:00
|
|
|
__slots__ = ('providers', '__dict__')
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-10-16 23:54:51 +03:00
|
|
|
def __init__(self, *providers):
|
2015-10-15 20:15:38 +03:00
|
|
|
"""Initializer."""
|
2015-10-16 23:54:51 +03:00
|
|
|
self.providers = dict((provider.bind.name, provider)
|
|
|
|
for provider in providers
|
|
|
|
if self._ensure_provider_is_bound(provider))
|
|
|
|
self.__dict__.update(self.providers)
|
|
|
|
super(CatalogBundle, self).__init__()
|
2015-10-15 20:15:38 +03:00
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
"""Return provider with specified name or raises error."""
|
|
|
|
try:
|
|
|
|
return self.providers[name]
|
|
|
|
except KeyError:
|
|
|
|
self._raise_undefined_provider_error(name)
|
|
|
|
|
|
|
|
def has(self, name):
|
|
|
|
"""Check if there is provider with certain name."""
|
|
|
|
return name in self.providers
|
|
|
|
|
|
|
|
def _ensure_provider_is_bound(self, provider):
|
2015-10-16 23:54:51 +03:00
|
|
|
"""Check that provider is bound to the bundle's catalog."""
|
|
|
|
if not provider.is_bound:
|
2015-10-15 20:15:38 +03:00
|
|
|
raise Error('Provider {0} is not bound to '
|
|
|
|
'any catalog'.format(provider))
|
|
|
|
if provider is not self.catalog.get(provider.bind.name):
|
|
|
|
raise Error('{0} can contain providers from '
|
2015-10-16 23:54:51 +03:00
|
|
|
'catalog {0}'.format(self.__class__, self.catalog))
|
|
|
|
return True
|
2015-10-15 20:15:38 +03:00
|
|
|
|
|
|
|
def _raise_undefined_provider_error(self, name):
|
2015-10-16 23:54:51 +03:00
|
|
|
"""Raise error for cases when there is no such provider in bundle."""
|
2015-10-15 20:15:38 +03:00
|
|
|
raise Error('Provider "{0}" is not a part of {1}'.format(name, self))
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
"""Raise an error on every attempt to get undefined provider."""
|
2015-10-19 10:47:24 +03:00
|
|
|
if item.startswith('__') and item.endswith('__'):
|
|
|
|
return super(CatalogBundle, self).__getattr__(item)
|
2015-10-15 20:15:38 +03:00
|
|
|
self._raise_undefined_provider_error(item)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-11-04 17:32:04 +03:00
|
|
|
"""Return string representation of catalog bundle."""
|
|
|
|
return '<{0}.{1}.Bundle({2})>'.format(
|
|
|
|
self.catalog.__module__, self.catalog.__name__,
|
|
|
|
', '.join(six.iterkeys(self.providers)))
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-11-04 17:32:04 +03:00
|
|
|
__str__ = __repr__
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-11-04 17:32:04 +03:00
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
class Catalog(object):
|
|
|
|
"""Catalog of providers."""
|
|
|
|
|
|
|
|
def __init__(self, name, **providers):
|
|
|
|
"""Initializer."""
|
|
|
|
self.name = name
|
|
|
|
self.providers = providers
|
|
|
|
|
|
|
|
|
2015-11-04 17:32:04 +03:00
|
|
|
@six.python_2_unicode_compatible
|
2015-11-10 11:42:29 +03:00
|
|
|
class DeclarativeCatalogMetaClass(type):
|
|
|
|
"""Declarative catalog meta class."""
|
2015-07-17 19:31:44 +03:00
|
|
|
|
|
|
|
def __new__(mcs, class_name, bases, attributes):
|
2015-11-10 11:42:29 +03:00
|
|
|
"""Declarative catalog class factory."""
|
2015-10-07 13:36:28 +03:00
|
|
|
cls_providers = dict((name, provider)
|
|
|
|
for name, provider in six.iteritems(attributes)
|
|
|
|
if is_provider(provider))
|
|
|
|
|
|
|
|
inherited_providers = dict((name, provider)
|
|
|
|
for base in bases if is_catalog(base)
|
|
|
|
for name, provider in six.iteritems(
|
|
|
|
base.providers))
|
|
|
|
|
2015-07-17 19:31:44 +03:00
|
|
|
providers = dict()
|
2015-10-07 13:36:28 +03:00
|
|
|
providers.update(cls_providers)
|
|
|
|
providers.update(inherited_providers)
|
2015-07-17 19:31:44 +03:00
|
|
|
|
2015-10-14 17:51:05 +03:00
|
|
|
cls = type.__new__(mcs, class_name, bases, attributes)
|
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
cls.cls_providers = cls_providers
|
|
|
|
cls.inherited_providers = inherited_providers
|
|
|
|
cls.providers = providers
|
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
cls.overridden_by = tuple()
|
|
|
|
|
2015-10-16 23:54:51 +03:00
|
|
|
cls.Bundle = mcs.bundle_cls_factory(cls)
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-10-14 17:51:05 +03:00
|
|
|
for name, provider in six.iteritems(cls_providers):
|
2015-10-17 01:00:32 +03:00
|
|
|
if provider.is_bound:
|
|
|
|
raise Error('Provider {0} has been already bound to catalog'
|
|
|
|
'{1} as "{2}"'.format(provider,
|
|
|
|
provider.bind.catalog,
|
|
|
|
provider.bind.name))
|
2015-10-14 17:51:05 +03:00
|
|
|
provider.bind = ProviderBinding(cls, name)
|
|
|
|
|
|
|
|
return cls
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-10-16 23:54:51 +03:00
|
|
|
@classmethod
|
|
|
|
def bundle_cls_factory(mcs, cls):
|
|
|
|
"""Create bundle class for catalog."""
|
|
|
|
return type('{0}Bundle', (CatalogBundle,), dict(catalog=cls))
|
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
@property
|
|
|
|
def is_overridden(cls):
|
|
|
|
"""Check if catalog is overridden by another catalog."""
|
|
|
|
return bool(cls.overridden_by)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def last_overriding(cls):
|
|
|
|
"""Return last overriding catalog."""
|
|
|
|
try:
|
|
|
|
return cls.overridden_by[-1]
|
|
|
|
except (TypeError, IndexError):
|
|
|
|
raise Error('Catalog {0} is not overridden'.format(str(cls)))
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def __repr__(cls):
|
|
|
|
"""Return string representation of the catalog class."""
|
2015-11-04 17:32:04 +03:00
|
|
|
return '<{0}.{1}>'.format(cls.__module__, cls.__name__)
|
|
|
|
|
|
|
|
__str__ = __repr__
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
@six.add_metaclass(DeclarativeCatalogMetaClass)
|
|
|
|
class DeclarativeCatalog(object):
|
|
|
|
"""Declarative catalog catalog of providers.
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
:type Bundle: CatalogBundle
|
|
|
|
:param Bundle: Catalog's bundle class
|
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
:type providers: dict[str, dependency_injector.Provider]
|
|
|
|
:param providers: Dict of all catalog providers, including inherited from
|
|
|
|
parent catalogs
|
|
|
|
|
|
|
|
:type cls_providers: dict[str, dependency_injector.Provider]
|
|
|
|
:param cls_providers: Dict of current catalog providers
|
|
|
|
|
|
|
|
:type inherited_providers: dict[str, dependency_injector.Provider]
|
|
|
|
:param inherited_providers: Dict of providers, that are inherited from
|
|
|
|
parent catalogs
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
:type overridden_by: tuple[DeclarativeCatalog]
|
2015-10-23 16:23:12 +03:00
|
|
|
:param overridden_by: Tuple of overriding catalogs
|
|
|
|
|
|
|
|
:type is_overridden: bool
|
|
|
|
:param is_overridden: Read-only, evaluated in runtime, property that is
|
|
|
|
set to True if catalog is overridden
|
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
:type last_overriding: DeclarativeCatalog | None
|
2015-10-23 16:23:12 +03:00
|
|
|
:param last_overriding: Reference to the last overriding catalog, if any
|
2015-10-07 13:36:28 +03:00
|
|
|
"""
|
2015-07-17 19:31:44 +03:00
|
|
|
|
2015-10-16 23:54:51 +03:00
|
|
|
Bundle = CatalogBundle
|
2015-10-15 20:15:38 +03:00
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
cls_providers = dict()
|
|
|
|
inherited_providers = dict()
|
2015-10-15 20:15:38 +03:00
|
|
|
providers = dict()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
overridden_by = tuple()
|
|
|
|
is_overridden = bool
|
|
|
|
last_overriding = None
|
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
__IS_CATALOG__ = True
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-10-19 12:12:38 +03:00
|
|
|
@classmethod
|
|
|
|
def is_bundle_owner(cls, bundle):
|
|
|
|
"""Check if catalog is bundle owner."""
|
|
|
|
return ensure_is_catalog_bundle(bundle) and bundle.catalog is cls
|
|
|
|
|
2015-01-11 19:10:11 +03:00
|
|
|
@classmethod
|
2015-07-17 19:31:44 +03:00
|
|
|
def filter(cls, provider_type):
|
|
|
|
"""Return dict of providers, that are instance of provided type."""
|
2015-09-03 16:00:23 +03:00
|
|
|
return dict((name, provider)
|
|
|
|
for name, provider in six.iteritems(cls.providers)
|
|
|
|
if isinstance(provider, provider_type))
|
2015-01-11 19:10:11 +03:00
|
|
|
|
|
|
|
@classmethod
|
2015-03-12 13:45:15 +03:00
|
|
|
def override(cls, overriding):
|
2015-07-06 16:52:51 +03:00
|
|
|
"""Override current catalog providers by overriding catalog providers.
|
2015-01-11 19:10:11 +03:00
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
:type overriding: DeclarativeCatalog
|
2015-01-11 19:10:11 +03:00
|
|
|
"""
|
2015-10-23 16:23:12 +03:00
|
|
|
cls.overridden_by += (overriding,)
|
2015-10-07 13:36:28 +03:00
|
|
|
for name, provider in six.iteritems(overriding.cls_providers):
|
2015-07-17 19:31:44 +03:00
|
|
|
cls.providers[name].override(provider)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
@classmethod
|
|
|
|
def reset_last_overriding(cls):
|
|
|
|
"""Reset last overriding catalog."""
|
|
|
|
if not cls.is_overridden:
|
|
|
|
raise Error('Catalog {0} is not overridden'.format(str(cls)))
|
|
|
|
cls.overridden_by = cls.overridden_by[:-1]
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def reset_override(cls):
|
|
|
|
"""Reset all overridings for all catalog providers."""
|
|
|
|
cls.overridden_by = tuple()
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_override()
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
"""Return provider with specified name or raises error."""
|
|
|
|
try:
|
|
|
|
return cls.providers[name]
|
|
|
|
except KeyError:
|
|
|
|
raise Error('{0} has no provider with such name - {1}'.format(
|
|
|
|
cls, name))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def has(cls, name):
|
|
|
|
"""Check if there is provider with certain name."""
|
|
|
|
return name in cls.providers
|
|
|
|
|
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
# Backward compatibility for versions < 0.11.*
|
|
|
|
AbstractCatalog = DeclarativeCatalog
|
|
|
|
|
|
|
|
|
2015-10-14 17:51:05 +03:00
|
|
|
class ProviderBinding(object):
|
|
|
|
"""Catalog provider binding."""
|
|
|
|
|
|
|
|
__slots__ = ('catalog', 'name')
|
|
|
|
|
|
|
|
def __init__(self, catalog, name):
|
|
|
|
"""Initializer."""
|
|
|
|
self.catalog = catalog
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
2015-08-03 12:57:42 +03:00
|
|
|
def override(catalog):
|
|
|
|
"""Catalog overriding decorator."""
|
|
|
|
def decorator(overriding_catalog):
|
|
|
|
"""Overriding decorator."""
|
|
|
|
catalog.override(overriding_catalog)
|
|
|
|
return overriding_catalog
|
|
|
|
return decorator
|