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

Better settings parsing and pip installation handling #106

Merged
merged 2 commits into from
Sep 4, 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
File renamed without changes.
7 changes: 7 additions & 0 deletions goosebit/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import uvicorn

from goosebit import app
from goosebit.settings import config

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=config.port)
b-rowan marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 12 additions & 1 deletion goosebit/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from .const import BASE_DIR, PWD_CXT # noqa: F401
import logging.config

from .const import PWD_CXT # noqa: F401
from .schema import GooseBitSettings

config = GooseBitSettings()


logging.config.dictConfig(config.logging)

logger = logging.getLogger(__name__)

if config.config_file is not None:
logger.info(f"Loading settings from: {config.config_file}")


USERS = {u.username: u for u in config.users}
4 changes: 3 additions & 1 deletion goosebit/settings/const.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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())

PWD_CXT = PasswordHasher()

Expand Down
35 changes: 26 additions & 9 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,7 @@
YamlConfigSettingsSource,
)

from .const import BASE_DIR, LOGGING_DEFAULT, PWD_CXT
from .const import CURRENT_DIR, GOOSEBIT_ROOT_DIR, LOGGING_DEFAULT, PWD_CXT


class User(BaseModel):
Expand All @@ -36,8 +37,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 +45,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')}"
b-rowan marked this conversation as resolved.
Show resolved Hide resolved
artifacts_dir: Path = GOOSEBIT_ROOT_DIR.joinpath("artifacts")

metrics: MetricsSettings = MetricsSettings()

Expand All @@ -61,9 +61,26 @@ def settings_customise_sources(
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (
init_settings,
YamlConfigSettingsSource(settings_cls, BASE_DIR.joinpath("settings.yaml")),
env_settings,
file_secret_settings,
settings_sources = [env_settings]
config_files = []

if (path := os.getenv("GOOSEBIT_SETTINGS")) is not None:
config_files.append(Path(path))

config_files.extend(
[
CURRENT_DIR.joinpath("goosebit.yaml"),
GOOSEBIT_ROOT_DIR.joinpath("goosebit.yaml"),
Path("/etc/goosebit.yaml"),
]
)

cls.config_file = None
for config_file in config_files:
b-rowan marked this conversation as resolved.
Show resolved Hide resolved
if config_file.exists():
settings_sources.append(
YamlConfigSettingsSource(settings_cls, config_file),
)
cls.config_file = config_file
break
return tuple(settings_sources)
5 changes: 0 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import logging.config

import uvicorn

from goosebit import app
from goosebit.settings import config

logging.config.dictConfig(config.logging)


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=config.port)