162 lines
4.7 KiB
Python
162 lines
4.7 KiB
Python
"""Runtime configuration.
|
|
|
|
Settings can be supplied through (in increasing priority):
|
|
|
|
1. the defaults declared below,
|
|
2. a ``.env`` file / process environment using the ``GTCG_`` prefix, e.g.
|
|
``GTCG_LANGUAGE=fr`` or ``GTCG_CARD__DPI=600`` (nested keys use ``__``).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
__all__ = ["BASE_DPI", "CardSettings", "PageSettings", "RenderSettings", "Settings"]
|
|
|
|
MM_PER_INCH = 25.4
|
|
#: The reference resolution the card templates are designed against (63x88mm).
|
|
BASE_DPI = 300
|
|
|
|
DEFAULT_SOURCE_BASE_URL = "https://raw.githubusercontent.com/MadeBaruna/paimon-moe/main"
|
|
|
|
|
|
class CardSettings(BaseModel):
|
|
"""Physical and pixel geometry of a single card."""
|
|
|
|
width_mm: float = Field(default=63.0, gt=0)
|
|
height_mm: float = Field(default=88.0, gt=0)
|
|
dpi: int = Field(default=BASE_DPI, ge=BASE_DPI)
|
|
|
|
@property
|
|
def design_width_px(self) -> int:
|
|
"""Template pixel width (always computed at :data:`BASE_DPI`)."""
|
|
return round(self.width_mm / MM_PER_INCH * BASE_DPI)
|
|
|
|
@property
|
|
def design_height_px(self) -> int:
|
|
return round(self.height_mm / MM_PER_INCH * BASE_DPI)
|
|
|
|
@property
|
|
def output_width_px(self) -> int:
|
|
return round(self.design_width_px * self.scale_factor)
|
|
|
|
@property
|
|
def output_height_px(self) -> int:
|
|
return round(self.design_height_px * self.scale_factor)
|
|
|
|
@property
|
|
def scale_factor(self) -> float:
|
|
"""Playwright ``deviceScaleFactor`` needed to hit :attr:`dpi`."""
|
|
return self.dpi / BASE_DPI
|
|
|
|
|
|
class PageSettings(BaseModel):
|
|
"""A4 sheet geometry and grid."""
|
|
|
|
width_mm: float = Field(default=210.0, gt=0)
|
|
height_mm: float = Field(default=297.0, gt=0)
|
|
columns: int = Field(default=3, ge=1)
|
|
rows: int = Field(default=3, ge=1)
|
|
margin_mm: float = Field(default=0.0, ge=0, description="0 = auto-centre the grid")
|
|
gutter_mm: float = Field(default=0.0, ge=0)
|
|
cut_marks: bool = True
|
|
mark_length_mm: float = Field(default=4.0, gt=0)
|
|
|
|
@property
|
|
def capacity(self) -> int:
|
|
return self.columns * self.rows
|
|
|
|
|
|
class RenderSettings(BaseModel):
|
|
"""Browser-based rendering knobs."""
|
|
|
|
timeout_ms: int = Field(default=30_000, gt=0)
|
|
keep_html: bool = True
|
|
max_concurrency: int = Field(default=1, ge=1)
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""Top-level application settings."""
|
|
|
|
model_config = SettingsConfigDict(
|
|
env_prefix="GTCG_",
|
|
env_nested_delimiter="__",
|
|
env_file=".env",
|
|
extra="ignore",
|
|
)
|
|
|
|
language: str = Field(default="fr", min_length=2, max_length=8)
|
|
source_base_url: str = Field(default=DEFAULT_SOURCE_BASE_URL)
|
|
assets_dir: Path = Field(default=Path("assets"))
|
|
build_dir: Path = Field(default=Path("build"))
|
|
|
|
card: CardSettings = Field(default_factory=CardSettings)
|
|
page: PageSettings = Field(default_factory=PageSettings)
|
|
render: RenderSettings = Field(default_factory=RenderSettings)
|
|
|
|
@field_validator("source_base_url")
|
|
@classmethod
|
|
def _strip_trailing_slash(cls, value: str) -> str:
|
|
return value.rstrip("/")
|
|
|
|
# -- Derived paths ---------------------------------------------------------
|
|
|
|
@property
|
|
def source_dir(self) -> Path:
|
|
return self.assets_dir / "source"
|
|
|
|
@property
|
|
def source_json(self) -> Path:
|
|
return self.source_dir / f"{self.language}.json"
|
|
|
|
@property
|
|
def tags_json(self) -> Path:
|
|
return self.source_dir / f"tags_{self.language}.json"
|
|
|
|
@property
|
|
def wiki_dir(self) -> Path:
|
|
return self.source_dir / "wiki"
|
|
|
|
@property
|
|
def tcg_dir(self) -> Path:
|
|
return self.assets_dir / "tcg"
|
|
|
|
@property
|
|
def faction_icons_dir(self) -> Path:
|
|
return self.tcg_dir / "icons" / "factions"
|
|
|
|
@property
|
|
def factions_override(self) -> Path:
|
|
return self.assets_dir / "overrides" / "factions.json"
|
|
|
|
@property
|
|
def html_dir(self) -> Path:
|
|
return self.build_dir / "html"
|
|
|
|
@property
|
|
def png_dir(self) -> Path:
|
|
return self.build_dir / "png"
|
|
|
|
@property
|
|
def catalog_path(self) -> Path:
|
|
return self.build_dir / "catalog.json"
|
|
|
|
@property
|
|
def pdf_path(self) -> Path:
|
|
return self.build_dir / f"cards-{self.language}.pdf"
|
|
|
|
def ensure_directories(self) -> None:
|
|
"""Create every directory the pipeline writes to."""
|
|
for directory in (
|
|
self.source_dir,
|
|
self.wiki_dir,
|
|
self.tcg_dir,
|
|
self.html_dir,
|
|
self.png_dir,
|
|
self.build_dir,
|
|
):
|
|
directory.mkdir(parents=True, exist_ok=True)
|