mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-21 17:16:46 +03:00
Renaming Catalog class to AbstractCatalog
This commit is contained in:
parent
65893178dc
commit
5462bb91c4
40
README.md
40
README.md
|
@ -11,7 +11,9 @@ Example of objects catalog definition and usage:
|
|||
Concept example of objects catalogs.
|
||||
"""
|
||||
|
||||
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import Singleton, NewInstance
|
||||
from objects.injections import InitArg, Attribute
|
||||
import sqlite3
|
||||
|
||||
|
||||
|
@ -28,7 +30,7 @@ class ObjectB(object):
|
|||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class AppCatalog(Catalog):
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
@ -49,20 +51,20 @@ class AppCatalog(Catalog):
|
|||
|
||||
|
||||
# Catalog static provides.
|
||||
a1, a2 = AppCatalog.object_a(), AppCatalog.object_a()
|
||||
b1, b2 = AppCatalog.object_b(), AppCatalog.object_b()
|
||||
a1, a2 = Catalog.object_a(), Catalog.object_a()
|
||||
b1, b2 = Catalog.object_b(), Catalog.object_b()
|
||||
|
||||
# Some asserts.
|
||||
assert a1 is not a2
|
||||
assert b1 is not b2
|
||||
assert a1.db is a2.db is b1.db is b2.db is AppCatalog.database()
|
||||
assert a1.db is a2.db is b1.db is b2.db is Catalog.database()
|
||||
|
||||
|
||||
# Dependencies injection (The Python Way) into class.
|
||||
class Consumer(object):
|
||||
|
||||
dependencies = AppCatalog(AppCatalog.object_a,
|
||||
AppCatalog.object_b)
|
||||
dependencies = Catalog(Catalog.object_a,
|
||||
Catalog.object_b)
|
||||
|
||||
def test(self):
|
||||
a1 = self.dependencies.object_a()
|
||||
|
@ -87,8 +89,8 @@ Consumer().test()
|
|||
|
||||
|
||||
# Dependencies injection (The Python Way) into a callback.
|
||||
def consumer_callback(dependencies=AppCatalog(AppCatalog.object_a,
|
||||
AppCatalog.object_b)):
|
||||
def consumer_callback(dependencies=Catalog(Catalog.object_a,
|
||||
Catalog.object_b)):
|
||||
a1 = dependencies.object_a()
|
||||
a2 = dependencies.object_a()
|
||||
|
||||
|
@ -116,7 +118,9 @@ Concept example of objects overrides.
|
|||
"""
|
||||
|
||||
|
||||
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute, overrides
|
||||
from objects import AbstractCatalog, overrides
|
||||
from objects.providers import Singleton, NewInstance
|
||||
from objects.injections import InitArg, Attribute
|
||||
import sqlite3
|
||||
|
||||
|
||||
|
@ -131,7 +135,7 @@ class ObjectAMock(ObjectA):
|
|||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class AppCatalog(Catalog):
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
@ -146,25 +150,25 @@ class AppCatalog(Catalog):
|
|||
""" :type: (objects.Provider) -> ObjectA """
|
||||
|
||||
|
||||
# Overriding AppCatalog by SandboxCatalog with some mocks.
|
||||
@overrides(AppCatalog)
|
||||
class SandboxCatalog(AppCatalog):
|
||||
# Overriding Catalog by SandboxCatalog with some mocks.
|
||||
@overrides(Catalog)
|
||||
class SandboxCatalog(Catalog):
|
||||
"""
|
||||
Sandbox objects catalog with some mocks.
|
||||
"""
|
||||
|
||||
object_a = NewInstance(ObjectAMock,
|
||||
InitArg('db', AppCatalog.database))
|
||||
InitArg('db', Catalog.database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
|
||||
|
||||
# Catalog static provides.
|
||||
a1 = AppCatalog.object_a()
|
||||
a2 = AppCatalog.object_a()
|
||||
a1 = Catalog.object_a()
|
||||
a2 = Catalog.object_a()
|
||||
|
||||
# Some asserts.
|
||||
assert isinstance(a1, ObjectAMock)
|
||||
assert isinstance(a2, ObjectAMock)
|
||||
assert a1 is not a2
|
||||
assert a1.db is a2.db is AppCatalog.database()
|
||||
assert a1.db is a2.db is Catalog.database()
|
||||
```
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
Concept example of objects catalogs.
|
||||
"""
|
||||
|
||||
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import Singleton, NewInstance
|
||||
from objects.injections import InitArg, Attribute
|
||||
import sqlite3
|
||||
|
||||
|
||||
|
@ -19,7 +21,7 @@ class ObjectB(object):
|
|||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class AppCatalog(Catalog):
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
@ -40,20 +42,20 @@ class AppCatalog(Catalog):
|
|||
|
||||
|
||||
# Catalog static provides.
|
||||
a1, a2 = AppCatalog.object_a(), AppCatalog.object_a()
|
||||
b1, b2 = AppCatalog.object_b(), AppCatalog.object_b()
|
||||
a1, a2 = Catalog.object_a(), Catalog.object_a()
|
||||
b1, b2 = Catalog.object_b(), Catalog.object_b()
|
||||
|
||||
# Some asserts.
|
||||
assert a1 is not a2
|
||||
assert b1 is not b2
|
||||
assert a1.db is a2.db is b1.db is b2.db is AppCatalog.database()
|
||||
assert a1.db is a2.db is b1.db is b2.db is Catalog.database()
|
||||
|
||||
|
||||
# Dependencies injection (The Python Way) into class.
|
||||
class Consumer(object):
|
||||
|
||||
dependencies = AppCatalog(AppCatalog.object_a,
|
||||
AppCatalog.object_b)
|
||||
dependencies = Catalog(Catalog.object_a,
|
||||
Catalog.object_b)
|
||||
|
||||
def test(self):
|
||||
a1 = self.dependencies.object_a()
|
||||
|
@ -78,8 +80,8 @@ Consumer().test()
|
|||
|
||||
|
||||
# Dependencies injection (The Python Way) into a callback.
|
||||
def consumer_callback(dependencies=AppCatalog(AppCatalog.object_a,
|
||||
AppCatalog.object_b)):
|
||||
def consumer_callback(dependencies=Catalog(Catalog.object_a,
|
||||
Catalog.object_b)):
|
||||
a1 = dependencies.object_a()
|
||||
a2 = dependencies.object_a()
|
||||
|
||||
|
|
|
@ -3,7 +3,9 @@ Concept example of objects overrides.
|
|||
"""
|
||||
|
||||
|
||||
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute, overrides
|
||||
from objects import AbstractCatalog, overrides
|
||||
from objects.providers import Singleton, NewInstance
|
||||
from objects.injections import InitArg, Attribute
|
||||
import sqlite3
|
||||
|
||||
|
||||
|
@ -18,7 +20,7 @@ class ObjectAMock(ObjectA):
|
|||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class AppCatalog(Catalog):
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
@ -33,24 +35,24 @@ class AppCatalog(Catalog):
|
|||
""" :type: (objects.Provider) -> ObjectA """
|
||||
|
||||
|
||||
# Overriding AppCatalog by SandboxCatalog with some mocks.
|
||||
@overrides(AppCatalog)
|
||||
class SandboxCatalog(AppCatalog):
|
||||
# Overriding Catalog by SandboxCatalog with some mocks.
|
||||
@overrides(Catalog)
|
||||
class SandboxCatalog(Catalog):
|
||||
"""
|
||||
Sandbox objects catalog with some mocks.
|
||||
"""
|
||||
|
||||
object_a = NewInstance(ObjectAMock,
|
||||
InitArg('db', AppCatalog.database))
|
||||
InitArg('db', Catalog.database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
|
||||
|
||||
# Catalog static provides.
|
||||
a1 = AppCatalog.object_a()
|
||||
a2 = AppCatalog.object_a()
|
||||
a1 = Catalog.object_a()
|
||||
a2 = Catalog.object_a()
|
||||
|
||||
# Some asserts.
|
||||
assert isinstance(a1, ObjectAMock)
|
||||
assert isinstance(a2, ObjectAMock)
|
||||
assert a1 is not a2
|
||||
assert a1.db is a2.db is AppCatalog.database()
|
||||
assert a1.db is a2.db is Catalog.database()
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
`Objects` library.
|
||||
"""
|
||||
|
||||
from .catalog import Catalog, overrides
|
||||
from .catalog import AbstractCatalog, overrides
|
||||
from .providers import (Provider, NewInstance, Singleton, Class, Object,
|
||||
Function, Value)
|
||||
from .injections import InitArg, Attribute, Method
|
||||
|
||||
|
||||
__all__ = ['Catalog', 'overrides',
|
||||
__all__ = ['AbstractCatalog', 'overrides',
|
||||
|
||||
# Providers
|
||||
'Provider', 'NewInstance', 'Singleton', 'Class',
|
||||
|
|
|
@ -5,9 +5,9 @@ Catalog module.
|
|||
from .providers import Provider
|
||||
|
||||
|
||||
class Catalog(object):
|
||||
class AbstractCatalog(object):
|
||||
"""
|
||||
Object provides catalog.
|
||||
Abstract object provides catalog.
|
||||
"""
|
||||
|
||||
def __init__(self, *used_providers):
|
||||
|
@ -23,7 +23,7 @@ class Catalog(object):
|
|||
:param item:
|
||||
:return:
|
||||
"""
|
||||
attribute = super(Catalog, self).__getattribute__(item)
|
||||
attribute = super(AbstractCatalog, self).__getattribute__(item)
|
||||
if item in ('__used_providers__',):
|
||||
return attribute
|
||||
|
||||
|
@ -38,7 +38,7 @@ class Catalog(object):
|
|||
Returns set of all class providers.
|
||||
"""
|
||||
providers = set()
|
||||
for attr_name in set(dir(cls)) - set(dir(Catalog)):
|
||||
for attr_name in set(dir(cls)) - set(dir(AbstractCatalog)):
|
||||
provider = getattr(cls, attr_name)
|
||||
if not isinstance(provider, Provider):
|
||||
continue
|
||||
|
@ -50,7 +50,7 @@ class Catalog(object):
|
|||
"""
|
||||
Overrides current catalog providers by overriding catalog providers.
|
||||
|
||||
:param overriding: Catalog
|
||||
:param overriding: AbstractCatalog
|
||||
"""
|
||||
overriden = overriding.__all_providers__() - cls.__all_providers__()
|
||||
for name, provider in overriden:
|
||||
|
|
Loading…
Reference in New Issue
Block a user