updating concept

This commit is contained in:
Roman Mogilatov 2015-01-10 11:24:25 +02:00
parent 80e2a1fdf1
commit 5b9723c530
6 changed files with 99 additions and 148 deletions

View File

@ -2,7 +2,7 @@
Concept example of objects catalogs. Concept example of objects catalogs.
""" """
import objects from objects import Catalog, Singleton, NewInstance, InitArg, Attribute
import sqlite3 import sqlite3
@ -19,29 +19,30 @@ class B(object):
# Catalog of objects providers. # Catalog of objects providers.
class Catalog(objects.Catalog): class AppCatalog(Catalog):
""" """
Objects catalog. Objects catalog.
""" """
database = objects.Singleton(provides=sqlite3.Connection, database = Singleton(sqlite3.Connection,
database='example.db') InitArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row))
""" :type: (objects.Provider) -> sqlite3.Connection """ """ :type: (objects.Provider) -> sqlite3.Connection """
object_a = objects.NewInstance(provides=A, object_a = NewInstance(A,
db=database) InitArg('db', database))
""" :type: (objects.Provider) -> A """ """ :type: (objects.Provider) -> A """
object_b = objects.NewInstance(provides=B, object_b = NewInstance(B,
a=object_a, InitArg('a', object_a),
db=database) InitArg('db', database))
""" :type: (objects.Provider) -> B """ """ :type: (objects.Provider) -> B """
# Catalog injection into consumer class. # Catalog injection into consumer class.
class Consumer(object): class Consumer(object):
catalog = Catalog(Catalog.object_a, catalog = AppCatalog(AppCatalog.object_a,
Catalog.object_b) AppCatalog.object_b)
def return_a_b(self): def return_a_b(self):
return (self.catalog.object_a(), return (self.catalog.object_a(),
@ -51,8 +52,8 @@ a1, b1 = Consumer().return_a_b()
# Catalog static provides. # Catalog static provides.
a2 = Catalog.object_a() a2 = AppCatalog.object_a()
b2 = Catalog.object_b() b2 = AppCatalog.object_b()
# Some asserts. # Some asserts.
assert a1 is not a2 assert a1 is not a2

View File

@ -1,61 +0,0 @@
"""
Concept example of objects catalogs.
"""
from objects import Catalog, Singleton, NewInstance, KwArg, Attribute
import sqlite3
# Some example classes.
class A(object):
def __init__(self, db):
self.db = db
class B(object):
def __init__(self, a, db):
self.a = a
self.db = db
# Catalog of objects providers.
class AppCatalog(Catalog):
"""
Objects catalog.
"""
database = Singleton(provides=sqlite3.Connection,
database=KwArg('example.db'),
row_factory=Attribute(sqlite3.Row))
""" :type: (Provider) -> sqlite3.Connection """
object_a = NewInstance(provides=A,
db=KwArg(database))
""" :type: (Provider) -> A """
object_b = NewInstance(provides=B,
a=KwArg(object_a),
db=KwArg(database))
""" :type: (Provider) -> B """
# Catalog injection into consumer class.
class Consumer(object):
catalog = AppCatalog(AppCatalog.object_a,
AppCatalog.object_b)
def return_a_b(self):
return (self.catalog.object_a(),
self.catalog.object_b())
a1, b1 = Consumer().return_a_b()
# Catalog static provides.
a2 = AppCatalog.object_a()
b2 = AppCatalog.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

View File

@ -1,61 +0,0 @@
"""
Concept example of objects catalogs.
"""
from objects import Catalog, Singleton, NewInstance, KwArg, Attribute
import sqlite3
# Some example classes.
class A(object):
def __init__(self, db):
self.db = db
class B(object):
def __init__(self, a, db):
self.a = a
self.db = db
# Catalog of objects providers.
class AppCatalog(Catalog):
"""
Objects catalog.
"""
database = Singleton(sqlite3.Connection,
KwArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row))
""" :type: (objects.Provider) -> sqlite3.Connection """
object_a = NewInstance(A,
KwArg('db', database))
""" :type: (objects.Provider) -> A """
object_b = NewInstance(B,
KwArg('a', object_a),
KwArg('db', database))
""" :type: (objects.Provider) -> B """
# Catalog injection into consumer class.
class Consumer(object):
catalog = AppCatalog(AppCatalog.object_a,
AppCatalog.object_b)
def return_a_b(self):
return (self.catalog.object_a(),
self.catalog.object_b())
a1, b1 = Consumer().return_a_b()
# Catalog static provides.
a2 = AppCatalog.object_a()
b2 = AppCatalog.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

View File

@ -5,7 +5,14 @@
from .catalog import Catalog from .catalog import Catalog
from .std_providers import (Provider, NewInstance, Singleton, Class, Object, from .std_providers import (Provider, NewInstance, Singleton, Class, Object,
Function, Value) Function, Value)
from .injections import InitArg, Attribute, Method
__all__ = ['Catalog', 'Provider', 'NewInstance', 'Singleton', 'Class', __all__ = ['Catalog',
'Object', 'Function', 'Value']
# Providers
'Provider', 'NewInstance', 'Singleton', 'Class',
'Object', 'Function', 'Value',
# Injections
'InitArg', 'Attribute', 'Method']

43
objects/injections.py Normal file
View File

@ -0,0 +1,43 @@
"""
Injections module.
"""
class Injection(object):
"""
Base injection class.
"""
def __init__(self, name, injectable):
"""
Initializer.
"""
self.name = name
self.injectable = injectable
@classmethod
def fetch(cls, injections):
"""
Fetches injections of self type from list.
"""
return tuple([injection
for injection in injections
if isinstance(injection, cls)])
class InitArg(Injection):
"""
Init argument injection.
"""
class Attribute(Injection):
"""
Attribute injection.
"""
class Method(Injection):
"""
Method injection.
"""

View File

@ -2,6 +2,8 @@
Standard providers. Standard providers.
""" """
from .injections import InitArg, Attribute, Method
class Provider(object): class Provider(object):
""" """
@ -14,34 +16,54 @@ class Provider(object):
""" """
raise NotImplementedError() raise NotImplementedError()
@staticmethod
def prepare_injections(injections):
"""
Prepares injections list to injection.
"""
prepared_injections = dict()
for injection in injections:
if isinstance(injection.injectable, Provider):
value = injection.injectable.__call__()
else:
value = injection.injectable
prepared_injections[injection.name] = value
return prepared_injections
class NewInstance(Provider): class NewInstance(Provider):
""" """
New instance providers will create and return new instance on every call. New instance providers will create and return new instance on every call.
""" """
def __init__(self, provides, **dependencies): def __init__(self, provides, *injections):
""" """
Initializer. Initializer.
""" """
self.provides = provides self.provides = provides
self.dependencies = dependencies self.init_injections = InitArg.fetch(injections)
self.attribute_injections = Attribute.fetch(injections)
self.method_injections = Method.fetch(injections)
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
""" """
Returns provided instance. Returns provided instance.
""" """
for name, dependency in self.dependencies.iteritems(): init_injections = Provider.prepare_injections(self.init_injections)
if name in kwargs: init_injections.update(kwargs)
continue
if isinstance(dependency, Provider): provided = self.provides(*args, **init_injections)
value = dependency.__call__()
else:
value = dependency
kwargs[name] = value attribute_injections = Provider.prepare_injections(
return self.provides(*args, **kwargs) self.attribute_injections)
for name, injectable in attribute_injections.iteritems():
setattr(provided, name, injectable)
method_injections = Provider.prepare_injections(self.method_injections)
for name, injectable in method_injections.iteritems():
getattr(provided, name)(injectable)
return provided
class Singleton(NewInstance): class Singleton(NewInstance):