Remove required DynamicCatalog.name attribute

This commit is contained in:
Roman Mogilatov 2015-11-10 19:47:45 +02:00
parent 05d6db5664
commit 548847f83a
4 changed files with 13 additions and 17 deletions

View File

@ -42,7 +42,7 @@ from .utils import ensure_is_catalog_bundle
from .errors import Error
# Backward compatimility fix for versions < 0.11.*
# Backward compatibility for versions < 0.11.*
from . import catalogs
catalog = catalogs

View File

@ -35,8 +35,7 @@ class CatalogBundle(object):
:rtype: CatalogBundle
:return: Subclass of CatalogBundle
"""
return type('{0}Bundle'.format(catalog.name), (cls,),
dict(catalog=catalog))
return type('BundleSubclass', (cls,), dict(catalog=catalog))
def get(self, name):
"""Return provider with specified name or raise an error."""
@ -69,19 +68,17 @@ class DynamicCatalog(object):
"""Catalog of providers."""
__IS_CATALOG__ = True
__slots__ = ('name', 'Bundle', 'providers', 'provider_names',
'overridden_by')
__slots__ = ('Bundle', 'providers', 'provider_names',
'overridden_by', 'name')
def __init__(self, name, **providers):
def __init__(self, **providers):
"""Initializer.
:param name: Catalog's name
:type name: str
:param kwargs: Dict of providers with their catalog names
:type kwargs: dict[str, dependency_injector.providers.Provider]
"""
self.name = name
self.Bundle = CatalogBundle.sub_cls_factory(self)
self.providers = dict()
self.provider_names = dict()
@ -94,6 +91,8 @@ class DynamicCatalog(object):
self.provider_names[provider] = name
self.providers[name] = provider
self.overridden_by = tuple()
self.name = '.'.join((self.__class__.__module__,
self.__class__.__name__))
def is_bundle_owner(self, bundle):
"""Check if catalog is bundle owner."""
@ -191,9 +190,9 @@ class DeclarativeCatalogMetaClass(type):
providers = cls_providers + inherited_providers
cls.name = '.'.join((cls.__module__, cls.__name__))
cls.catalog = DynamicCatalog(cls.name, **dict(providers))
cls.catalog = DynamicCatalog(**dict(providers))
cls.catalog.name = cls.name
cls.Bundle = cls.catalog.Bundle
cls.cls_providers = dict(cls_providers)
cls.inherited_providers = dict(inherited_providers)

View File

@ -273,8 +273,7 @@ class OverrideTests(unittest.TestCase):
def test_overriding_with_dynamic_catalog(self):
"""Test catalog overriding with another dynamic catalog."""
self.Catalog.override(di.DynamicCatalog('OverridingCatalog',
obj=di.Value(1),
self.Catalog.override(di.DynamicCatalog(obj=di.Value(1),
another_obj=di.Value(2)))
self.assertEqual(self.Catalog.obj(), 1)
self.assertEqual(self.Catalog.another_obj(), 2)

View File

@ -217,7 +217,7 @@ class IsCatalogTests(unittest.TestCase):
def test_with_dynamic_catalog(self):
"""Test with class."""
self.assertTrue(di.is_catalog(di.DynamicCatalog('TestCatalog')))
self.assertTrue(di.is_catalog(di.DynamicCatalog()))
def test_with_child_class(self):
"""Test with parent class."""
@ -244,8 +244,7 @@ class IsDynamicCatalogTests(unittest.TestCase):
def test_with_dynamic_catalog(self):
"""Test with dynamic catalog."""
self.assertTrue(di.is_dynamic_catalog(di.DynamicCatalog(
'TestCatalog')))
self.assertTrue(di.is_dynamic_catalog(di.DynamicCatalog()))
class IsDeclarativeCatalogTests(unittest.TestCase):
@ -257,8 +256,7 @@ class IsDeclarativeCatalogTests(unittest.TestCase):
def test_with_dynamic_catalog(self):
"""Test with dynamic catalog."""
self.assertFalse(di.is_declarative_catalog(di.DynamicCatalog(
'TestCatalog')))
self.assertFalse(di.is_declarative_catalog(di.DynamicCatalog()))
class IsCatalogBundleTests(unittest.TestCase):