mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 03:36:41 +03:00
Drop catalog examples
This commit is contained in:
parent
5361694272
commit
68ae1b80df
|
@ -1,22 +0,0 @@
|
||||||
"""Declarative catalog simple example."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
# Defining declarative catalog:
|
|
||||||
class Catalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Providers catalog."""
|
|
||||||
|
|
||||||
factory1 = providers.Factory(object)
|
|
||||||
|
|
||||||
factory2 = providers.Factory(object)
|
|
||||||
|
|
||||||
# Creating some objects:
|
|
||||||
object1 = Catalog.factory1()
|
|
||||||
object2 = Catalog.factory2()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert object1 is not object2
|
|
||||||
assert isinstance(object1, object)
|
|
||||||
assert isinstance(object2, object)
|
|
|
@ -1,30 +0,0 @@
|
||||||
"""Declarative catalogs inheritance example."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
class CatalogA(catalogs.DeclarativeCatalog):
|
|
||||||
"""Example catalog A."""
|
|
||||||
|
|
||||||
provider1 = providers.Factory(object)
|
|
||||||
|
|
||||||
|
|
||||||
class CatalogB(CatalogA):
|
|
||||||
"""Example catalog B."""
|
|
||||||
|
|
||||||
provider2 = providers.Singleton(object)
|
|
||||||
|
|
||||||
|
|
||||||
# Making some asserts for `providers` attribute:
|
|
||||||
assert CatalogA.providers == dict(provider1=CatalogA.provider1)
|
|
||||||
assert CatalogB.providers == dict(provider1=CatalogA.provider1,
|
|
||||||
provider2=CatalogB.provider2)
|
|
||||||
|
|
||||||
# Making some asserts for `cls_providers` attribute:
|
|
||||||
assert CatalogA.cls_providers == dict(provider1=CatalogA.provider1)
|
|
||||||
assert CatalogB.cls_providers == dict(provider2=CatalogB.provider2)
|
|
||||||
|
|
||||||
# Making some asserts for `inherited_providers` attribute:
|
|
||||||
assert CatalogA.inherited_providers == dict()
|
|
||||||
assert CatalogB.inherited_providers == dict(provider1=CatalogA.provider1)
|
|
|
@ -1,47 +0,0 @@
|
||||||
"""Declarative catalog's provider injections example."""
|
|
||||||
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
class UsersService(object):
|
|
||||||
"""Users service, that has dependency on database."""
|
|
||||||
|
|
||||||
def __init__(self, db):
|
|
||||||
"""Initializer."""
|
|
||||||
self.db = db
|
|
||||||
|
|
||||||
|
|
||||||
class AuthService(object):
|
|
||||||
"""Auth service, that has dependencies on users service and database."""
|
|
||||||
|
|
||||||
def __init__(self, db, users_service):
|
|
||||||
"""Initializer."""
|
|
||||||
self.db = db
|
|
||||||
self.users_service = users_service
|
|
||||||
|
|
||||||
|
|
||||||
class Services(catalogs.DeclarativeCatalog):
|
|
||||||
"""Catalog of service providers."""
|
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect, ':memory:')
|
|
||||||
|
|
||||||
users = providers.Factory(UsersService,
|
|
||||||
db=database)
|
|
||||||
|
|
||||||
auth = providers.Factory(AuthService,
|
|
||||||
db=database,
|
|
||||||
users_service=users)
|
|
||||||
|
|
||||||
|
|
||||||
# Retrieving service providers from catalog:
|
|
||||||
users_service = Services.users()
|
|
||||||
auth_service = Services.auth()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert users_service.db is auth_service.db is Services.database()
|
|
||||||
assert isinstance(auth_service.users_service, UsersService)
|
|
||||||
assert users_service is not Services.users()
|
|
||||||
assert auth_service is not Services.auth()
|
|
|
@ -1,73 +0,0 @@
|
||||||
"""Specialized declarative catalog example."""
|
|
||||||
|
|
||||||
import services
|
|
||||||
|
|
||||||
from dependency_injector import providers
|
|
||||||
from dependency_injector import errors
|
|
||||||
|
|
||||||
|
|
||||||
class UsersService(services.Base):
|
|
||||||
"""Users service."""
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
"""Initializer."""
|
|
||||||
self.config = config
|
|
||||||
super(UsersService, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
class AuthService(services.Base):
|
|
||||||
"""Auth service."""
|
|
||||||
|
|
||||||
def __init__(self, config, users_service):
|
|
||||||
"""Initializer."""
|
|
||||||
self.config = config
|
|
||||||
self.users_service = users_service
|
|
||||||
super(AuthService, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
class Services(services.Catalog):
|
|
||||||
"""Services catalog."""
|
|
||||||
|
|
||||||
users = services.Provider(UsersService,
|
|
||||||
config={'option1': '111',
|
|
||||||
'option2': '222'})
|
|
||||||
|
|
||||||
auth = services.Provider(AuthService,
|
|
||||||
config={'option3': '333',
|
|
||||||
'option4': '444'},
|
|
||||||
users_service=users)
|
|
||||||
|
|
||||||
|
|
||||||
# Creating users & auth services:
|
|
||||||
users_service = Services.users()
|
|
||||||
auth_service = Services.auth()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert users_service.config == {'option1': '111',
|
|
||||||
'option2': '222'}
|
|
||||||
assert auth_service.config == {'option3': '333',
|
|
||||||
'option4': '444'}
|
|
||||||
assert isinstance(auth_service.users_service, UsersService)
|
|
||||||
|
|
||||||
# Trying to declare services catalog with other provider type:
|
|
||||||
try:
|
|
||||||
class Services1(services.Catalog):
|
|
||||||
"""Services catalog."""
|
|
||||||
|
|
||||||
users = providers.Factory(UsersService)
|
|
||||||
except errors.Error as exception:
|
|
||||||
print exception
|
|
||||||
# <__main__.Services1()> can contain only <class 'services.Provider'>
|
|
||||||
# instances
|
|
||||||
|
|
||||||
# Trying to declare services catalog with correct provider by invalid provided
|
|
||||||
# type:
|
|
||||||
try:
|
|
||||||
class Services2(services.Catalog):
|
|
||||||
"""Services catalog."""
|
|
||||||
|
|
||||||
users = services.Provider(object)
|
|
||||||
except errors.Error as exception:
|
|
||||||
print exception
|
|
||||||
# <class 'services.Provider'> can provide only <class 'services.Base'>
|
|
||||||
# instances
|
|
|
@ -1,26 +0,0 @@
|
||||||
"""Base classes for services."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
class Base(object):
|
|
||||||
"""Base service class."""
|
|
||||||
|
|
||||||
|
|
||||||
class Provider(providers.Factory):
|
|
||||||
"""Service provider.
|
|
||||||
|
|
||||||
Can provide :py:class:`Base` only.
|
|
||||||
"""
|
|
||||||
|
|
||||||
provided_type = Base
|
|
||||||
|
|
||||||
|
|
||||||
class Catalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Base catalog of services.
|
|
||||||
|
|
||||||
Can include :py:class:`Provider`'s only.
|
|
||||||
"""
|
|
||||||
|
|
||||||
provider_type = Provider
|
|
|
@ -1,18 +0,0 @@
|
||||||
"""Dynamic catalog simple example."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
# Defining dynamic catalog:
|
|
||||||
catalog = catalogs.DynamicCatalog(factory1=providers.Factory(object),
|
|
||||||
factory2=providers.Factory(object))
|
|
||||||
|
|
||||||
# Creating some objects:
|
|
||||||
object1 = catalog.factory1()
|
|
||||||
object2 = catalog.factory2()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert object1 is not object2
|
|
||||||
assert isinstance(object1, object)
|
|
||||||
assert isinstance(object2, object)
|
|
|
@ -1,63 +0,0 @@
|
||||||
"""Specialized dynamic catalog example."""
|
|
||||||
|
|
||||||
import services
|
|
||||||
|
|
||||||
from dependency_injector import providers
|
|
||||||
from dependency_injector import errors
|
|
||||||
|
|
||||||
|
|
||||||
class UsersService(services.Base):
|
|
||||||
"""Users service."""
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
"""Initializer."""
|
|
||||||
self.config = config
|
|
||||||
super(UsersService, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
class AuthService(services.Base):
|
|
||||||
"""Auth service."""
|
|
||||||
|
|
||||||
def __init__(self, config, users_service):
|
|
||||||
"""Initializer."""
|
|
||||||
self.config = config
|
|
||||||
self.users_service = users_service
|
|
||||||
super(AuthService, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
services_catalog = services.Catalog()
|
|
||||||
services_catalog.users = services.Provider(UsersService,
|
|
||||||
config={'option1': '111',
|
|
||||||
'option2': '222'})
|
|
||||||
services_catalog.auth = services.Provider(AuthService,
|
|
||||||
config={'option3': '333',
|
|
||||||
'option4': '444'},
|
|
||||||
users_service=services_catalog.users)
|
|
||||||
|
|
||||||
# Creating users & auth services:
|
|
||||||
users_service = services_catalog.users()
|
|
||||||
auth_service = services_catalog.auth()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert users_service.config == {'option1': '111',
|
|
||||||
'option2': '222'}
|
|
||||||
assert auth_service.config == {'option3': '333',
|
|
||||||
'option4': '444'}
|
|
||||||
assert isinstance(auth_service.users_service, UsersService)
|
|
||||||
|
|
||||||
# Trying to declare services catalog with other provider type:
|
|
||||||
try:
|
|
||||||
services_catalog.users = providers.Factory(UsersService)
|
|
||||||
except errors.Error as exception:
|
|
||||||
print exception
|
|
||||||
# <services.Catalog(users, auth)> can contain only
|
|
||||||
# <class 'services.Provider'> instances
|
|
||||||
|
|
||||||
# Trying to declare services catalog with correct provider by invalid provided
|
|
||||||
# type:
|
|
||||||
try:
|
|
||||||
services_catalog.users = services.Provider(object)
|
|
||||||
except errors.Error as exception:
|
|
||||||
print exception
|
|
||||||
# <class 'services.Provider'> can provide only <class 'services.Base'>
|
|
||||||
# instances
|
|
|
@ -1,26 +0,0 @@
|
||||||
"""Base classes for services."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
class Base(object):
|
|
||||||
"""Base service class."""
|
|
||||||
|
|
||||||
|
|
||||||
class Provider(providers.Factory):
|
|
||||||
"""Service provider.
|
|
||||||
|
|
||||||
Can provide :py:class:`Base` only.
|
|
||||||
"""
|
|
||||||
|
|
||||||
provided_type = Base
|
|
||||||
|
|
||||||
|
|
||||||
class Catalog(catalogs.DynamicCatalog):
|
|
||||||
"""Base catalog of services.
|
|
||||||
|
|
||||||
Can include :py:class:`Provider`'s only.
|
|
||||||
"""
|
|
||||||
|
|
||||||
provider_type = Provider
|
|
|
@ -1,66 +0,0 @@
|
||||||
"""Dynamic catalog creation and runtime filling of it example."""
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
|
|
||||||
|
|
||||||
# Defining several example services:
|
|
||||||
class UsersService(object):
|
|
||||||
"""Example users service."""
|
|
||||||
|
|
||||||
|
|
||||||
class AuthService(object):
|
|
||||||
"""Example auth service."""
|
|
||||||
|
|
||||||
|
|
||||||
def import_cls(cls_name):
|
|
||||||
"""Import class by its fully qualified name.
|
|
||||||
|
|
||||||
In terms of current example it is just a small helper function. Please,
|
|
||||||
don't use it in production approaches.
|
|
||||||
"""
|
|
||||||
path_components = cls_name.split('.')
|
|
||||||
if len(path_components) == 1:
|
|
||||||
path_components.insert(0, '__main__')
|
|
||||||
module = __import__('.'.join(path_components[0:-1]),
|
|
||||||
locals(),
|
|
||||||
globals(),
|
|
||||||
fromlist=path_components[-1:])
|
|
||||||
return getattr(module, path_components[-1])
|
|
||||||
|
|
||||||
|
|
||||||
# "Parsing" some configuration:
|
|
||||||
config = {
|
|
||||||
'services': {
|
|
||||||
'users': {
|
|
||||||
'class': 'UsersService',
|
|
||||||
'provider_class': 'dependency_injector.providers.Factory',
|
|
||||||
},
|
|
||||||
'auth': {
|
|
||||||
'class': 'AuthService',
|
|
||||||
'provider_class': 'dependency_injector.providers.Factory',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Defining dynamic service providers catalog:
|
|
||||||
services = catalogs.DynamicCatalog()
|
|
||||||
|
|
||||||
# Filling dynamic service providers catalog according to the configuration:
|
|
||||||
for service_name, service_info in config['services'].iteritems():
|
|
||||||
# Runtime importing of service and service provider classes:
|
|
||||||
service_cls = import_cls(service_info['class'])
|
|
||||||
service_provider_cls = import_cls(service_info['provider_class'])
|
|
||||||
|
|
||||||
# Creating service provider:
|
|
||||||
service_provider = service_provider_cls(service_cls)
|
|
||||||
|
|
||||||
# Binding service provider to the dynamic service providers catalog:
|
|
||||||
services.bind_provider(service_name, service_provider)
|
|
||||||
|
|
||||||
# Creating some objects:
|
|
||||||
users_service = services.users()
|
|
||||||
auth_service = services.auth()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert isinstance(users_service, UsersService)
|
|
||||||
assert isinstance(auth_service, AuthService)
|
|
|
@ -1,45 +0,0 @@
|
||||||
"""Declarative catalog overriding example."""
|
|
||||||
|
|
||||||
import collections
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
# Creating some example classes:
|
|
||||||
Object1 = collections.namedtuple('Object1', ['arg1', 'arg2'])
|
|
||||||
Object2 = collections.namedtuple('Object2', ['object1'])
|
|
||||||
ExtendedObject2 = collections.namedtuple('ExtendedObject2', [])
|
|
||||||
|
|
||||||
|
|
||||||
class Catalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Catalog of some providers."""
|
|
||||||
|
|
||||||
object1_factory = providers.Factory(Object1,
|
|
||||||
arg1=1,
|
|
||||||
arg2=2)
|
|
||||||
|
|
||||||
object2_factory = providers.Factory(Object2,
|
|
||||||
object1=object1_factory)
|
|
||||||
|
|
||||||
|
|
||||||
class AnotherCatalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Overriding catalog."""
|
|
||||||
|
|
||||||
object2_factory = providers.Factory(ExtendedObject2)
|
|
||||||
|
|
||||||
|
|
||||||
# Overriding `Catalog` with `AnotherCatalog`:
|
|
||||||
Catalog.override(AnotherCatalog)
|
|
||||||
|
|
||||||
# Creating some objects using overridden catalog:
|
|
||||||
object2_1 = Catalog.object2_factory()
|
|
||||||
object2_2 = Catalog.object2_factory()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert Catalog.is_overridden
|
|
||||||
|
|
||||||
assert object2_1 is not object2_2
|
|
||||||
|
|
||||||
assert isinstance(object2_1, ExtendedObject2)
|
|
||||||
assert isinstance(object2_2, ExtendedObject2)
|
|
|
@ -1,41 +0,0 @@
|
||||||
"""Declarative catalog overriding by dynamic catalog example."""
|
|
||||||
|
|
||||||
import collections
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
|
|
||||||
# Creating some example classes:
|
|
||||||
Object1 = collections.namedtuple('Object1', ['arg1', 'arg2'])
|
|
||||||
Object2 = collections.namedtuple('Object2', ['object1'])
|
|
||||||
ExtendedObject2 = collections.namedtuple('ExtendedObject2', [])
|
|
||||||
|
|
||||||
|
|
||||||
class Catalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Catalog of some providers."""
|
|
||||||
|
|
||||||
object1_factory = providers.Factory(Object1,
|
|
||||||
arg1=1,
|
|
||||||
arg2=2)
|
|
||||||
|
|
||||||
object2_factory = providers.Factory(Object2,
|
|
||||||
object1=object1_factory)
|
|
||||||
|
|
||||||
|
|
||||||
# Overriding `Catalog` with some `DynamicCatalog` instance:
|
|
||||||
overriding_catalog = catalogs.DynamicCatalog(
|
|
||||||
object2_factory=providers.Factory(ExtendedObject2))
|
|
||||||
Catalog.override(overriding_catalog)
|
|
||||||
|
|
||||||
# Creating some objects using overridden catalog:
|
|
||||||
object2_1 = Catalog.object2_factory()
|
|
||||||
object2_2 = Catalog.object2_factory()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert Catalog.is_overridden
|
|
||||||
|
|
||||||
assert object2_1 is not object2_2
|
|
||||||
|
|
||||||
assert isinstance(object2_1, ExtendedObject2)
|
|
||||||
assert isinstance(object2_2, ExtendedObject2)
|
|
|
@ -1,43 +0,0 @@
|
||||||
"""Declarative catalog overriding using `@override()` decorator example."""
|
|
||||||
|
|
||||||
import collections
|
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
|
||||||
from dependency_injector import providers
|
|
||||||
|
|
||||||
# Creating some example classes:
|
|
||||||
Object1 = collections.namedtuple('Object1', ['arg1', 'arg2'])
|
|
||||||
Object2 = collections.namedtuple('Object2', ['object1'])
|
|
||||||
ExtendedObject2 = collections.namedtuple('ExtendedObject2', [])
|
|
||||||
|
|
||||||
|
|
||||||
class Catalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Catalog of some providers."""
|
|
||||||
|
|
||||||
object1_factory = providers.Factory(Object1,
|
|
||||||
arg1=1,
|
|
||||||
arg2=2)
|
|
||||||
|
|
||||||
object2_factory = providers.Factory(Object2,
|
|
||||||
object1=object1_factory)
|
|
||||||
|
|
||||||
|
|
||||||
# Overriding `Catalog` with `AnotherCatalog`:
|
|
||||||
@catalogs.override(Catalog)
|
|
||||||
class AnotherCatalog(catalogs.DeclarativeCatalog):
|
|
||||||
"""Overriding catalog."""
|
|
||||||
|
|
||||||
object2_factory = providers.Factory(ExtendedObject2)
|
|
||||||
|
|
||||||
|
|
||||||
# Creating some objects using overridden catalog:
|
|
||||||
object2_1 = Catalog.object2_factory()
|
|
||||||
object2_2 = Catalog.object2_factory()
|
|
||||||
|
|
||||||
# Making some asserts:
|
|
||||||
assert Catalog.is_overridden
|
|
||||||
|
|
||||||
assert object2_1 is not object2_2
|
|
||||||
|
|
||||||
assert isinstance(object2_1, ExtendedObject2)
|
|
||||||
assert isinstance(object2_2, ExtendedObject2)
|
|
Loading…
Reference in New Issue
Block a user