From 075e1bcf4fa090f203cc71b619812e578e68f659 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 18 May 2016 23:18:29 +0300 Subject: [PATCH] Update services miniapp --- README.rst | 75 ++++++++++++++----- examples/miniapps/services/README.rst | 8 +- examples/miniapps/services/catalogs.py | 33 ++++++++ .../services/catalogs_alt_syntax_1.py | 37 +++++++++ .../services/catalogs_alt_syntax_2.py | 37 +++++++++ examples/miniapps/services/main.py | 50 ++----------- examples/miniapps/services/main2.py | 54 ------------- examples/miniapps/services/main3.py | 54 ------------- 8 files changed, 178 insertions(+), 170 deletions(-) create mode 100644 examples/miniapps/services/catalogs.py create mode 100644 examples/miniapps/services/catalogs_alt_syntax_1.py create mode 100644 examples/miniapps/services/catalogs_alt_syntax_2.py delete mode 100644 examples/miniapps/services/main2.py delete mode 100644 examples/miniapps/services/main3.py diff --git a/README.rst b/README.rst index 05fe1412..b1770d23 100644 --- a/README.rst +++ b/README.rst @@ -55,20 +55,20 @@ Installation Example ------- +Brief example below demonstrates usage of *Dependency Injector* catalogs and +providers for definition of several IoC containers for some microservice +system that consists from several business and platform services: + .. code-block:: python - """Dependency Injector example.""" + """Example of several Dependency Injector catalogs.""" - import sys import sqlite3 - - from boto.s3.connection import S3Connection + import boto.s3.connection + import example.services from dependency_injector import catalogs from dependency_injector import providers - from dependency_injector import injections - - from example import services class Platform(catalogs.DeclarativeCatalog): @@ -76,7 +76,7 @@ Example database = providers.Singleton(sqlite3.connect, ':memory:') - s3 = providers.Singleton(S3Connection, + s3 = providers.Singleton(boto.s3.connection.S3Connection, aws_access_key_id='KEY', aws_secret_access_key='SECRET') @@ -84,32 +84,69 @@ Example class Services(catalogs.DeclarativeCatalog): """Catalog of business service providers.""" - users = providers.Factory(services.Users, + users = providers.Factory(example.services.Users, db=Platform.database) - photos = providers.Factory(services.Photos, + photos = providers.Factory(example.services.Photos, db=Platform.database, s3=Platform.s3) - auth = providers.Factory(services.Auth, + auth = providers.Factory(example.services.Auth, db=Platform.database, token_ttl=3600) +Next example demonstrates usage of these IoC containers with the help of +``@inject`` decorator: - @injections.inject(users_service=Services.users) - @injections.inject(auth_service=Services.auth) - @injections.inject(photos_service=Services.photos) - def main(argv, users_service, auth_service, photos_service): +.. code-block:: python + + """Dependency Injector example.""" + + from dependency_injector.injections import inject + + from catalogs import Services + + + @inject(users_service=Services.users) + @inject(auth_service=Services.auth) + @inject(photos_service=Services.photos) + def main(login, password, photo, users_service, auth_service, photos_service): """Main function.""" - login, password, photo_path = argv[1:] - user = users_service.get_user(login) auth_service.authenticate(user, password) - photos_service.upload_photo(user['id'], photo_path) + photos_service.upload_photo(user['id'], photo) if __name__ == '__main__': - main(sys.argv) + main(login='user', password='secret', photo='photo.jpg') + +Also some alternative definition styles could be used. Like this one: + +.. code-block:: python + + class Platform(catalogs.DeclarativeCatalog): + """Catalog of platform service providers.""" + + database = providers.Singleton(sqlite3.connect) \ + .args(':memory:') + + s3 = providers.Singleton(boto.s3.connection.S3Connection) \ + .kwargs(aws_access_key_id='KEY', + aws_secret_access_key='SECRET') + +or like this one: + +.. code-block:: python + + class Platform(catalogs.DeclarativeCatalog): + """Catalog of platform service providers.""" + + database = providers.Singleton(sqlite3.connect) + database.args(':memory:') + + s3 = providers.Singleton(boto.s3.connection.S3Connection) + s3.kwargs(aws_access_key_id='KEY', + aws_secret_access_key='SECRET') You can get more *Dependency Injector* examples in ``/examples`` directory on GitHub: diff --git a/examples/miniapps/services/README.rst b/examples/miniapps/services/README.rst index ccf3947b..00c11981 100644 --- a/examples/miniapps/services/README.rst +++ b/examples/miniapps/services/README.rst @@ -5,4 +5,10 @@ Instructions for running: .. code-block:: bash - python main.py dependency-injector secret myself.jpg + python main.py + + +.. note:: + + ``catalogs_alt_style_1.py`` and ``catalogs_alt_style_2.py`` contain + examples of atlernative catalogs definition styles. diff --git a/examples/miniapps/services/catalogs.py b/examples/miniapps/services/catalogs.py new file mode 100644 index 00000000..3ff6ca2c --- /dev/null +++ b/examples/miniapps/services/catalogs.py @@ -0,0 +1,33 @@ +"""Example of several Dependency Injector catalogs.""" + +import sqlite3 +import boto.s3.connection +import example.services + +from dependency_injector import catalogs +from dependency_injector import providers + + +class Platform(catalogs.DeclarativeCatalog): + """Catalog of platform service providers.""" + + 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(catalogs.DeclarativeCatalog): + """Catalog of business service providers.""" + + users = providers.Factory(example.services.Users, + db=Platform.database) + + photos = providers.Factory(example.services.Photos, + db=Platform.database, + s3=Platform.s3) + + auth = providers.Factory(example.services.Auth, + db=Platform.database, + token_ttl=3600) diff --git a/examples/miniapps/services/catalogs_alt_syntax_1.py b/examples/miniapps/services/catalogs_alt_syntax_1.py new file mode 100644 index 00000000..40863bfa --- /dev/null +++ b/examples/miniapps/services/catalogs_alt_syntax_1.py @@ -0,0 +1,37 @@ +"""Example of several Dependency Injector catalogs. + +Alternative definition style #1. +""" + +import sqlite3 +import boto.s3.connection +import example.services + +from dependency_injector import catalogs +from dependency_injector import providers + + +class Platform(catalogs.DeclarativeCatalog): + """Catalog of platform service providers.""" + + database = providers.Singleton(sqlite3.connect) \ + .args(':memory:') + + s3 = providers.Singleton(boto.s3.connection.S3Connection) \ + .kwargs(aws_access_key_id='KEY', + aws_secret_access_key='SECRET') + + +class Services(catalogs.DeclarativeCatalog): + """Catalog of business service providers.""" + + users = providers.Factory(example.services.Users) \ + .kwargs(db=Platform.database) + + photos = providers.Factory(example.services.Photos) \ + .kwargs(db=Platform.database, + s3=Platform.s3) + + auth = providers.Factory(example.services.Auth) \ + .kwargs(db=Platform.database, + token_ttl=3600) diff --git a/examples/miniapps/services/catalogs_alt_syntax_2.py b/examples/miniapps/services/catalogs_alt_syntax_2.py new file mode 100644 index 00000000..588b726e --- /dev/null +++ b/examples/miniapps/services/catalogs_alt_syntax_2.py @@ -0,0 +1,37 @@ +"""Example of several Dependency Injector catalogs. + +Alternative definition style #2. +""" + +import sqlite3 +import boto.s3.connection +import example.services + +from dependency_injector import catalogs +from dependency_injector import providers + + +class Platform(catalogs.DeclarativeCatalog): + """Catalog of platform service providers.""" + + database = providers.Singleton(sqlite3.connect) + database.args(':memory:') + + s3 = providers.Singleton(boto.s3.connection.S3Connection) + s3.kwargs(aws_access_key_id='KEY', + aws_secret_access_key='SECRET') + + +class Services(catalogs.DeclarativeCatalog): + """Catalog of business service providers.""" + + users = providers.Factory(example.services.Users) + users.kwargs(db=Platform.database) + + photos = providers.Factory(example.services.Photos) + photos.kwargs(db=Platform.database, + s3=Platform.s3) + + auth = providers.Factory(example.services.Auth) + auth.kwargs(db=Platform.database, + token_ttl=3600) diff --git a/examples/miniapps/services/main.py b/examples/miniapps/services/main.py index 30ebdb76..b0da2297 100644 --- a/examples/miniapps/services/main.py +++ b/examples/miniapps/services/main.py @@ -1,53 +1,19 @@ """Dependency Injector example.""" -import sys -import sqlite3 +from dependency_injector.injections import inject -from boto.s3.connection import S3Connection - -from dependency_injector import catalogs -from dependency_injector import providers -from dependency_injector import injections - -from example import services +from catalogs import Services -class Platform(catalogs.DeclarativeCatalog): - """Catalog of platform service providers.""" - - database = providers.Singleton(sqlite3.connect, ':memory:') - - s3 = providers.Singleton(S3Connection, - aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - -class Services(catalogs.DeclarativeCatalog): - """Catalog of business service providers.""" - - users = providers.Factory(services.Users, - db=Platform.database) - - photos = providers.Factory(services.Photos, - db=Platform.database, - s3=Platform.s3) - - auth = providers.Factory(services.Auth, - db=Platform.database, - token_ttl=3600) - - -@injections.inject(users_service=Services.users) -@injections.inject(auth_service=Services.auth) -@injections.inject(photos_service=Services.photos) -def main(argv, users_service, auth_service, photos_service): +@inject(users_service=Services.users) +@inject(auth_service=Services.auth) +@inject(photos_service=Services.photos) +def main(login, password, photo, users_service, auth_service, photos_service): """Main function.""" - login, password, photo_path = argv[1:] - user = users_service.get_user(login) auth_service.authenticate(user, password) - photos_service.upload_photo(user['id'], photo_path) + photos_service.upload_photo(user['id'], photo) if __name__ == '__main__': - main(sys.argv) + main(login='user', password='secret', photo='photo.jpg') diff --git a/examples/miniapps/services/main2.py b/examples/miniapps/services/main2.py deleted file mode 100644 index 517f5d38..00000000 --- a/examples/miniapps/services/main2.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Dependency Injector example.""" - -import sys -import sqlite3 - -from boto.s3.connection import S3Connection - -from dependency_injector import catalogs -from dependency_injector import providers -from dependency_injector import injections - -from example import services - - -class Platform(catalogs.DeclarativeCatalog): - """Catalog of platform service providers.""" - - database = providers.Singleton(sqlite3.connect) - database.args(':memory:') - - s3 = providers.Singleton(S3Connection) - s3.kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - -class Services(catalogs.DeclarativeCatalog): - """Catalog of business service providers.""" - - users = providers.Factory(services.Users) - users.kwargs(db=Platform.database) - - photos = providers.Factory(services.Photos) - photos.kwargs(db=Platform.database, - s3=Platform.s3) - - auth = providers.Factory(services.Auth) - auth.kwargs(db=Platform.database, - token_ttl=3600) - - -@injections.inject(users_service=Services.users) -@injections.inject(auth_service=Services.auth) -@injections.inject(photos_service=Services.photos) -def main(argv, users_service, auth_service, photos_service): - """Main function.""" - login, password, photo_path = argv[1:] - - user = users_service.get_user(login) - auth_service.authenticate(user, password) - photos_service.upload_photo(user['id'], photo_path) - - -if __name__ == '__main__': - main(sys.argv) diff --git a/examples/miniapps/services/main3.py b/examples/miniapps/services/main3.py deleted file mode 100644 index 54fea3d3..00000000 --- a/examples/miniapps/services/main3.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Dependency Injector example.""" - -import sys -import sqlite3 - -from boto.s3.connection import S3Connection - -from dependency_injector import catalogs -from dependency_injector import providers -from dependency_injector import injections - -from example import services - - -class Platform(catalogs.DeclarativeCatalog): - """Catalog of platform service providers.""" - - database = providers.Singleton(sqlite3.connect) \ - .args(':memory:') - - s3 = providers.Singleton(S3Connection) \ - .kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - -class Services(catalogs.DeclarativeCatalog): - """Catalog of business service providers.""" - - users = providers.Factory(services.Users) \ - .kwargs(db=Platform.database) - - photos = providers.Factory(services.Photos) \ - .kwargs(db=Platform.database, - s3=Platform.s3) - - auth = providers.Factory(services.Auth) \ - .kwargs(db=Platform.database, - token_ttl=3600) - - -@injections.inject(users_service=Services.users) -@injections.inject(auth_service=Services.auth) -@injections.inject(photos_service=Services.photos) -def main(argv, users_service, auth_service, photos_service): - """Main function.""" - login, password, photo_path = argv[1:] - - user = users_service.get_user(login) - auth_service.authenticate(user, password) - photos_service.upload_photo(user['id'], photo_path) - - -if __name__ == '__main__': - main(sys.argv)