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

DM-42937: Accept timedelta config values as strings #154

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
14 changes: 13 additions & 1 deletion src/vocutouts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import timedelta
from pathlib import Path

from pydantic import Field, PostgresDsn
from pydantic import Field, PostgresDsn, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from safir.logging import LogLevel, Profile

Expand Down Expand Up @@ -91,6 +91,18 @@ class Config(BaseSettings):
env_prefix="CUTOUT_", case_sensitive=False
)

@field_validator("lifetime", "sync_timeout", "timeout", mode="before")
@classmethod
def _parse_as_seconds(cls, v: int | str | timedelta) -> int | timedelta:
"""Convert timedelta strings so they are parsed as seconds."""
if isinstance(v, timedelta):
return v
try:
return int(v)
except ValueError as e:
msg = f"value {v} must be an integer number of seconds"
raise ValueError(msg) from e

def uws_config(self) -> UWSConfig:
"""Convert to configuration for the UWS subsystem."""
return UWSConfig(
Expand Down