version 0.2.0, external dependency provider has been added

This commit is contained in:
Roman Mogilatov 2015-01-23 01:42:59 +02:00
parent 5462bb91c4
commit 92254b0276
4 changed files with 152 additions and 1 deletions

View File

@ -172,3 +172,62 @@ assert isinstance(a2, ObjectAMock)
assert a1 is not a2
assert a1.db is a2.db is Catalog.database()
```
Example of objects catalog external dependency:
```python
"""
Concept example of objects catalogs.
"""
from objects import AbstractCatalog
from objects.providers import Singleton, NewInstance, ExternalDependency
from objects.injections import InitArg, Attribute
import sqlite3
# Some example classes.
class ObjectA(object):
def __init__(self, db):
self.db = db
class ObjectB(object):
def __init__(self, a, db):
self.a = a
self.db = db
# Catalog of objects providers.
class Catalog(AbstractCatalog):
"""
Objects catalog.
"""
database = ExternalDependency(instance_of=sqlite3.Connection)
""" :type: (objects.Provider) -> sqlite3.Connection """
object_a = NewInstance(ObjectA,
InitArg('db', database))
""" :type: (objects.Provider) -> ObjectA """
object_b = NewInstance(ObjectB,
InitArg('a', object_a),
InitArg('db', database))
""" :type: (objects.Provider) -> ObjectB """
# Satisfaction of external dependency.
Catalog.database.satisfy(Singleton(sqlite3.Connection,
InitArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row)))
# Catalog static provides.
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 Catalog.database()
```

View File

@ -1 +1 @@
0.1.0
0.2.0

View File

@ -0,0 +1,54 @@
"""
Concept example of objects catalogs.
"""
from objects import AbstractCatalog
from objects.providers import Singleton, NewInstance, ExternalDependency
from objects.injections import InitArg, Attribute
import sqlite3
# Some example classes.
class ObjectA(object):
def __init__(self, db):
self.db = db
class ObjectB(object):
def __init__(self, a, db):
self.a = a
self.db = db
# Catalog of objects providers.
class Catalog(AbstractCatalog):
"""
Objects catalog.
"""
database = ExternalDependency(instance_of=sqlite3.Connection)
""" :type: (objects.Provider) -> sqlite3.Connection """
object_a = NewInstance(ObjectA,
InitArg('db', database))
""" :type: (objects.Provider) -> ObjectA """
object_b = NewInstance(ObjectB,
InitArg('a', object_a),
InitArg('db', database))
""" :type: (objects.Provider) -> ObjectB """
# Satisfaction of external dependency.
Catalog.database.satisfy(Singleton(sqlite3.Connection,
InitArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row)))
# Catalog static provides.
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 Catalog.database()

View File

@ -108,6 +108,44 @@ class Singleton(NewInstance):
return self.instance
class ExternalDependency(Provider):
"""
External dependency provider.
"""
def __init__(self, instance_of):
"""
Initializer
:param instance_of: type
"""
self.instance_of = instance_of
self.dependency = None
super(ExternalDependency, self).__init__()
def satisfy(self, provider):
"""
Satisfies an external dependency.
:param provider: Provider
"""
self.dependency = provider
def __call__(self, *args, **kwargs):
"""
Returns provided instance.
"""
if not self.dependency:
raise ValueError('Dependency is not satisfied')
result = self.dependency.__call__(*args, **kwargs)
if not isinstance(result, self.instance_of):
raise TypeError('{} is not an instance of {}'.format(result,
self.instance_of))
return result
class _StaticProvider(Provider):
"""
Static provider is base implementation that provides exactly the same as