#!/usr/bin/env python3 import csv import re import sys from pathlib import Path # ============================================================================= # Configuration # ============================================================================= COLUMNS = [ "Technique", "Niv", "Type", "Sang", "Jauge", "Autre", "Effet", "Support", "Dégats", "Survivabilité", "Mobilité", "C.C", "Altérations", "QoL", "AB", "Compléxité", ] STAT_COLUMNS = COLUMNS[7:] # ============================================================================= # Helpers # ============================================================================= def parse_cost(cost: str) -> tuple[str, str, str]: """ Split a cost into Sang / Jauge / Autre. Examples: 4🩸 -> ("4", "", "") 2⚡ -> ("", "2", "") 4🩸2⚡ -> ("4", "2", "") 3🩸1⚡2◆ -> ("3", "1", "2") Multiple occurrences of the same resource are summed. """ blood = 0 gauge = 0 other = 0 matches = re.findall( r"(\d+(?:\.\d+)?)\s*(🩸|⚡|◆)", cost, ) for value, symbol in matches: value = float(value) if symbol == "🩸": blood += value elif symbol == "⚡": gauge += value elif symbol == "◆": other += value def clean(value: float) -> str: if value == 0: return "" if value.is_integer(): return str(int(value)) return str(value) return ( clean(blood), clean(gauge), clean(other), ) def is_empty(row: list[str]) -> bool: """Return whether a row contains no meaningful data.""" return not row or all(cell.strip() == "" for cell in row) def is_tier(row: list[str]) -> bool: """Return whether a row is a Tier marker.""" return bool(row) and row[0].strip().startswith("Tier ") def normalize_length(row: list[str], length: int) -> list[str]: """ Make a row exactly `length` columns wide. Missing columns are filled with empty strings. Extra columns are discarded. """ row = row[:length] if len(row) < length: row.extend([""] * (length - len(row))) return row # ============================================================================= # Conversion # ============================================================================= def convert(input_path: Path, output_path: Path) -> None: with input_path.open( "r", encoding="utf-8", newline="", ) as source: reader = csv.reader( source, delimiter="\t", ) rows = [] for row in reader: # --------------------------------------------------------------- # Empty rows # --------------------------------------------------------------- if is_empty(row): rows.append([]) continue # --------------------------------------------------------------- # Tier markers # # Keep the tier row as-is. This means all its existing tabs # remain untouched. # --------------------------------------------------------------- if is_tier(row): rows.append(row) continue # --------------------------------------------------------------- # Header # # Regardless of what the old header contains, always emit the # complete canonical schema. # --------------------------------------------------------------- if row[0].strip() == "Technique": rows.append(COLUMNS.copy()) continue # --------------------------------------------------------------- # Technique row # --------------------------------------------------------------- # Old minimum layout: # # 0 Technique # 1 Niv # 2 Type # 3 Coût # 4 empty # 5 Effet # row = normalize_length(row, max(6, len(row))) technique = row[0] level = row[1] type_ = row[2] cost = row[3] effect = row[5] # --------------------------------------------------------------- # Split cost # --------------------------------------------------------------- blood, gauge, other = parse_cost(cost) # --------------------------------------------------------------- # Preserve statistic columns # # Old columns after Effet are already in the correct order: # # Support # Dégats # Survivabilité # Mobilité # C.C # Altérations # QoL # AB # Compléxité # # Missing fields are padded with empty strings. # --------------------------------------------------------------- stats = row[6:] stats = normalize_length( stats, len(STAT_COLUMNS), ) # --------------------------------------------------------------- # Build canonical row # --------------------------------------------------------------- new_row = [ technique, level, type_, blood, gauge, other, effect, *stats, ] new_row = normalize_length( new_row, len(COLUMNS), ) rows.append(new_row) # ============================================================================= # Write output # ============================================================================= with output_path.open( "w", encoding="utf-8", newline="", ) as destination: writer = csv.writer( destination, delimiter="\t", lineterminator="\n", ) writer.writerows(rows) # ============================================================================= # CLI # ============================================================================= def main() -> None: if len(sys.argv) != 3: print( f"Usage: {Path(sys.argv[0]).name} INPUT.tsv OUTPUT.tsv", file=sys.stderr, ) sys.exit(1) input_path = Path(sys.argv[1]) output_path = Path(sys.argv[2]) if not input_path.exists(): print( f"Error: input file does not exist: {input_path}", file=sys.stderr, ) sys.exit(1) convert( input_path, output_path, ) print(f"Converted: {input_path} -> {output_path}") if __name__ == "__main__": main()