Files
jobboard/roledesk/config.py
T
WolfandClaude Opus 5.5 4dfb8831aa Extract gate columns from posting text with a Haiku ModelTask
roledesk job extract reads open, unanswered roles 20 to a call and fills
place, country, remote, seniority, salary, currency, function, permit and
languages, only into columns the board left empty. Schema v2 adds
roles.permit, roles.languages and roles.extract_tries. Off by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:05:30 +02:00

74 lines
2.5 KiB
Python

"""Config loading: one YAML file, `roles.yaml`, validated into a model.
A typo in a key is an error naming the key, because `Section` forbids extras.
`sources` stays a dict: one block per plugin, whose keys only the plugin knows.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
import yaml
from pydantic import Field, ValidationError
from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig
DEFAULT_PATH = Path(os.environ.get("ROLEDESK_CONFIG", "roles.yaml"))
class ExtractConfig(ModelTaskConfig):
"""Turning a posting's title and text into the gate columns. See
roledesk/jobs/extract.py. Off by default, so nobody is billed by surprise:
on the API backend every batch is a paid call."""
enabled: bool = False
model: str = "claude-haiku-4-5-20251001"
batch_size: int = Field(20, ge=1)
class Config(Section):
"""The whole file."""
database: str = "roledesk.db"
plugin_paths: list[str] = Field(default_factory=lambda: ["plugins"])
workers: WorkersConfig = Field(default_factory=WorkersConfig)
agent: AgentConfig = Field(default_factory=AgentConfig)
extract: ExtractConfig = Field(default_factory=ExtractConfig)
#: One block per plugin: `enabled`, and whatever that plugin reads.
sources: dict[str, Any] = Field(default_factory=dict)
#: Where this was loaded from. Never written back into the file.
path: str = Field("", exclude=True)
class ConfigError(FileNotFoundError):
"""A config that exists but does not validate. A FileNotFoundError so the
CLI reports both kinds of unusable config the same way: as a message."""
def load(path: str | Path | None = None) -> Config:
p = Path(path or DEFAULT_PATH).expanduser()
if not p.exists():
raise FileNotFoundError(
f"No config at {p}. Copy config.example.yaml to {p} and edit it.")
with p.open(encoding="utf-8") as fh:
raw = yaml.safe_load(fh) or {}
try:
config = Config(**raw, path=str(p))
except ValidationError as exc:
raise ConfigError(f"{p}:\n{_explain(exc)}") from None
config.agent.credentials_database = config.database
return config
def _explain(exc: ValidationError) -> str:
lines = []
for err in exc.errors():
where = ".".join(str(p) for p in err["loc"]) or "(top level)"
detail = err["msg"]
if err["type"] == "extra_forbidden":
detail = "unknown setting, check the spelling against config.example.yaml"
lines.append(f" {where}: {detail}")
return "\n".join(lines)