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

54 lines
1.5 KiB
Python
Raw Normal View History

2016-04-20 14:25:40 +03:00
"""Dependency Injector example."""
2016-04-20 14:19:54 +03:00
import sys
import sqlite3
2016-04-20 14:25:40 +03:00
from boto.s3.connection import S3Connection
2016-04-20 14:19:54 +03:00
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
2016-04-20 14:45:42 +03:00
from example import services
2016-04-20 14:25:40 +03:00
2016-04-20 14:19:54 +03:00
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect, ':memory:')
2016-04-20 14:25:40 +03:00
s3 = providers.Singleton(S3Connection,
2016-04-20 14:19:54 +03:00
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)
2016-04-20 14:25:40 +03:00
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
2016-04-20 14:19:54 +03:00
"""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)