Skip to content

Commit

Permalink
Rename settings.yaml to goosebit.yaml, and check multiple places for …
Browse files Browse the repository at this point in the history
…settings.

Use can now pass `GOOSEBIT_SETTINGS` to customize settings location, as well as goosebit auto checking `/etc/goosebit` if it exists, the root of the project directory, and the current working directory when it is run.
This should allow for more optionality when loading settings files.

Renaming settings to `goosebit.yaml` is mainly to specify what the file is when running from CWD, it should be distinct from other files.

Fixes: #103
  • Loading branch information
UpstreamData authored and b-rowan committed Aug 31, 2024
1 parent 6b0fc33 commit bc48b10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion goosebit/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .const import BASE_DIR, PWD_CXT # noqa: F401
from .const import PWD_CXT # noqa: F401
from .schema import GooseBitSettings

config = GooseBitSettings()
Expand Down
5 changes: 4 additions & 1 deletion goosebit/settings/const.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from pathlib import Path

from argon2 import PasswordHasher

BASE_DIR = Path(__file__).resolve().parent.parent.parent
GOOSEBIT_ROOT_DIR = Path(__file__).resolve().parent.parent.parent
CURRENT_DIR = Path(os.getcwd())
ETC_SETTINGS_DIR = Path("/etc/goosebit")

PWD_CXT = PasswordHasher()

Expand Down
36 changes: 28 additions & 8 deletions goosebit/settings/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import secrets
from pathlib import Path
from typing import Annotated
Expand All @@ -11,7 +12,13 @@
YamlConfigSettingsSource,
)

from .const import BASE_DIR, LOGGING_DEFAULT, PWD_CXT
from .const import (
CURRENT_DIR,
ETC_SETTINGS_DIR,
GOOSEBIT_ROOT_DIR,
LOGGING_DEFAULT,
PWD_CXT,
)


class User(BaseModel):
Expand All @@ -36,8 +43,6 @@ class GooseBitSettings(BaseSettings):

port: int = 60053 # GOOSE

artifacts_dir: Path = BASE_DIR.joinpath("artifacts")

poll_time_default: str = "00:01:00"
poll_time_updating: str = "00:00:05"
poll_time_registration: str = "00:00:10"
Expand All @@ -46,7 +51,8 @@ class GooseBitSettings(BaseSettings):

users: list[User] = []

db_uri: str = f"sqlite:///{BASE_DIR.joinpath('db.sqlite3')}"
db_uri: str = f"sqlite:///{GOOSEBIT_ROOT_DIR.joinpath('db.sqlite3')}"
artifacts_dir: Path = GOOSEBIT_ROOT_DIR.joinpath("artifacts")

metrics: MetricsSettings = MetricsSettings()

Expand All @@ -61,9 +67,23 @@ def settings_customise_sources(
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (
settings_sources = [
init_settings,
YamlConfigSettingsSource(settings_cls, BASE_DIR.joinpath("settings.yaml")),
env_settings,
file_secret_settings,
YamlConfigSettingsSource(settings_cls, GOOSEBIT_ROOT_DIR.joinpath("goosebit.yaml")),
YamlConfigSettingsSource(settings_cls, CURRENT_DIR.joinpath("goosebit.yaml")),
]
if ETC_SETTINGS_DIR.exists():
settings_sources.append(
YamlConfigSettingsSource(settings_cls, ETC_SETTINGS_DIR.joinpath("goosebit.yaml")),
)
if os.getenv("GOOSEBIT_SETTINGS"):
settings_sources.append(
YamlConfigSettingsSource(settings_cls, os.getenv("GOOSEBIT_SETTINGS")),
)
settings_sources.extend(
[
env_settings,
file_secret_settings,
]
)
return tuple(settings_sources)

0 comments on commit bc48b10

Please sign in to comment.