mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Refactoring of Catalog using metaclass
This commit is contained in:
parent
3b9d36f2d2
commit
0e272f9d58
|
@ -1,14 +1,38 @@
|
||||||
"""Catalog module."""
|
"""Catalog module."""
|
||||||
|
|
||||||
from .providers import Provider
|
from six import iteritems
|
||||||
|
|
||||||
from .errors import Error
|
from .errors import Error
|
||||||
|
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()
|
||||||
|
for name, value in attributes.iteritems():
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class AbstractCatalog(object):
|
class AbstractCatalog(object):
|
||||||
|
|
||||||
"""Abstract object provides catalog."""
|
"""Abstract providers catalog."""
|
||||||
|
|
||||||
__slots__ = ('__used_providers__',)
|
providers = dict()
|
||||||
|
|
||||||
|
__slots__ = ('providers', '__used_providers__',)
|
||||||
|
__metaclass__ = CatalogMetaClass
|
||||||
|
|
||||||
def __init__(self, *used_providers):
|
def __init__(self, *used_providers):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
|
@ -17,7 +41,7 @@ class AbstractCatalog(object):
|
||||||
def __getattribute__(self, item):
|
def __getattribute__(self, item):
|
||||||
"""Return providers."""
|
"""Return providers."""
|
||||||
attribute = super(AbstractCatalog, self).__getattribute__(item)
|
attribute = super(AbstractCatalog, self).__getattribute__(item)
|
||||||
if item in ('__used_providers__',):
|
if item in ('providers', '__used_providers__',):
|
||||||
return attribute
|
return attribute
|
||||||
|
|
||||||
if attribute not in self.__used_providers__:
|
if attribute not in self.__used_providers__:
|
||||||
|
@ -26,15 +50,11 @@ class AbstractCatalog(object):
|
||||||
return attribute
|
return attribute
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all_providers(cls, provider_type=Provider):
|
def filter(cls, provider_type):
|
||||||
"""Return set of all class providers."""
|
"""Return dict of providers, that are instance of provided type."""
|
||||||
providers = set()
|
return dict([(name, provider)
|
||||||
for attr_name in set(dir(cls)) - set(dir(AbstractCatalog)):
|
for name, provider in iteritems(cls.providers)
|
||||||
provider = getattr(cls, attr_name)
|
if isinstance(provider, provider_type)])
|
||||||
if not isinstance(provider, provider_type):
|
|
||||||
continue
|
|
||||||
providers.add((attr_name, provider))
|
|
||||||
return providers
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def override(cls, overriding):
|
def override(cls, overriding):
|
||||||
|
@ -42,7 +62,5 @@ class AbstractCatalog(object):
|
||||||
|
|
||||||
:type overriding: AbstractCatalog
|
:type overriding: AbstractCatalog
|
||||||
"""
|
"""
|
||||||
overridden = overriding.all_providers() - cls.all_providers()
|
for name, provider in iteritems(overriding.providers):
|
||||||
for name, provider in overridden:
|
cls.providers[name].override(provider)
|
||||||
overridden_provider = getattr(cls, name)
|
|
||||||
overridden_provider.override(provider)
|
|
||||||
|
|
|
@ -33,19 +33,38 @@ class CatalogTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_all_providers(self):
|
def test_all_providers(self):
|
||||||
"""Test getting of all catalog providers."""
|
"""Test getting of all catalog providers."""
|
||||||
all_providers = self.Catalog.all_providers()
|
self.assertTrue(len(self.Catalog.providers) == 2)
|
||||||
all_providers_dict = dict(all_providers)
|
|
||||||
|
|
||||||
self.assertIsInstance(all_providers, set)
|
self.assertIn('obj', self.Catalog.providers)
|
||||||
self.assertTrue(len(all_providers) == 2)
|
self.assertIn(self.Catalog.obj, self.Catalog.providers.values())
|
||||||
|
|
||||||
self.assertIn('obj', all_providers_dict)
|
self.assertIn('another_obj', self.Catalog.providers)
|
||||||
self.assertIn(self.Catalog.obj, all_providers_dict.values())
|
self.assertIn(self.Catalog.another_obj,
|
||||||
|
self.Catalog.providers.values())
|
||||||
self.assertIn('another_obj', all_providers_dict)
|
|
||||||
self.assertIn(self.Catalog.another_obj, all_providers_dict.values())
|
|
||||||
|
|
||||||
def test_all_providers_by_type(self):
|
def test_all_providers_by_type(self):
|
||||||
"""Test getting of all catalog providers of specific type."""
|
"""Test getting of all catalog providers of specific type."""
|
||||||
self.assertTrue(len(self.Catalog.all_providers(Object)) == 2)
|
self.assertTrue(len(self.Catalog.filter(Object)) == 2)
|
||||||
self.assertTrue(len(self.Catalog.all_providers(Value)) == 0)
|
self.assertTrue(len(self.Catalog.filter(Value)) == 0)
|
||||||
|
|
||||||
|
def test_metaclass_with_several_catalogs(self):
|
||||||
|
"""Test that metaclass work well with several catalogs."""
|
||||||
|
class Catalog1(AbstractCatalog):
|
||||||
|
|
||||||
|
"""Catalog1."""
|
||||||
|
|
||||||
|
provider = Object(object())
|
||||||
|
|
||||||
|
class Catalog2(AbstractCatalog):
|
||||||
|
|
||||||
|
"""Catalog2."""
|
||||||
|
|
||||||
|
provider = Object(object())
|
||||||
|
|
||||||
|
self.assertTrue(len(Catalog1.providers) == 1)
|
||||||
|
self.assertIs(Catalog1.provider, Catalog1.providers['provider'])
|
||||||
|
|
||||||
|
self.assertTrue(len(Catalog2.providers) == 1)
|
||||||
|
self.assertIs(Catalog2.provider, Catalog2.providers['provider'])
|
||||||
|
|
||||||
|
self.assertIsNot(Catalog1.provider, Catalog2.provider)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user