mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-30 19:24:31 +03:00
Update main example
This commit is contained in:
parent
4cf3bdb1fb
commit
1292d4af68
23
README.rst
23
README.rst
|
@ -52,30 +52,31 @@ Installation
|
||||||
|
|
||||||
pip install dependency_injector
|
pip install dependency_injector
|
||||||
|
|
||||||
Examples
|
Example
|
||||||
--------
|
-------
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
"""Dependency Injector initial example."""
|
"""Dependency Injector example."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
|
||||||
|
|
||||||
import services
|
from boto.s3.connection import S3Connection
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
from dependency_injector import catalogs
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
from dependency_injector import injections
|
from dependency_injector import injections
|
||||||
|
|
||||||
|
import services # Example business services module (Users, Photos, Auth)
|
||||||
|
|
||||||
|
|
||||||
class Platform(catalogs.DeclarativeCatalog):
|
class Platform(catalogs.DeclarativeCatalog):
|
||||||
"""Catalog of platform service providers."""
|
"""Catalog of platform service providers."""
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect, ':memory:')
|
database = providers.Singleton(sqlite3.connect, ':memory:')
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
s3 = providers.Singleton(S3Connection,
|
||||||
aws_access_key_id='KEY',
|
aws_access_key_id='KEY',
|
||||||
aws_secret_access_key='SECRET')
|
aws_secret_access_key='SECRET')
|
||||||
|
|
||||||
|
@ -97,19 +98,13 @@ Examples
|
||||||
|
|
||||||
@injections.inject(users_service=Services.users)
|
@injections.inject(users_service=Services.users)
|
||||||
@injections.inject(auth_service=Services.auth)
|
@injections.inject(auth_service=Services.auth)
|
||||||
def main(argv, users_service, auth_service):
|
@injections.inject(photos_service=Services.photos)
|
||||||
|
def main(argv, users_service, auth_service, photos_service):
|
||||||
"""Main function."""
|
"""Main function."""
|
||||||
login, password, photo_path = argv[1:]
|
login, password, photo_path = argv[1:]
|
||||||
|
|
||||||
user = users_service.get_user(login)
|
user = users_service.get_user(login)
|
||||||
auth_service.authenticate(user, password)
|
auth_service.authenticate(user, password)
|
||||||
|
|
||||||
upload_photo(user, photo_path)
|
|
||||||
|
|
||||||
|
|
||||||
@injections.inject(photos_service=Services.photos)
|
|
||||||
def upload_photo(user, photo_path, photos_service):
|
|
||||||
"""Upload photo."""
|
|
||||||
photos_service.upload_photo(user['id'], photo_path)
|
photos_service.upload_photo(user['id'], photo_path)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,23 @@
|
||||||
"""Dependency Injector initial example."""
|
"""Dependency Injector example."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
|
||||||
|
|
||||||
import services
|
from boto.s3.connection import S3Connection
|
||||||
|
|
||||||
from dependency_injector import catalogs
|
from dependency_injector import catalogs
|
||||||
from dependency_injector import providers
|
from dependency_injector import providers
|
||||||
from dependency_injector import injections
|
from dependency_injector import injections
|
||||||
|
|
||||||
|
import services # Example business services module (Users, Photos, Auth)
|
||||||
|
|
||||||
|
|
||||||
class Platform(catalogs.DeclarativeCatalog):
|
class Platform(catalogs.DeclarativeCatalog):
|
||||||
"""Catalog of platform service providers."""
|
"""Catalog of platform service providers."""
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect, ':memory:')
|
database = providers.Singleton(sqlite3.connect, ':memory:')
|
||||||
|
|
||||||
s3 = providers.Singleton(boto.s3.connection.S3Connection,
|
s3 = providers.Singleton(S3Connection,
|
||||||
aws_access_key_id='KEY',
|
aws_access_key_id='KEY',
|
||||||
aws_secret_access_key='SECRET')
|
aws_secret_access_key='SECRET')
|
||||||
|
|
||||||
|
@ -38,19 +39,13 @@ class Services(catalogs.DeclarativeCatalog):
|
||||||
|
|
||||||
@injections.inject(users_service=Services.users)
|
@injections.inject(users_service=Services.users)
|
||||||
@injections.inject(auth_service=Services.auth)
|
@injections.inject(auth_service=Services.auth)
|
||||||
def main(argv, users_service, auth_service):
|
@injections.inject(photos_service=Services.photos)
|
||||||
|
def main(argv, users_service, auth_service, photos_service):
|
||||||
"""Main function."""
|
"""Main function."""
|
||||||
login, password, photo_path = argv[1:]
|
login, password, photo_path = argv[1:]
|
||||||
|
|
||||||
user = users_service.get_user(login)
|
user = users_service.get_user(login)
|
||||||
auth_service.authenticate(user, password)
|
auth_service.authenticate(user, password)
|
||||||
|
|
||||||
upload_photo(user, photo_path)
|
|
||||||
|
|
||||||
|
|
||||||
@injections.inject(photos_service=Services.photos)
|
|
||||||
def upload_photo(user, photo_path, photos_service):
|
|
||||||
"""Upload photo."""
|
|
||||||
photos_service.upload_photo(user['id'], photo_path)
|
photos_service.upload_photo(user['id'], photo_path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user