mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-12 01:20:51 +03:00
Update readme and services miniapp example
This commit is contained in:
parent
6881d370ae
commit
a2843a974a
104
README.rst
104
README.rst
|
@ -123,36 +123,41 @@ several IoC containers for some example application:
|
||||||
import dependency_injector.providers as providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Core(containers.DeclarativeContainer):
|
||||||
"""IoC container of platform service providers."""
|
"""IoC container of core component providers."""
|
||||||
|
|
||||||
configuration = providers.Configuration('config')
|
configuration = providers.Configuration('config')
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger, name='example')
|
logger = providers.Singleton(logging.Logger, name='example')
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect, configuration.database.dsn)
|
|
||||||
|
class Gateways(containers.DeclarativeContainer):
|
||||||
|
"""IoC container of gateway (API clients to remote services) providers."""
|
||||||
|
|
||||||
|
database = providers.Singleton(sqlite3.connect,
|
||||||
|
Core.configuration.database.dsn)
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
||||||
configuration.aws.access_key_id,
|
Core.configuration.aws.access_key_id,
|
||||||
configuration.aws.secret_access_key)
|
Core.configuration.aws.secret_access_key)
|
||||||
|
|
||||||
|
|
||||||
class Services(containers.DeclarativeContainer):
|
class Services(containers.DeclarativeContainer):
|
||||||
"""IoC container of business service providers."""
|
"""IoC container of business service providers."""
|
||||||
|
|
||||||
users = providers.Factory(example.services.UsersService,
|
users = providers.Factory(example.services.UsersService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database)
|
logger=Core.logger)
|
||||||
|
|
||||||
auth = providers.Factory(example.services.AuthService,
|
auth = providers.Factory(example.services.AuthService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database,
|
logger=Core.logger,
|
||||||
token_ttl=Platform.configuration.auth.token_ttl)
|
token_ttl=Core.configuration.auth.token_ttl)
|
||||||
|
|
||||||
photos = providers.Factory(example.services.PhotosService,
|
photos = providers.Factory(example.services.PhotosService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database,
|
s3=Gateways.s3,
|
||||||
s3=Platform.s3)
|
logger=Core.logger)
|
||||||
|
|
||||||
|
|
||||||
class Application(containers.DeclarativeContainer):
|
class Application(containers.DeclarativeContainer):
|
||||||
|
@ -172,83 +177,22 @@ Next example demonstrates run of example application defined above:
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from containers import Platform, Application
|
from containers import Core, Application
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Configure platform:
|
# Configure platform:
|
||||||
Platform.configuration.update({'database': {'dsn': ':memory:'},
|
Core.configuration.update({'database': {'dsn': ':memory:'},
|
||||||
'aws': {'access_key_id': 'KEY',
|
'aws': {'access_key_id': 'KEY',
|
||||||
'secret_access_key': 'SECRET'},
|
'secret_access_key': 'SECRET'},
|
||||||
'auth': {'token_ttl': 3600}})
|
'auth': {'token_ttl': 3600}})
|
||||||
Platform.logger().addHandler(logging.StreamHandler(sys.stdout))
|
Core.logger().addHandler(logging.StreamHandler(sys.stdout))
|
||||||
|
|
||||||
# Run application:
|
# Run application:
|
||||||
Application.main(uid=sys.argv[1],
|
Application.main(uid=sys.argv[1],
|
||||||
password=sys.argv[2],
|
password=sys.argv[2],
|
||||||
photo=sys.argv[3])
|
photo=sys.argv[3])
|
||||||
|
|
||||||
# Previous call is an equivalent of next operations:
|
|
||||||
#
|
|
||||||
# logger = logging.Logger(name='example')
|
|
||||||
# database = sqlite3.connect(':memory:')
|
|
||||||
# s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY',
|
|
||||||
# aws_secret_access_key='SECRET')
|
|
||||||
#
|
|
||||||
# example.main.main(
|
|
||||||
# uid=sys.argv[1],
|
|
||||||
# password=sys.argv[2],
|
|
||||||
# photo=sys.argv[3],
|
|
||||||
# users_service=example.services.UsersService(logger=logger,
|
|
||||||
# db=database),
|
|
||||||
# auth_service=example.services.AuthService(logger=logger,
|
|
||||||
# db=database,
|
|
||||||
# token_ttl=3600),
|
|
||||||
# photos_service=example.services.PhotosService(logger=logger,
|
|
||||||
# db=database,
|
|
||||||
# s3=s3))
|
|
||||||
|
|
||||||
Alternative definition styles of providers
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
*Dependecy Injector* supports few other styles of dependency injections
|
|
||||||
definition.
|
|
||||||
|
|
||||||
IoC containers from previous example could look like these:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of platform service providers."""
|
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger) \
|
|
||||||
.add_kwargs(name='example')
|
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect) \
|
|
||||||
.add_args(':memory:')
|
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection) \
|
|
||||||
.add_kwargs(aws_access_key_id='KEY',
|
|
||||||
aws_secret_access_key='SECRET')
|
|
||||||
|
|
||||||
or like this these:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of platform service providers."""
|
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger)
|
|
||||||
logger.add_kwargs(name='example')
|
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect)
|
|
||||||
database.add_args(':memory:')
|
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection)
|
|
||||||
s3.add_kwargs(aws_access_key_id='KEY',
|
|
||||||
aws_secret_access_key='SECRET')
|
|
||||||
|
|
||||||
|
|
||||||
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
||||||
GitHub:
|
GitHub:
|
||||||
|
|
||||||
|
|
|
@ -12,36 +12,41 @@ import dependency_injector.containers as containers
|
||||||
import dependency_injector.providers as providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Core(containers.DeclarativeContainer):
|
||||||
"""IoC container of platform service providers."""
|
"""IoC container of core component providers."""
|
||||||
|
|
||||||
configuration = providers.Configuration('config')
|
configuration = providers.Configuration('config')
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger, name='example')
|
logger = providers.Singleton(logging.Logger, name='example')
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect, configuration.database.dsn)
|
|
||||||
|
class Gateways(containers.DeclarativeContainer):
|
||||||
|
"""IoC container of gateway (API clients to remote services) providers."""
|
||||||
|
|
||||||
|
database = providers.Singleton(sqlite3.connect,
|
||||||
|
Core.configuration.database.dsn)
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
||||||
configuration.aws.access_key_id,
|
Core.configuration.aws.access_key_id,
|
||||||
configuration.aws.secret_access_key)
|
Core.configuration.aws.secret_access_key)
|
||||||
|
|
||||||
|
|
||||||
class Services(containers.DeclarativeContainer):
|
class Services(containers.DeclarativeContainer):
|
||||||
"""IoC container of business service providers."""
|
"""IoC container of business service providers."""
|
||||||
|
|
||||||
users = providers.Factory(example.services.UsersService,
|
users = providers.Factory(example.services.UsersService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database)
|
logger=Core.logger)
|
||||||
|
|
||||||
auth = providers.Factory(example.services.AuthService,
|
auth = providers.Factory(example.services.AuthService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database,
|
logger=Core.logger,
|
||||||
token_ttl=Platform.configuration.auth.token_ttl)
|
token_ttl=Core.configuration.auth.token_ttl)
|
||||||
|
|
||||||
photos = providers.Factory(example.services.PhotosService,
|
photos = providers.Factory(example.services.PhotosService,
|
||||||
logger=Platform.logger,
|
db=Gateways.database,
|
||||||
db=Platform.database,
|
s3=Gateways.s3,
|
||||||
s3=Platform.s3)
|
logger=Core.logger)
|
||||||
|
|
||||||
|
|
||||||
class Application(containers.DeclarativeContainer):
|
class Application(containers.DeclarativeContainer):
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
"""Example of dependency injection in Python.
|
|
||||||
|
|
||||||
Alternative injections definition style #1.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
import boto.s3.connection
|
|
||||||
|
|
||||||
import example.main
|
|
||||||
import example.services
|
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
|
||||||
import dependency_injector.providers as providers
|
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of platform service providers."""
|
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger) \
|
|
||||||
.add_kwargs(name='example')
|
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect) \
|
|
||||||
.add_args(':memory:')
|
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection) \
|
|
||||||
.add_kwargs(aws_access_key_id='KEY',
|
|
||||||
aws_secret_access_key='SECRET')
|
|
||||||
|
|
||||||
|
|
||||||
class Services(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of business service providers."""
|
|
||||||
|
|
||||||
users = providers.Factory(example.services.UsersService) \
|
|
||||||
.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database)
|
|
||||||
|
|
||||||
auth = providers.Factory(example.services.AuthService) \
|
|
||||||
.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database,
|
|
||||||
token_ttl=3600)
|
|
||||||
|
|
||||||
photos = providers.Factory(example.services.PhotosService) \
|
|
||||||
.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database,
|
|
||||||
s3=Platform.s3)
|
|
||||||
|
|
||||||
|
|
||||||
class Application(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of application component providers."""
|
|
||||||
|
|
||||||
main = providers.Callable(example.main.main) \
|
|
||||||
.add_kwargs(users_service=Services.users,
|
|
||||||
auth_service=Services.auth,
|
|
||||||
photos_service=Services.photos)
|
|
|
@ -1,56 +0,0 @@
|
||||||
"""Example of dependency injection in Python.
|
|
||||||
|
|
||||||
Alternative injections definition style #2.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
import boto.s3.connection
|
|
||||||
|
|
||||||
import example.main
|
|
||||||
import example.services
|
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
|
||||||
import dependency_injector.providers as providers
|
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of platform service providers."""
|
|
||||||
|
|
||||||
logger = providers.Singleton(logging.Logger)
|
|
||||||
logger.add_kwargs(name='example')
|
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect)
|
|
||||||
database.add_args(':memory:')
|
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection)
|
|
||||||
s3.add_kwargs(aws_access_key_id='KEY',
|
|
||||||
aws_secret_access_key='SECRET')
|
|
||||||
|
|
||||||
|
|
||||||
class Services(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of business service providers."""
|
|
||||||
|
|
||||||
users = providers.Factory(example.services.UsersService)
|
|
||||||
users.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database)
|
|
||||||
|
|
||||||
auth = providers.Factory(example.services.AuthService)
|
|
||||||
auth.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database,
|
|
||||||
token_ttl=3600)
|
|
||||||
|
|
||||||
photos = providers.Factory(example.services.PhotosService)
|
|
||||||
photos.add_kwargs(logger=Platform.logger,
|
|
||||||
db=Platform.database,
|
|
||||||
s3=Platform.s3)
|
|
||||||
|
|
||||||
|
|
||||||
class Application(containers.DeclarativeContainer):
|
|
||||||
"""IoC container of application component providers."""
|
|
||||||
|
|
||||||
main = providers.Callable(example.main.main)
|
|
||||||
main.add_kwargs(users_service=Services.users,
|
|
||||||
auth_service=Services.auth,
|
|
||||||
photos_service=Services.photos)
|
|
|
@ -3,38 +3,18 @@
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from containers import Platform, Application
|
from containers import Core, Application
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Configure platform:
|
# Configure platform:
|
||||||
Platform.configuration.update({'database': {'dsn': ':memory:'},
|
Core.configuration.update({'database': {'dsn': ':memory:'},
|
||||||
'aws': {'access_key_id': 'KEY',
|
'aws': {'access_key_id': 'KEY',
|
||||||
'secret_access_key': 'SECRET'},
|
'secret_access_key': 'SECRET'},
|
||||||
'auth': {'token_ttl': 3600}})
|
'auth': {'token_ttl': 3600}})
|
||||||
Platform.logger().addHandler(logging.StreamHandler(sys.stdout))
|
Core.logger().addHandler(logging.StreamHandler(sys.stdout))
|
||||||
|
|
||||||
# Run application:
|
# Run application:
|
||||||
Application.main(uid=sys.argv[1],
|
Application.main(uid=sys.argv[1],
|
||||||
password=sys.argv[2],
|
password=sys.argv[2],
|
||||||
photo=sys.argv[3])
|
photo=sys.argv[3])
|
||||||
|
|
||||||
# Previous call is an equivalent of next operations:
|
|
||||||
#
|
|
||||||
# logger = logging.Logger(name='example')
|
|
||||||
# database = sqlite3.connect(':memory:')
|
|
||||||
# s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY',
|
|
||||||
# aws_secret_access_key='SECRET')
|
|
||||||
#
|
|
||||||
# example.main.main(
|
|
||||||
# uid=sys.argv[1],
|
|
||||||
# password=sys.argv[2],
|
|
||||||
# photo=sys.argv[3],
|
|
||||||
# users_service=example.services.UsersService(logger=logger,
|
|
||||||
# db=database),
|
|
||||||
# auth_service=example.services.AuthService(logger=logger,
|
|
||||||
# db=database,
|
|
||||||
# token_ttl=3600),
|
|
||||||
# photos_service=example.services.PhotosService(logger=logger,
|
|
||||||
# db=database,
|
|
||||||
# s3=s3))
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user