From 92254b0276acfd84c3127a4a423e57d15a497674 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 23 Jan 2015 01:42:59 +0200 Subject: [PATCH] version 0.2.0, external dependency provider has been added --- README.md | 59 +++++++++++++++++++++++++++++++++ VERSION | 2 +- examples/external_dependency.py | 54 ++++++++++++++++++++++++++++++ objects/providers.py | 38 +++++++++++++++++++++ 4 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 examples/external_dependency.py diff --git a/README.md b/README.md index e7461fba..fd8fdf1f 100644 --- a/README.md +++ b/README.md @@ -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() +``` diff --git a/VERSION b/VERSION index 6e8bf73a..0ea3a944 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 +0.2.0 diff --git a/examples/external_dependency.py b/examples/external_dependency.py new file mode 100644 index 00000000..a19e0c95 --- /dev/null +++ b/examples/external_dependency.py @@ -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() diff --git a/objects/providers.py b/objects/providers.py index af200116..0114090a 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -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