From 68ae1b80df480aa7a18fa60baf88ca3a5bc75060 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sun, 29 May 2016 22:21:50 +0300 Subject: [PATCH] Drop catalog examples --- examples/catalogs/declarative.py | 22 ------ examples/catalogs/declarative_inheritance.py | 30 -------- examples/catalogs/declarative_injections.py | 47 ------------ .../declarative_provider_type/catalog.py | 73 ------------------- .../declarative_provider_type/services.py | 26 ------- examples/catalogs/dynamic.py | 18 ----- .../catalogs/dynamic_provider_type/catalog.py | 63 ---------------- .../dynamic_provider_type/services.py | 26 ------- examples/catalogs/dynamic_runtime_creation.py | 66 ----------------- examples/catalogs/override_declarative.py | 45 ------------ .../override_declarative_by_dynamic.py | 41 ----------- .../override_declarative_decorator.py | 43 ----------- 12 files changed, 500 deletions(-) delete mode 100644 examples/catalogs/declarative.py delete mode 100644 examples/catalogs/declarative_inheritance.py delete mode 100644 examples/catalogs/declarative_injections.py delete mode 100644 examples/catalogs/declarative_provider_type/catalog.py delete mode 100644 examples/catalogs/declarative_provider_type/services.py delete mode 100644 examples/catalogs/dynamic.py delete mode 100644 examples/catalogs/dynamic_provider_type/catalog.py delete mode 100644 examples/catalogs/dynamic_provider_type/services.py delete mode 100644 examples/catalogs/dynamic_runtime_creation.py delete mode 100644 examples/catalogs/override_declarative.py delete mode 100644 examples/catalogs/override_declarative_by_dynamic.py delete mode 100644 examples/catalogs/override_declarative_decorator.py diff --git a/examples/catalogs/declarative.py b/examples/catalogs/declarative.py deleted file mode 100644 index a483fc88..00000000 --- a/examples/catalogs/declarative.py +++ /dev/null @@ -1,22 +0,0 @@ -"""Declarative catalog simple example.""" - -from dependency_injector import catalogs -from dependency_injector import providers - - -# Defining declarative catalog: -class Catalog(catalogs.DeclarativeCatalog): - """Providers catalog.""" - - factory1 = providers.Factory(object) - - factory2 = providers.Factory(object) - -# Creating some objects: -object1 = Catalog.factory1() -object2 = Catalog.factory2() - -# Making some asserts: -assert object1 is not object2 -assert isinstance(object1, object) -assert isinstance(object2, object) diff --git a/examples/catalogs/declarative_inheritance.py b/examples/catalogs/declarative_inheritance.py deleted file mode 100644 index e0ff494c..00000000 --- a/examples/catalogs/declarative_inheritance.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Declarative catalogs inheritance example.""" - -from dependency_injector import catalogs -from dependency_injector import providers - - -class CatalogA(catalogs.DeclarativeCatalog): - """Example catalog A.""" - - provider1 = providers.Factory(object) - - -class CatalogB(CatalogA): - """Example catalog B.""" - - provider2 = providers.Singleton(object) - - -# Making some asserts for `providers` attribute: -assert CatalogA.providers == dict(provider1=CatalogA.provider1) -assert CatalogB.providers == dict(provider1=CatalogA.provider1, - provider2=CatalogB.provider2) - -# Making some asserts for `cls_providers` attribute: -assert CatalogA.cls_providers == dict(provider1=CatalogA.provider1) -assert CatalogB.cls_providers == dict(provider2=CatalogB.provider2) - -# Making some asserts for `inherited_providers` attribute: -assert CatalogA.inherited_providers == dict() -assert CatalogB.inherited_providers == dict(provider1=CatalogA.provider1) diff --git a/examples/catalogs/declarative_injections.py b/examples/catalogs/declarative_injections.py deleted file mode 100644 index 85dc8f87..00000000 --- a/examples/catalogs/declarative_injections.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Declarative catalog's provider injections example.""" - -import sqlite3 - -from dependency_injector import catalogs -from dependency_injector import providers - - -class UsersService(object): - """Users service, that has dependency on database.""" - - def __init__(self, db): - """Initializer.""" - self.db = db - - -class AuthService(object): - """Auth service, that has dependencies on users service and database.""" - - def __init__(self, db, users_service): - """Initializer.""" - self.db = db - self.users_service = users_service - - -class Services(catalogs.DeclarativeCatalog): - """Catalog of service providers.""" - - database = providers.Singleton(sqlite3.connect, ':memory:') - - users = providers.Factory(UsersService, - db=database) - - auth = providers.Factory(AuthService, - db=database, - users_service=users) - - -# Retrieving service providers from catalog: -users_service = Services.users() -auth_service = Services.auth() - -# Making some asserts: -assert users_service.db is auth_service.db is Services.database() -assert isinstance(auth_service.users_service, UsersService) -assert users_service is not Services.users() -assert auth_service is not Services.auth() diff --git a/examples/catalogs/declarative_provider_type/catalog.py b/examples/catalogs/declarative_provider_type/catalog.py deleted file mode 100644 index 520da6cc..00000000 --- a/examples/catalogs/declarative_provider_type/catalog.py +++ /dev/null @@ -1,73 +0,0 @@ -"""Specialized declarative catalog example.""" - -import services - -from dependency_injector import providers -from dependency_injector import errors - - -class UsersService(services.Base): - """Users service.""" - - def __init__(self, config): - """Initializer.""" - self.config = config - super(UsersService, self).__init__() - - -class AuthService(services.Base): - """Auth service.""" - - def __init__(self, config, users_service): - """Initializer.""" - self.config = config - self.users_service = users_service - super(AuthService, self).__init__() - - -class Services(services.Catalog): - """Services catalog.""" - - users = services.Provider(UsersService, - config={'option1': '111', - 'option2': '222'}) - - auth = services.Provider(AuthService, - config={'option3': '333', - 'option4': '444'}, - users_service=users) - - -# Creating users & auth services: -users_service = Services.users() -auth_service = Services.auth() - -# Making some asserts: -assert users_service.config == {'option1': '111', - 'option2': '222'} -assert auth_service.config == {'option3': '333', - 'option4': '444'} -assert isinstance(auth_service.users_service, UsersService) - -# Trying to declare services catalog with other provider type: -try: - class Services1(services.Catalog): - """Services catalog.""" - - users = providers.Factory(UsersService) -except errors.Error as exception: - print exception - # <__main__.Services1()> can contain only - # instances - -# Trying to declare services catalog with correct provider by invalid provided -# type: -try: - class Services2(services.Catalog): - """Services catalog.""" - - users = services.Provider(object) -except errors.Error as exception: - print exception - # can provide only - # instances diff --git a/examples/catalogs/declarative_provider_type/services.py b/examples/catalogs/declarative_provider_type/services.py deleted file mode 100644 index 7a1f0432..00000000 --- a/examples/catalogs/declarative_provider_type/services.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Base classes for services.""" - -from dependency_injector import catalogs -from dependency_injector import providers - - -class Base(object): - """Base service class.""" - - -class Provider(providers.Factory): - """Service provider. - - Can provide :py:class:`Base` only. - """ - - provided_type = Base - - -class Catalog(catalogs.DeclarativeCatalog): - """Base catalog of services. - - Can include :py:class:`Provider`'s only. - """ - - provider_type = Provider diff --git a/examples/catalogs/dynamic.py b/examples/catalogs/dynamic.py deleted file mode 100644 index b89bc92c..00000000 --- a/examples/catalogs/dynamic.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Dynamic catalog simple example.""" - -from dependency_injector import catalogs -from dependency_injector import providers - - -# Defining dynamic catalog: -catalog = catalogs.DynamicCatalog(factory1=providers.Factory(object), - factory2=providers.Factory(object)) - -# Creating some objects: -object1 = catalog.factory1() -object2 = catalog.factory2() - -# Making some asserts: -assert object1 is not object2 -assert isinstance(object1, object) -assert isinstance(object2, object) diff --git a/examples/catalogs/dynamic_provider_type/catalog.py b/examples/catalogs/dynamic_provider_type/catalog.py deleted file mode 100644 index 7578c492..00000000 --- a/examples/catalogs/dynamic_provider_type/catalog.py +++ /dev/null @@ -1,63 +0,0 @@ -"""Specialized dynamic catalog example.""" - -import services - -from dependency_injector import providers -from dependency_injector import errors - - -class UsersService(services.Base): - """Users service.""" - - def __init__(self, config): - """Initializer.""" - self.config = config - super(UsersService, self).__init__() - - -class AuthService(services.Base): - """Auth service.""" - - def __init__(self, config, users_service): - """Initializer.""" - self.config = config - self.users_service = users_service - super(AuthService, self).__init__() - - -services_catalog = services.Catalog() -services_catalog.users = services.Provider(UsersService, - config={'option1': '111', - 'option2': '222'}) -services_catalog.auth = services.Provider(AuthService, - config={'option3': '333', - 'option4': '444'}, - users_service=services_catalog.users) - -# Creating users & auth services: -users_service = services_catalog.users() -auth_service = services_catalog.auth() - -# Making some asserts: -assert users_service.config == {'option1': '111', - 'option2': '222'} -assert auth_service.config == {'option3': '333', - 'option4': '444'} -assert isinstance(auth_service.users_service, UsersService) - -# Trying to declare services catalog with other provider type: -try: - services_catalog.users = providers.Factory(UsersService) -except errors.Error as exception: - print exception - # can contain only - # instances - -# Trying to declare services catalog with correct provider by invalid provided -# type: -try: - services_catalog.users = services.Provider(object) -except errors.Error as exception: - print exception - # can provide only - # instances diff --git a/examples/catalogs/dynamic_provider_type/services.py b/examples/catalogs/dynamic_provider_type/services.py deleted file mode 100644 index 853ea1b4..00000000 --- a/examples/catalogs/dynamic_provider_type/services.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Base classes for services.""" - -from dependency_injector import catalogs -from dependency_injector import providers - - -class Base(object): - """Base service class.""" - - -class Provider(providers.Factory): - """Service provider. - - Can provide :py:class:`Base` only. - """ - - provided_type = Base - - -class Catalog(catalogs.DynamicCatalog): - """Base catalog of services. - - Can include :py:class:`Provider`'s only. - """ - - provider_type = Provider diff --git a/examples/catalogs/dynamic_runtime_creation.py b/examples/catalogs/dynamic_runtime_creation.py deleted file mode 100644 index c42570b1..00000000 --- a/examples/catalogs/dynamic_runtime_creation.py +++ /dev/null @@ -1,66 +0,0 @@ -"""Dynamic catalog creation and runtime filling of it example.""" - -from dependency_injector import catalogs - - -# Defining several example services: -class UsersService(object): - """Example users service.""" - - -class AuthService(object): - """Example auth service.""" - - -def import_cls(cls_name): - """Import class by its fully qualified name. - - In terms of current example it is just a small helper function. Please, - don't use it in production approaches. - """ - path_components = cls_name.split('.') - if len(path_components) == 1: - path_components.insert(0, '__main__') - module = __import__('.'.join(path_components[0:-1]), - locals(), - globals(), - fromlist=path_components[-1:]) - return getattr(module, path_components[-1]) - - -# "Parsing" some configuration: -config = { - 'services': { - 'users': { - 'class': 'UsersService', - 'provider_class': 'dependency_injector.providers.Factory', - }, - 'auth': { - 'class': 'AuthService', - 'provider_class': 'dependency_injector.providers.Factory', - } - } -} - -# Defining dynamic service providers catalog: -services = catalogs.DynamicCatalog() - -# Filling dynamic service providers catalog according to the configuration: -for service_name, service_info in config['services'].iteritems(): - # Runtime importing of service and service provider classes: - service_cls = import_cls(service_info['class']) - service_provider_cls = import_cls(service_info['provider_class']) - - # Creating service provider: - service_provider = service_provider_cls(service_cls) - - # Binding service provider to the dynamic service providers catalog: - services.bind_provider(service_name, service_provider) - -# Creating some objects: -users_service = services.users() -auth_service = services.auth() - -# Making some asserts: -assert isinstance(users_service, UsersService) -assert isinstance(auth_service, AuthService) diff --git a/examples/catalogs/override_declarative.py b/examples/catalogs/override_declarative.py deleted file mode 100644 index a38777ad..00000000 --- a/examples/catalogs/override_declarative.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Declarative catalog overriding example.""" - -import collections - -from dependency_injector import catalogs -from dependency_injector import providers - - -# Creating some example classes: -Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) -Object2 = collections.namedtuple('Object2', ['object1']) -ExtendedObject2 = collections.namedtuple('ExtendedObject2', []) - - -class Catalog(catalogs.DeclarativeCatalog): - """Catalog of some providers.""" - - object1_factory = providers.Factory(Object1, - arg1=1, - arg2=2) - - object2_factory = providers.Factory(Object2, - object1=object1_factory) - - -class AnotherCatalog(catalogs.DeclarativeCatalog): - """Overriding catalog.""" - - object2_factory = providers.Factory(ExtendedObject2) - - -# Overriding `Catalog` with `AnotherCatalog`: -Catalog.override(AnotherCatalog) - -# Creating some objects using overridden catalog: -object2_1 = Catalog.object2_factory() -object2_2 = Catalog.object2_factory() - -# Making some asserts: -assert Catalog.is_overridden - -assert object2_1 is not object2_2 - -assert isinstance(object2_1, ExtendedObject2) -assert isinstance(object2_2, ExtendedObject2) diff --git a/examples/catalogs/override_declarative_by_dynamic.py b/examples/catalogs/override_declarative_by_dynamic.py deleted file mode 100644 index 7246e5f4..00000000 --- a/examples/catalogs/override_declarative_by_dynamic.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Declarative catalog overriding by dynamic catalog example.""" - -import collections - -from dependency_injector import catalogs -from dependency_injector import providers - - -# Creating some example classes: -Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) -Object2 = collections.namedtuple('Object2', ['object1']) -ExtendedObject2 = collections.namedtuple('ExtendedObject2', []) - - -class Catalog(catalogs.DeclarativeCatalog): - """Catalog of some providers.""" - - object1_factory = providers.Factory(Object1, - arg1=1, - arg2=2) - - object2_factory = providers.Factory(Object2, - object1=object1_factory) - - -# Overriding `Catalog` with some `DynamicCatalog` instance: -overriding_catalog = catalogs.DynamicCatalog( - object2_factory=providers.Factory(ExtendedObject2)) -Catalog.override(overriding_catalog) - -# Creating some objects using overridden catalog: -object2_1 = Catalog.object2_factory() -object2_2 = Catalog.object2_factory() - -# Making some asserts: -assert Catalog.is_overridden - -assert object2_1 is not object2_2 - -assert isinstance(object2_1, ExtendedObject2) -assert isinstance(object2_2, ExtendedObject2) diff --git a/examples/catalogs/override_declarative_decorator.py b/examples/catalogs/override_declarative_decorator.py deleted file mode 100644 index cb59a752..00000000 --- a/examples/catalogs/override_declarative_decorator.py +++ /dev/null @@ -1,43 +0,0 @@ -"""Declarative catalog overriding using `@override()` decorator example.""" - -import collections - -from dependency_injector import catalogs -from dependency_injector import providers - -# Creating some example classes: -Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) -Object2 = collections.namedtuple('Object2', ['object1']) -ExtendedObject2 = collections.namedtuple('ExtendedObject2', []) - - -class Catalog(catalogs.DeclarativeCatalog): - """Catalog of some providers.""" - - object1_factory = providers.Factory(Object1, - arg1=1, - arg2=2) - - object2_factory = providers.Factory(Object2, - object1=object1_factory) - - -# Overriding `Catalog` with `AnotherCatalog`: -@catalogs.override(Catalog) -class AnotherCatalog(catalogs.DeclarativeCatalog): - """Overriding catalog.""" - - object2_factory = providers.Factory(ExtendedObject2) - - -# Creating some objects using overridden catalog: -object2_1 = Catalog.object2_factory() -object2_2 = Catalog.object2_factory() - -# Making some asserts: -assert Catalog.is_overridden - -assert object2_1 is not object2_2 - -assert isinstance(object2_1, ExtendedObject2) -assert isinstance(object2_2, ExtendedObject2)