From b4591c326f95932e9d9defb06a0b0dd1bd5880b4 Mon Sep 17 00:00:00 2001 From: Aldo Mateli Date: Sun, 11 Aug 2024 21:22:56 +0100 Subject: [PATCH] Add parameters and make container optional in initialize_container --- docs/pages/getting_started.md | 29 ++++++++++++++--------------- readme.md | 24 ++++++++++++------------ wireup/import_util.py | 24 +++++++++++++++++++----- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index d5fec85..12e8989 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -28,32 +28,31 @@ In this example, we will store the Redis URL and an API key for the weather serv === "@ Annotations" ```python title="main.py" hl_lines="9 10 13 17" - import os - from wireup import container, initialize_container + import wireup from myapp import services def create_app(): app = ... - # Expose configuration by populating container.params. - container.params.put("redis_url", os.environ["APP_REDIS_URL"]) - container.params.put("weather_api_key", os.environ["APP_WEATHER_API_KEY"]) - - # Bulk update is also possible. - container.params.update(Settings().model_dump()) - - # Start the container: This registers + initializes services. - # `service_modules` contains top-level modules containing registrations. - initialize_container(container, service_modules=[services]) + wireup.initialize_container( + # Top-level modules containing service registrations. + # This is where your services live. + service_modules=[services], + # Parameters serve as application/service configuration. + parameters={ + "redis_url": os.environ["APP_REDIS_URL"], + "weather_api_key": os.environ["APP_WEATHER_API_KEY"] + } + ) return app ``` === "🏭 Programmatic" ```python title="main.py" hl_lines="15 19" - from pydantic import Field, PostgresDsn + from pydantic import Field from pydantic_settings import BaseSettings - from wireup import container, initialize_container + import wireup from myapp.services import factories @@ -69,7 +68,7 @@ In this example, we will store the Redis URL and an API key for the weather serv # Start the container: This registers + initializes services # service_modules contains top-level modules containing registrations. - initialize_container(container, service_modules=[factories]) + wireup.initialize_container(container, service_modules=[factories]) return app ``` diff --git a/readme.md b/readme.md index 5d04f19..53e793a 100644 --- a/readme.md +++ b/readme.md @@ -31,21 +31,21 @@ Example showcasing a Redis wrapper and a weather service that calls an external **1. Set up** ```python -from wireup import container, initialize_container +import wireup + def create_app(): app = ... - # ⬇️ Expose configuration by populating container.params. - container.params.put("redis_url", os.environ["APP_REDIS_URL"]) - container.params.put("weather_api_key", os.environ["APP_WEATHER_API_KEY"]) - - # Bulk update is possible via the "update" method. - container.params.update(Settings().model_dump()) - - # Start the container: This registers and initializes services. - # `service_modules` contains top-level modules containing registrations. - # ⬇️ - initialize_container(container, service_modules=[services]) + wireup.initialize_container( + # Top-level modules containing service registrations. + # This is where your services live. + service_modules=[services], + # Parameters serve as application/service configuration. + parameters={ + "redis_url": os.environ["APP_REDIS_URL"], + "weather_api_key": os.environ["APP_WEATHER_API_KEY"] + } + ) return app ``` diff --git a/wireup/import_util.py b/wireup/import_util.py index 24f30bb..cd95e25 100644 --- a/wireup/import_util.py +++ b/wireup/import_util.py @@ -9,21 +9,35 @@ from pathlib import Path from typing import TYPE_CHECKING, Any +import wireup +from wireup import DependencyContainer from wireup.annotation import AbstractDeclaration, ServiceDeclaration if TYPE_CHECKING: from collections.abc import Callable from types import ModuleType - from wireup import DependencyContainer - -def initialize_container(dependency_container: DependencyContainer, *, service_modules: list[ModuleType]) -> None: +def initialize_container( + dependency_container: DependencyContainer | None = None, + *, + service_modules: list[ModuleType], + parameters: dict[str, Any] | None = None, +) -> None: """Trigger service registrations in `service_modules` and initialize registered singleton services. - !!! note - For long-lived processes this should be executed once at startup. + :param dependency_container: The container to initialize, defaults to `wireup.container` if left empty. + :param parameters: Parameters to be added to the container. + :param service_modules: Top-level modules containing service registrations. + """ + if dependency_container is None: + wireup.container = DependencyContainer(wireup.ParameterBag()) + dependency_container = wireup.container + + if parameters: + dependency_container.params.update(parameters) + _register_services(dependency_container, service_modules) dependency_container.warmup()