Skip to content

Commit

Permalink
Improve settings support
Browse files Browse the repository at this point in the history
  • Loading branch information
humrochagf committed Jun 6, 2024
1 parent 51808ef commit b807cca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
show_source: false
members:
- Wheke
- WhekeSettings
- Pod
- ServiceList
- aget_service
- get_service
- settings
- get_settings
5 changes: 3 additions & 2 deletions src/wheke/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from ._demo import demo_pod
from ._pod import Pod, ServiceList
from ._service import aget_service, get_service
from ._settings import settings
from ._settings import WhekeSettings, get_settings

__all__ = [
"Pod",
"ServiceList",
"Wheke",
"WhekeSettings",
"aget_service",
"demo_pod",
"get_service",
"settings",
"get_settings",
]
22 changes: 18 additions & 4 deletions src/wheke/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._cli import empty_callback, version
from ._pod import Pod
from ._service import get_service_registry
from ._settings import settings
from ._settings import WhekeSettings, get_settings


class Wheke:
Expand All @@ -18,10 +18,24 @@ class Wheke:
pods: list[Pod]
"The list of pods plugged to Wheke."

def __init__(self) -> None:
def __init__(
self, settings: WhekeSettings | type[WhekeSettings] | None = None
) -> None:
self.pods = []

for pod in settings.pods:
if settings is None:
settings_cls = WhekeSettings
settings_obj = WhekeSettings()
elif isinstance(settings, WhekeSettings):
settings_cls = type(settings)
settings_obj = settings
else:
settings_cls = settings
settings_obj = settings_cls()

get_service_registry().register_value(settings_cls, settings_obj)

for pod in get_settings(WhekeSettings).pods:
self.add_pod(pod)

def add_pod(self, pod_to_add: Pod | str) -> None:
Expand All @@ -45,7 +59,7 @@ def create_app(self) -> FastAPI:
"""
Create a FastAPI app with all plugged pods.
"""
app = FastAPI(title=settings.project_name)
app = FastAPI(title=get_settings(WhekeSettings).project_name)

for pod in self.pods:
if pod.static_url is not None and pod.static_path is not None:
Expand Down
12 changes: 10 additions & 2 deletions src/wheke/_settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import TypeVar

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict

from ._service import get_service


class Settings(BaseSettings):
class WhekeSettings(BaseSettings):
project_name: str = "Wheke"

pods: list[str] = Field(default_factory=list)
Expand All @@ -15,4 +19,8 @@ class Settings(BaseSettings):
)


settings = Settings()
T = TypeVar("T", bound=WhekeSettings)


def get_settings(cls: type[T]) -> T:
return get_service(cls)
20 changes: 15 additions & 5 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import FastAPI, status
from fastapi.testclient import TestClient

from wheke import Pod, Wheke, demo_pod, settings
from wheke import Pod, Wheke, WhekeSettings, demo_pod, get_settings
from wheke._demo import DEMO_PAGE


Expand All @@ -14,18 +14,16 @@ def test_create_app() -> None:


def test_create_app_with_demo_pod_in_settings() -> None:
before_pods = settings.pods.copy()
settings = WhekeSettings()
settings.pods = ["wheke.demo_pod"]

wheke = Wheke()
wheke = Wheke(settings)

app = wheke.create_app()

assert type(app) is FastAPI
assert demo_pod in wheke.pods

settings.pods = before_pods


def test_create_app_with_empty_pod() -> None:
empty_pod = Pod("empty")
Expand All @@ -38,6 +36,18 @@ def test_create_app_with_empty_pod() -> None:
assert wheke.pods == [empty_pod]


def test_create_app_with_custom_settings_class() -> None:
class CustomSettings(WhekeSettings):
test_setting: str = "test"

wheke = Wheke(CustomSettings)

app = wheke.create_app()

assert type(app) is FastAPI
assert get_settings(CustomSettings).test_setting == "test"


def test_demo_pod(client: TestClient) -> None:
response = client.get("/")

Expand Down

0 comments on commit b807cca

Please sign in to comment.