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-07-17 19:31:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CatalogMetaClass(type):
|
|
|
|
"""Providers catalog meta class."""
|
|
|
|
|
|
|
|
def __new__(mcs, class_name, bases, attributes):
|
|
|
|
"""Meta 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-07 13:36:28 +03:00
|
|
|
attributes['cls_providers'] = cls_providers
|
|
|
|
attributes['inherited_providers'] = inherited_providers
|
|
|
|
attributes['providers'] = providers
|
|
|
|
return type.__new__(mcs, class_name, bases, attributes)
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def __repr__(cls):
|
|
|
|
"""Return string representation of the catalog class."""
|
|
|
|
return '<Catalog "' + '.'.join((cls.__module__, cls.__name__)) + '">'
|
|
|
|
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-09-01 00:36:26 +03:00
|
|
|
@six.add_metaclass(CatalogMetaClass)
|
2015-01-18 03:28:41 +03:00
|
|
|
class AbstractCatalog(object):
|
2015-10-07 13:36:28 +03:00
|
|
|
"""Abstract providers catalog.
|
|
|
|
|
|
|
|
: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-07-17 19:31:44 +03:00
|
|
|
|
|
|
|
providers = dict()
|
2015-10-07 13:36:28 +03:00
|
|
|
cls_providers = dict()
|
|
|
|
inherited_providers = dict()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
__IS_CATALOG__ = True
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def __new__(cls, *providers):
|
|
|
|
"""Catalog constructor.
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
Catalogs are declaratives entities that could not be instantiated.
|
|
|
|
Catalog constructor is designed to produce subsets of catalog
|
|
|
|
providers.
|
|
|
|
"""
|
|
|
|
return CatalogSubset(catalog=cls, providers=providers)
|
2015-01-11 19:10:11 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
@classmethod
|
|
|
|
def is_subset_owner(cls, subset):
|
|
|
|
"""Check if catalog is subset owner."""
|
|
|
|
return subset.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-07-06 16:52:51 +03:00
|
|
|
:type overriding: AbstractCatalog
|
2015-01-11 19:10:11 +03:00
|
|
|
"""
|
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-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
|
|
|
|
|
|
|
|
|
|
|
|
class CatalogSubset(object):
|
|
|
|
"""Subset of catalog providers."""
|
|
|
|
|
|
|
|
__IS_SUBSET__ = True
|
|
|
|
__slots__ = ('catalog', 'available_providers', 'providers', '__dict__')
|
|
|
|
|
|
|
|
def __init__(self, catalog, providers):
|
|
|
|
"""Initializer."""
|
|
|
|
self.catalog = catalog
|
|
|
|
self.available_providers = set(providers)
|
|
|
|
self.providers = dict()
|
|
|
|
for provider_name in self.available_providers:
|
|
|
|
try:
|
|
|
|
provider = self.catalog.providers[provider_name]
|
|
|
|
except KeyError:
|
|
|
|
raise Error('Subset could not add "{0}" provider in scope, '
|
|
|
|
'because {1} has no provider with '
|
|
|
|
'such name'.format(provider_name, self.catalog))
|
|
|
|
else:
|
|
|
|
self.providers[provider_name] = provider
|
|
|
|
self.__dict__.update(self.providers)
|
|
|
|
super(CatalogSubset, self).__init__()
|
|
|
|
|
|
|
|
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 __getattr__(self, item):
|
|
|
|
"""Raise an error on every attempt to get undefined provider."""
|
|
|
|
self._raise_undefined_provider_error(item)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""Return string representation of subset."""
|
|
|
|
return '<Subset ({0}), {1}>'.format(
|
|
|
|
', '.join(self.available_providers), self.catalog)
|
|
|
|
|
|
|
|
def _raise_undefined_provider_error(self, name):
|
|
|
|
"""Raise error for cases when there is no such provider in subset."""
|
|
|
|
raise Error('Provider "{0}" is not a part of {1}'.format(name, self))
|
|
|
|
|
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
|