Skip to content

Commit

Permalink
Merge config from config/app-default.yaml and config/app-custom.yaml.
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed Dec 8, 2024
1 parent f0d67fe commit 781da7d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.coverage
reports/
__pycache__/
config/app-merged.yaml
config/app-custom.yaml
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ COPY requirements/prod.txt requirements.txt
RUN pip install -r requirements.txt

COPY slackhealthbot slackhealthbot
COPY config/app-default.yaml config/app-default.yaml
COPY templates templates
COPY alembic.ini alembic.ini
COPY alembic alembic
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Pushes messages to a pre-selected Slack channel, when users log new weight data
* Create an application in the [Fitbit developer dashboard](https://dev.fitbit.com/apps/).
* Create an application [on Slack](https://api.slack.com/apps), with a webhook to post messages to a specific channel.
* Copy the `.env.template` file to `.env`, and modify the values.
* [Optional]: Copy the `config/app-custom.yaml.template` file to `app-custom.yaml`, and override any values which are defined in `config/app-default.yaml`.

## Retrieve the docker image

Expand All @@ -36,7 +37,7 @@ Create a folder on the host where the database will be saved: `/path/to/data/`.
Run the docker image.

```
docker run --detach --publish 8000:8000 -v `pwd`/.env:/app/.env -v /path/to/data/:/tmp/data ghcr.io/caarmen/slack-health-bot
docker run --detach --publish 8000:8000 -v `pwd`/.env:/app/.env -v `pwd`/app-custom.yaml:/app/config/app-custom.yaml -v /path/to/data/:/tmp/data ghcr.io/caarmen/slack-health-bot
```

## Using the application
Expand Down
4 changes: 4 additions & 0 deletions config/app-custom.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Place any overrides of the default values, from app-default.yaml, into this file.
app:
logging:
sql_log_level: "DEBUG" # example override: set sql logging to DEBUG
File renamed without changes.
27 changes: 26 additions & 1 deletion slackhealthbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import datetime as dt
from pathlib import Path

import yaml
from pydantic import AnyHttpUrl, BaseModel, HttpUrl
from pydantic.v1.utils import deep_update
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
Expand Down Expand Up @@ -73,7 +75,27 @@ class AppSettings(BaseSettings):
app: App
withings: Withings
fitbit: Fitbit
model_config = SettingsConfigDict(yaml_file="config/app.yaml")
model_config = SettingsConfigDict(yaml_file="config/app-merged.yaml")

@classmethod
def _load_yaml_file(
cls,
path: str,
required: bool,
) -> dict:
try:
with open(path, "r", encoding="utf-8") as file:
return yaml.safe_load(file)
except OSError as e:
if required:
raise e
return {}

@classmethod
def _load_merged_config(cls) -> dict:
default_config = cls._load_yaml_file("config/app-default.yaml", required=True)
custom_config = cls._load_yaml_file("config/app-custom.yaml", required=False)
return deep_update(default_config, custom_config)

@classmethod
def settings_customise_sources(
Expand All @@ -82,6 +104,9 @@ def settings_customise_sources(
*args,
**kwargs,
) -> tuple[PydanticBaseSettingsSource, ...]:
merged_config = cls._load_merged_config()
with open("config/app-merged.yaml", "w", encoding="utf-8") as file:
yaml.safe_dump(merged_config, file)
yaml_settings_source = YamlConfigSettingsSource(settings_cls)
return (yaml_settings_source,)

Expand Down

0 comments on commit 781da7d

Please sign in to comment.