Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept container parameters in initialize_container #30

Merged
merged 11 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading