-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load TOML configuration with Pydantic
Convert config format to TOML (in practice it's not that different) Use pydantic to load and validate configuration data. Normalise notifier configuration structure in preparation for adding more notifier types in future. Signed-off-by: Joe Groocock <[email protected]>
- Loading branch information
Showing
9 changed files
with
289 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FROM spritsail/alpine:3.19 | ||
|
||
ARG NOTIFY_VER=1.4 | ||
ARG NOTIFY_VER=1.5 | ||
|
||
LABEL maintainer="Adam Dodman <[email protected]>" \ | ||
org.label-schema.vendor="Spritsail" \ | ||
|
@@ -19,4 +19,4 @@ RUN --mount=type=bind,target=/src,rw \ | |
WORKDIR /config | ||
VOLUME ["/config"] | ||
|
||
CMD ["/usr/bin/python3", "-m", "drone_notify", "/config/notify.conf"] | ||
CMD ["/usr/bin/python3", "-m", "drone_notify"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
Configuration parsing, validation and representation for drone-notify | ||
configuration data. | ||
""" | ||
|
||
import os | ||
import tomllib | ||
from typing import Annotated, Any, Literal | ||
|
||
from pydantic import BaseModel, ConfigDict, Field, model_validator | ||
|
||
|
||
class StrictModel(BaseModel): | ||
""" | ||
Base pydantic model that enables strict model configuration | ||
This should be used as the base class for all configuration classes | ||
""" | ||
|
||
model_config = ConfigDict(strict=True, extra="forbid") | ||
|
||
|
||
class BaseNotifier(StrictModel): | ||
""" | ||
An abstract notifier type with no special service-specific behaviours | ||
""" | ||
|
||
kind: str | ||
status: list[str] | None = None | ||
repos: list[str] | None = None | ||
|
||
|
||
class TelegramNotifier(BaseNotifier): | ||
""" | ||
A Telegram notifier type that uses a bot to notify a Telegram channel | ||
""" | ||
|
||
kind: Literal["telegram"] | ||
bot: str | ||
channel: str | ||
|
||
|
||
class TelegramBot(StrictModel): | ||
""" | ||
A Bot object for sending messages to Telegram as a bot user | ||
""" | ||
|
||
bot_token: str | ||
|
||
|
||
class Telegram(StrictModel): | ||
""" | ||
Container for one or more Telegram bot definitions | ||
""" | ||
|
||
bot: dict[str, TelegramBot] | ||
|
||
|
||
class Main(StrictModel): | ||
""" | ||
Main application-level configuration options | ||
""" | ||
|
||
host: str = Field(default="::") | ||
port: int = Field(default=5000) | ||
secret: str | None = None | ||
debug: bool = False | ||
|
||
|
||
class Config(StrictModel): | ||
""" | ||
Top-level application configuration | ||
""" | ||
|
||
main: Main | ||
notifier: dict[str, Annotated[TelegramNotifier, Field(discriminator="kind")]] | ||
telegram: Telegram | None | ||
|
||
@model_validator(mode="after") | ||
def match_notifiers(self) -> "Config": | ||
""" | ||
Validates that each notifier references a defined bot object | ||
""" | ||
for name, notif in self.notifier.items(): | ||
bots: dict[str, Any] = getattr(getattr(self, notif.kind), "bot") | ||
if notif.bot not in bots: | ||
raise ValueError( | ||
f"Notifier '{name}' references undefined {notif.kind} bot '{notif.bot}'" | ||
) | ||
|
||
return self | ||
|
||
|
||
def load_toml_file(path: str | bytes | os.PathLike[str]) -> Config: | ||
""" | ||
Loads a Config object from an ini file given a path | ||
""" | ||
with open(path, "rb") as fp: | ||
return Config(**tomllib.load(fp)) |
Oops, something went wrong.