56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
"""Statistiques liées aux minifigs pour les sets filtrés."""
|
|
|
|
from pathlib import Path
|
|
from typing import Dict, Iterable, List, Sequence, Tuple
|
|
|
|
from lib.rebrickable.parts_inventory import index_inventory_minifigs_by_inventory, select_latest_inventories
|
|
from lib.rebrickable.stats import read_rows
|
|
|
|
|
|
MINIFIG_TOTAL_LABEL = "Nombre total de minifigs (thèmes filtrés)"
|
|
TOTAL_PARTS_LABEL = "Total de pièces pour les thèmes filtrés"
|
|
|
|
|
|
def load_filtered_sets(path: Path) -> List[dict]:
|
|
"""Charge les sets filtrés depuis un CSV."""
|
|
return read_rows(path)
|
|
|
|
|
|
def compute_total_minifigs(
|
|
filtered_sets: Iterable[dict],
|
|
inventories: Dict[str, dict],
|
|
minifigs_by_inventory: Dict[str, List[dict]],
|
|
) -> int:
|
|
"""Additionne les minifigs présentes dans les inventaires des sets filtrés."""
|
|
total = 0
|
|
for set_row in filtered_sets:
|
|
inventory = inventories[set_row["set_num"]]
|
|
for minifig_row in minifigs_by_inventory.get(inventory["id"], []):
|
|
total += int(minifig_row["quantity"])
|
|
return total
|
|
|
|
|
|
def compute_filtered_minifig_total(
|
|
filtered_sets: Iterable[dict],
|
|
inventories_path: Path,
|
|
inventory_minifigs_path: Path,
|
|
) -> int:
|
|
"""Calcule le total de minifigs pour les sets filtrés à partir des CSV bruts."""
|
|
inventories = select_latest_inventories(inventories_path)
|
|
minifigs_by_inventory = index_inventory_minifigs_by_inventory(inventory_minifigs_path)
|
|
return compute_total_minifigs(filtered_sets, inventories, minifigs_by_inventory)
|
|
|
|
|
|
def merge_minifig_stat(stats: Sequence[Tuple[str, str]], total_minifigs: int) -> List[Tuple[str, str]]:
|
|
"""Insère le total de minifigs en évitant les doublons et en préservant l'ordre."""
|
|
filtered_stats = [(label, value) for label, value in stats if label != MINIFIG_TOTAL_LABEL]
|
|
insertion_index = next(
|
|
(index for index, (label, _) in enumerate(filtered_stats) if label == TOTAL_PARTS_LABEL),
|
|
len(filtered_stats),
|
|
)
|
|
return (
|
|
filtered_stats[: insertion_index + 1]
|
|
+ [(MINIFIG_TOTAL_LABEL, str(total_minifigs))]
|
|
+ filtered_stats[insertion_index + 1 :]
|
|
)
|