Skip to content

Commit

Permalink
Add parameters and make container optional in initialize_container
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Aug 11, 2024
1 parent f66465a commit b4591c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
29 changes: 14 additions & 15 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
Expand Down
24 changes: 12 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
24 changes: 19 additions & 5 deletions wireup/import_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit b4591c3

Please sign in to comment.