python-dependency-injector/examples/miniapps/services/containers.py

52 lines
1.7 KiB
Python
Raw Normal View History

2016-10-06 22:48:43 +03:00
"""Example of dependency injection in Python."""
2016-05-18 23:18:29 +03:00
2016-10-06 22:48:43 +03:00
import logging
2016-05-18 23:18:29 +03:00
import sqlite3
2016-10-06 22:48:43 +03:00
2016-05-18 23:18:29 +03:00
import boto.s3.connection
2016-09-18 23:00:10 +03:00
import example.main
2016-05-18 23:18:29 +03:00
import example.services
2016-06-01 19:52:10 +03:00
import dependency_injector.containers as containers
import dependency_injector.providers as providers
2016-05-18 23:18:29 +03:00
class Platform(containers.DeclarativeContainer):
"""IoC container of platform service providers."""
2016-05-18 23:18:29 +03:00
2016-10-06 22:48:43 +03:00
logger = providers.Singleton(logging.Logger, name='example')
2016-05-18 23:18:29 +03:00
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(containers.DeclarativeContainer):
"""IoC container of business service providers."""
2016-05-18 23:18:29 +03:00
users = providers.Factory(example.services.Users,
2016-10-06 22:48:43 +03:00
logger=Platform.logger,
2016-05-18 23:18:29 +03:00
db=Platform.database)
auth = providers.Factory(example.services.Auth,
2016-10-06 22:48:43 +03:00
logger=Platform.logger,
2016-05-18 23:18:29 +03:00
db=Platform.database,
token_ttl=3600)
2016-06-01 19:52:10 +03:00
photos = providers.Factory(example.services.Photos,
2016-10-06 22:48:43 +03:00
logger=Platform.logger,
2016-06-01 19:52:10 +03:00
db=Platform.database,
s3=Platform.s3)
2016-09-18 23:00:10 +03:00
class Application(containers.DeclarativeContainer):
"""IoC container of application component providers."""
main = providers.Callable(example.main.main,
users_service=Services.users,
auth_service=Services.auth,
photos_service=Services.photos)