mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
updating concept
This commit is contained in:
parent
80e2a1fdf1
commit
5b9723c530
|
@ -2,7 +2,7 @@
|
|||
Concept example of objects catalogs.
|
||||
"""
|
||||
|
||||
import objects
|
||||
from objects import Catalog, Singleton, NewInstance, InitArg, Attribute
|
||||
import sqlite3
|
||||
|
||||
|
||||
|
@ -19,29 +19,30 @@ class B(object):
|
|||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(objects.Catalog):
|
||||
class AppCatalog(Catalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
database = objects.Singleton(provides=sqlite3.Connection,
|
||||
database='example.db')
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
|
||||
object_a = objects.NewInstance(provides=A,
|
||||
db=database)
|
||||
object_a = NewInstance(A,
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> A """
|
||||
|
||||
object_b = objects.NewInstance(provides=B,
|
||||
a=object_a,
|
||||
db=database)
|
||||
object_b = NewInstance(B,
|
||||
InitArg('a', object_a),
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> B """
|
||||
|
||||
|
||||
# Catalog injection into consumer class.
|
||||
class Consumer(object):
|
||||
catalog = Catalog(Catalog.object_a,
|
||||
Catalog.object_b)
|
||||
catalog = AppCatalog(AppCatalog.object_a,
|
||||
AppCatalog.object_b)
|
||||
|
||||
def return_a_b(self):
|
||||
return (self.catalog.object_a(),
|
||||
|
@ -51,8 +52,8 @@ a1, b1 = Consumer().return_a_b()
|
|||
|
||||
|
||||
# Catalog static provides.
|
||||
a2 = Catalog.object_a()
|
||||
b2 = Catalog.object_b()
|
||||
a2 = AppCatalog.object_a()
|
||||
b2 = AppCatalog.object_b()
|
||||
|
||||
# Some asserts.
|
||||
assert a1 is not a2
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -5,7 +5,14 @@
|
|||
from .catalog import Catalog
|
||||
from .std_providers import (Provider, NewInstance, Singleton, Class, Object,
|
||||
Function, Value)
|
||||
from .injections import InitArg, Attribute, Method
|
||||
|
||||
|
||||
__all__ = ['Catalog', 'Provider', 'NewInstance', 'Singleton', 'Class',
|
||||
'Object', 'Function', 'Value']
|
||||
__all__ = ['Catalog',
|
||||
|
||||
# Providers
|
||||
'Provider', 'NewInstance', 'Singleton', 'Class',
|
||||
'Object', 'Function', 'Value',
|
||||
|
||||
# Injections
|
||||
'InitArg', 'Attribute', 'Method']
|
||||
|
|
43
objects/injections.py
Normal file
43
objects/injections.py
Normal 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.
|
||||
"""
|
|
@ -2,6 +2,8 @@
|
|||
Standard providers.
|
||||
"""
|
||||
|
||||
from .injections import InitArg, Attribute, Method
|
||||
|
||||
|
||||
class Provider(object):
|
||||
"""
|
||||
|
@ -14,34 +16,54 @@ class Provider(object):
|
|||
"""
|
||||
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):
|
||||
"""
|
||||
New instance providers will create and return new instance on every call.
|
||||
"""
|
||||
|
||||
def __init__(self, provides, **dependencies):
|
||||
def __init__(self, provides, *injections):
|
||||
"""
|
||||
Initializer.
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
Returns provided instance.
|
||||
"""
|
||||
for name, dependency in self.dependencies.iteritems():
|
||||
if name in kwargs:
|
||||
continue
|
||||
init_injections = Provider.prepare_injections(self.init_injections)
|
||||
init_injections.update(kwargs)
|
||||
|
||||
if isinstance(dependency, Provider):
|
||||
value = dependency.__call__()
|
||||
else:
|
||||
value = dependency
|
||||
provided = self.provides(*args, **init_injections)
|
||||
|
||||
kwargs[name] = value
|
||||
return self.provides(*args, **kwargs)
|
||||
attribute_injections = Provider.prepare_injections(
|
||||
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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user