This commit is contained in:
Trey t
2026-01-19 22:12:53 -06:00
parent 11c0ae70d2
commit a8b0491571
19 changed files with 1328 additions and 525 deletions

View File

@@ -54,9 +54,28 @@ class Team:
"stadium_id": self.stadium_id,
}
def to_canonical_dict(self) -> dict:
"""Convert to canonical dictionary format matching iOS app schema.
Returns:
Dictionary with field names matching JSONCanonicalTeam in BootstrapService.swift
"""
return {
"canonical_id": self.id,
"name": self.name,
"abbreviation": self.abbreviation,
"sport": self.sport,
"city": self.city,
"stadium_canonical_id": self.stadium_id or "",
"conference_id": self.conference,
"division_id": self.division,
"primary_color": self.primary_color,
"secondary_color": self.secondary_color,
}
@classmethod
def from_dict(cls, data: dict) -> "Team":
"""Create a Team from a dictionary."""
"""Create a Team from a dictionary (internal format)."""
return cls(
id=data["id"],
sport=data["sport"],
@@ -72,6 +91,23 @@ class Team:
stadium_id=data.get("stadium_id"),
)
@classmethod
def from_canonical_dict(cls, data: dict) -> "Team":
"""Create a Team from a canonical dictionary (iOS app format)."""
return cls(
id=data["canonical_id"],
sport=data["sport"],
city=data["city"],
name=data["name"],
full_name=f"{data['city']} {data['name']}", # Reconstruct full_name
abbreviation=data["abbreviation"],
conference=data.get("conference_id"),
division=data.get("division_id"),
primary_color=data.get("primary_color"),
secondary_color=data.get("secondary_color"),
stadium_id=data.get("stadium_canonical_id"),
)
def to_json(self) -> str:
"""Serialize to JSON string."""
return json.dumps(self.to_dict(), indent=2)
@@ -89,7 +125,10 @@ def save_teams(teams: list[Team], filepath: str) -> None:
def load_teams(filepath: str) -> list[Team]:
"""Load a list of teams from a JSON file."""
"""Load a list of teams from a JSON file (auto-detects format)."""
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
# Detect format: canonical has "canonical_id", internal has "id"
if data and "canonical_id" in data[0]:
return [Team.from_canonical_dict(d) for d in data]
return [Team.from_dict(d) for d in data]