darachhat
feat: build production-ready Khmer Document Corpus v0.2.0 with Typer CLI, PyMuPDF, Polars, and DI architecture
c4e128a | """Application settings loaded from config.yaml + env vars. | |
| Priority (highest first): | |
| 1. Environment variables (prefixed KDC_) | |
| 2. configs/config.yaml | |
| 3. Field defaults | |
| """ | |
| from __future__ import annotations | |
| from functools import lru_cache | |
| from pathlib import Path | |
| import yaml | |
| from pydantic import Field | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| class PathSettings(BaseSettings): | |
| pdf_dir: Path = Path("pdf") | |
| preview_dir: Path = Path("preview") | |
| metadata_dir: Path = Path("metadata") | |
| datasets_dir: Path = Path("datasets") | |
| dedup_registry: Path = Path("metadata/.dedup_registry.parquet") | |
| model_config = SettingsConfigDict(env_prefix="KDC_", extra="ignore") | |
| class LoggingSettings(BaseSettings): | |
| level: str = "INFO" | |
| sink_file: Path | None = Path("logs/kdc.log") | |
| rotation: str = "10 MB" | |
| retention: str = "30 days" | |
| model_config = SettingsConfigDict(env_prefix="KDC_LOG_", extra="ignore") | |
| class PreviewSettings(BaseSettings): | |
| dpi: int = Field(default=150, ge=72, le=600) | |
| format: str = "png" | |
| max_pages: int = Field(default=1, ge=1) | |
| quality: int = Field(default=90, ge=1, le=100) | |
| model_config = SettingsConfigDict(env_prefix="KDC_PREVIEW_", extra="ignore") | |
| class MetadataSettings(BaseSettings): | |
| fallback_to_pypdf: bool = True | |
| min_file_size_kb: float = 1.0 | |
| max_file_size_mb: float = 500.0 | |
| model_config = SettingsConfigDict(env_prefix="KDC_METADATA_", extra="ignore") | |
| class DedupSettings(BaseSettings): | |
| enabled: bool = True | |
| strategy: str = "sha256" | |
| model_config = SettingsConfigDict(env_prefix="KDC_DEDUP_", extra="ignore") | |
| class HuggingFaceSettings(BaseSettings): | |
| repo_id: str = "Darachhat/khmer-document-corpus" | |
| token: str | None = None | |
| model_config = SettingsConfigDict(env_prefix="HF_", extra="ignore") | |
| class Settings(BaseSettings): | |
| """Top-level aggregated settings.""" | |
| paths: PathSettings = Field(default_factory=PathSettings) | |
| logging: LoggingSettings = Field(default_factory=LoggingSettings) | |
| preview: PreviewSettings = Field(default_factory=PreviewSettings) | |
| metadata: MetadataSettings = Field(default_factory=MetadataSettings) | |
| dedup: DedupSettings = Field(default_factory=DedupSettings) | |
| huggingface: HuggingFaceSettings = Field(default_factory=HuggingFaceSettings) | |
| model_config = SettingsConfigDict( | |
| env_file=".env", | |
| env_file_encoding="utf-8", | |
| extra="ignore", | |
| ) | |
| def from_yaml(cls, path: Path | None = None) -> Settings: | |
| """Load settings from a YAML file, falling back to defaults.""" | |
| yaml_path = path or Path("configs/config.yaml") | |
| raw: dict = {} | |
| if yaml_path.exists(): | |
| with yaml_path.open(encoding="utf-8") as f: | |
| raw = yaml.safe_load(f) or {} | |
| return cls( | |
| paths=PathSettings(**raw.get("paths", {})), | |
| logging=LoggingSettings(**raw.get("logging", {})), | |
| preview=PreviewSettings(**raw.get("preview", {})), | |
| metadata=MetadataSettings(**raw.get("metadata", {})), | |
| dedup=DedupSettings(**raw.get("dedup", {})), | |
| huggingface=HuggingFaceSettings(**raw.get("huggingface", {})), | |
| ) | |
| def get_settings() -> Settings: | |
| """Return the singleton Settings instance (cached after first call).""" | |
| return Settings.from_yaml() | |