Fixed faction image fetching
This commit is contained in:
@@ -7,6 +7,7 @@ each step can be inspected before continuing.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
@@ -14,17 +15,29 @@ from pathlib import Path
|
||||
from genshin_tcg.config import Settings
|
||||
from genshin_tcg.data.assets import download_required_assets
|
||||
from genshin_tcg.data.catalog import Catalog, load_catalog, load_raw_source, save_catalog
|
||||
from genshin_tcg.data.fandom import (
|
||||
FactionIconReport,
|
||||
FactionIconSource,
|
||||
FactionResolution,
|
||||
FactionSource,
|
||||
FandomSource,
|
||||
resolve_factions,
|
||||
sync_faction_icons,
|
||||
)
|
||||
from genshin_tcg.data.paimon import DownloadReport, PaimonSource
|
||||
from genshin_tcg.logging_setup import get_logger
|
||||
from genshin_tcg.models import parse_catalog
|
||||
from genshin_tcg.pdf.imposition import ImpositionGrid, ImpositionResult, impose_pdf
|
||||
from genshin_tcg.rendering.context import load_faction_overrides
|
||||
from genshin_tcg.rendering.converters import HtmlConverter, PlaywrightConverter, Viewport
|
||||
from genshin_tcg.rendering.renderer import CardRenderer, RenderedCard, safe_name
|
||||
|
||||
__all__ = [
|
||||
"BuildResult",
|
||||
"FactionResult",
|
||||
"FetchResult",
|
||||
"build",
|
||||
"factions",
|
||||
"fetch",
|
||||
"impose",
|
||||
"render",
|
||||
@@ -47,6 +60,18 @@ class BuildResult:
|
||||
imposition: ImpositionResult
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class FactionResult:
|
||||
"""Outcome of :func:`factions` (wiki resolution + override file update)."""
|
||||
|
||||
resolution: FactionResolution
|
||||
output: Path
|
||||
written: bool
|
||||
added: tuple[str, ...] = ()
|
||||
conflicts: tuple[str, ...] = ()
|
||||
icons: FactionIconReport | None = None
|
||||
|
||||
|
||||
def fetch(
|
||||
settings: Settings,
|
||||
*,
|
||||
@@ -151,6 +176,87 @@ def build(
|
||||
return BuildResult(fetch=fetch_result, rendered=rendered, imposition=imposition)
|
||||
|
||||
|
||||
def factions(
|
||||
settings: Settings,
|
||||
*,
|
||||
overwrite: bool = False,
|
||||
force: bool = False,
|
||||
write: bool = True,
|
||||
icons: bool = True,
|
||||
source: FactionSource | None = None,
|
||||
icon_source: FactionIconSource | None = None,
|
||||
) -> FactionResult:
|
||||
"""Resolve card factions from the wiki and refresh the override file.
|
||||
|
||||
Playable characters are matched to their wiki region and enemies to their
|
||||
family (see :mod:`genshin_tcg.data.fandom`). Unless *overwrite* is set, the
|
||||
existing ``assets/overrides/factions.json`` is preserved: hand-made entries
|
||||
win over scraped ones and only missing keys are added, so the file stays
|
||||
safe to edit by hand.
|
||||
|
||||
When *icons* is set, the nation emblems are downloaded into
|
||||
``assets/tcg/icons/factions/<faction>.png`` so the renderer shows an image
|
||||
instead of the text label. *source* / *icon_source* inject a fake wiki
|
||||
(tests); *write* ``False`` performs a dry run.
|
||||
"""
|
||||
catalog = _load_catalogue(settings)
|
||||
icon_report: FactionIconReport | None = None
|
||||
if source is None:
|
||||
with FandomSource(cache_dir=settings.wiki_dir, force=force) as wiki:
|
||||
resolution = resolve_factions(catalog, source=wiki)
|
||||
if icons:
|
||||
icon_report = sync_faction_icons(
|
||||
resolution,
|
||||
source=icon_source or wiki,
|
||||
assets_dir=settings.faction_icons_dir,
|
||||
force=force,
|
||||
)
|
||||
else:
|
||||
resolution = resolve_factions(catalog, source=source)
|
||||
if icons and icon_source is not None:
|
||||
icon_report = sync_faction_icons(
|
||||
resolution,
|
||||
source=icon_source,
|
||||
assets_dir=settings.faction_icons_dir,
|
||||
force=force,
|
||||
)
|
||||
|
||||
existing = {} if overwrite else load_faction_overrides(settings.factions_override)
|
||||
added = tuple(sorted(set(resolution.assignments) - set(existing)))
|
||||
conflicts = tuple(
|
||||
sorted(
|
||||
card_id
|
||||
for card_id, faction in resolution.assignments.items()
|
||||
if card_id in existing and existing[card_id] != faction
|
||||
)
|
||||
)
|
||||
merged: dict[str, str] = {**resolution.assignments, **existing}
|
||||
if conflicts:
|
||||
_logger.warning(
|
||||
"Kept %d hand-made faction entry/entries over the wiki values: %s",
|
||||
len(conflicts),
|
||||
", ".join(conflicts),
|
||||
)
|
||||
|
||||
output = settings.factions_override
|
||||
written = False
|
||||
if write:
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = json.dumps(dict(sorted(merged.items())), ensure_ascii=False, indent=2)
|
||||
output.write_text(payload + "\n", encoding="utf-8")
|
||||
written = True
|
||||
_logger.info("Wrote %d faction override(s) to %s", len(merged), output)
|
||||
|
||||
return FactionResult(
|
||||
resolution=resolution,
|
||||
output=output,
|
||||
written=written,
|
||||
added=added,
|
||||
conflicts=conflicts,
|
||||
icons=icon_report,
|
||||
)
|
||||
|
||||
|
||||
def _load_catalogue(settings: Settings) -> Catalog:
|
||||
if not settings.catalog_path.is_file():
|
||||
raise FileNotFoundError(
|
||||
|
||||
Reference in New Issue
Block a user