Skip to content

Commit

Permalink
Accept container parameters in initialize_container (#30)
Browse files Browse the repository at this point in the history
Add parameters and make container optional in initialize_container
  • Loading branch information
maldoinc authored Aug 21, 2024
1 parent 8268073 commit 7afd402
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
26 changes: 14 additions & 12 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,33 @@ 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"
```python title="main.py" hl_lines="9-18"
import os
from wireup import container, initialize_container
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])
# ⬇️ Start the container: This registers and initializes services.
initialize_container(
container,
# Parameters serve as application/service configuration.
parameters={
"redis_url": os.environ["APP_REDIS_URL"],
"weather_api_key": os.environ["APP_WEATHER_API_KEY"]
},
# Top-level modules containing service registrations.
# This is where your services live.
service_modules=[services]
)

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

Expand Down
26 changes: 14 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ Example showcasing a Redis wrapper and a weather service that calls an external

```python
from wireup import container, initialize_container

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])

# ⬇️ Start the container: This registers and initializes services.
initialize_container(
container,
# Parameters serve as application/service configuration.
parameters={
"redis_url": os.environ["APP_REDIS_URL"],
"weather_api_key": os.environ["APP_WEATHER_API_KEY"]
},
# Top-level modules containing service registrations.
# This is where your services live.
service_modules=[services]
)

return app
```
Expand Down
15 changes: 14 additions & 1 deletion wireup/import_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@
from wireup import DependencyContainer


def initialize_container(dependency_container: DependencyContainer, *, service_modules: list[ModuleType]) -> None:
def initialize_container(
dependency_container: DependencyContainer,
*,
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.
:param service_modules: Top-level modules containing service registrations.
:param parameters: Parameters to be added to the container.
"""
if parameters:
dependency_container.params.update(parameters)

_register_services(dependency_container, service_modules)
dependency_container.warmup()

Expand Down
9 changes: 5 additions & 4 deletions wireup/integration/django/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class WireupConfig(AppConfig):
def ready(self) -> None: # noqa: D102
integration_settings: WireupSettings = settings.WIREUP

for entry in dir(settings):
if not entry.startswith("__") and hasattr(settings, entry):
container.params.put(entry, getattr(settings, entry))

initialize_container(
container,
parameters={
entry: getattr(settings, entry)
for entry in dir(settings)
if not entry.startswith("__") and hasattr(settings, entry)
},
service_modules=[
importlib.import_module(m) if isinstance(m, str) else m for m in integration_settings.service_modules
],
Expand Down

0 comments on commit 7afd402

Please sign in to comment.