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.
  • Loading branch information
b-rowan committed Aug 31, 2024
1 parent b2b0bd3 commit a98c235
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
File renamed without changes.
12 changes: 1 addition & 11 deletions goosebit/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import logging

from .const import BASE_DIR, CONFIG_DIR, PWD_CXT # noqa: F401
from .const import PWD_CXT # noqa: F401
from .schema import GooseBitSettings

logger = logging.getLogger(__name__)

if not (BASE_DIR.joinpath("settings.yaml").exists() or CONFIG_DIR.joinpath("settings.yaml").exists()):
logging.warning(
"Could not find settings files, using default settings. "
f"Please set a config in {CONFIG_DIR.joinpath('settings.yaml')}."
)

config = GooseBitSettings()

USERS = {u.username: u for u in config.users}
8 changes: 4 additions & 4 deletions goosebit/settings/const.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from pathlib import Path

import platformdirs
from argon2 import PasswordHasher

BASE_DIR = Path(__file__).resolve().parent.parent.parent
CONFIG_DIR = platformdirs.site_config_path("gooseBit")
DATA_DIR = platformdirs.site_data_path("gooseBit")
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, CONFIG_DIR, DATA_DIR, LOGGING_DEFAULT, PWD_CXT
from .const import (
CURRENT_DIR,
ETC_SETTINGS_DIR,
GOOSEBIT_ROOT_DIR,
LOGGING_DEFAULT,
PWD_CXT,
)


class User(BaseModel):
Expand Down Expand Up @@ -44,8 +51,8 @@ class GooseBitSettings(BaseSettings):

users: list[User] = []

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

metrics: MetricsSettings = MetricsSettings()

Expand All @@ -60,10 +67,23 @@ def settings_customise_sources(
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (
settings_sources = [
init_settings,
YamlConfigSettingsSource(settings_cls, CONFIG_DIR.joinpath("settings.yaml")),
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 a98c235

Please sign in to comment.