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-07-17 19:31:44 +03:00
|
|
|
from .utils import is_provider
|
|
|
|
|
|
|
|
|
|
|
|
class CatalogMetaClass(type):
|
|
|
|
|
|
|
|
"""Providers catalog meta class."""
|
|
|
|
|
|
|
|
def __new__(mcs, class_name, bases, attributes):
|
|
|
|
"""Meta class factory."""
|
|
|
|
providers = dict()
|
|
|
|
new_attributes = dict()
|
2015-09-01 00:36:26 +03:00
|
|
|
for name, value in six.iteritems(attributes):
|
2015-07-17 19:31:44 +03:00
|
|
|
if is_provider(value):
|
|
|
|
providers[name] = value
|
|
|
|
new_attributes[name] = value
|
|
|
|
|
|
|
|
cls = type.__new__(mcs, class_name, bases, new_attributes)
|
|
|
|
cls.providers = cls.providers.copy()
|
|
|
|
cls.providers.update(providers)
|
|
|
|
return cls
|
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-03-09 01:01:39 +03:00
|
|
|
|
2015-07-17 19:31:44 +03:00
|
|
|
"""Abstract providers catalog."""
|
|
|
|
|
|
|
|
providers = dict()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_used_providers',)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-01-04 17:26:33 +03:00
|
|
|
def __init__(self, *used_providers):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._used_providers = set(used_providers)
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-01-11 19:10:11 +03:00
|
|
|
def __getattribute__(self, item):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return providers."""
|
2015-01-18 03:28:41 +03:00
|
|
|
attribute = super(AbstractCatalog, self).__getattribute__(item)
|
2015-07-22 16:42:36 +03:00
|
|
|
if item in ('providers', '_used_providers',):
|
2015-01-11 19:10:11 +03:00
|
|
|
return attribute
|
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
if attribute not in self._used_providers:
|
2015-03-18 13:48:19 +03:00
|
|
|
raise Error('Provider \'{0}\' '.format(item) +
|
2015-03-13 18:31:07 +03:00
|
|
|
'is not listed in dependencies')
|
2015-01-11 19:10:11 +03:00
|
|
|
return attribute
|
|
|
|
|
|
|
|
@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-09-01 00:36:26 +03:00
|
|
|
for name, provider in six.iteritems(overriding.providers):
|
2015-07-17 19:31:44 +03:00
|
|
|
cls.providers[name].override(provider)
|
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
|