| """ |
| DataController β loads and validates all JSON data files at startup. |
| Exposes typed AppData to the rest of the app. |
| """ |
| import json |
| from pathlib import Path |
|
|
| from models import AppData, ImpCategory, IvCategory, TongueTwister |
| from models.data_models import TTLevels |
|
|
|
|
| DATA_DIR = Path(__file__).parent.parent / "src" / "data" |
|
|
|
|
| class DataController: |
| def __init__(self) -> None: |
| jam = json.loads((DATA_DIR / "jam.json").read_text(encoding="utf-8")) |
| tt_raw = json.loads((DATA_DIR / "tongue-twisters.json").read_text(encoding="utf-8")) |
| imp_raw = json.loads((DATA_DIR / "impromptu.json").read_text(encoding="utf-8")) |
| iv_raw = json.loads((DATA_DIR / "interview.json").read_text(encoding="utf-8")) |
|
|
| self.data = AppData( |
| jam=jam, |
| tt=TTLevels( |
| easy=[TongueTwister(**i) for i in tt_raw["easy"]], |
| medium=[TongueTwister(**i) for i in tt_raw["medium"]], |
| hard=[TongueTwister(**i) for i in tt_raw["hard"]], |
| ), |
| impromptu=[ImpCategory(**c) for c in imp_raw], |
| interview=[IvCategory(**c) for c in iv_raw], |
| ) |
|
|
| |
|
|
| @property |
| def jam_prompts(self) -> list[str]: |
| return self.data.jam |
|
|
| def tt_items(self, level: str) -> list[TongueTwister]: |
| return getattr(self.data.tt, level) |
|
|
| @property |
| def imp_categories(self) -> list[ImpCategory]: |
| return self.data.impromptu |
|
|
| @property |
| def imp_category_names(self) -> list[str]: |
| return [c.category for c in self.data.impromptu] |
|
|
| def imp_questions(self, cat_name: str) -> list[str]: |
| """Return questions for a category name, or all questions if 'All'.""" |
| if cat_name == "All": |
| return [q for c in self.data.impromptu for q in c.questions] |
| for c in self.data.impromptu: |
| if c.category == cat_name: |
| return c.questions |
| return [] |
|
|
| @property |
| def iv_categories(self) -> list[IvCategory]: |
| return self.data.interview |
|
|
| @property |
| def iv_category_names(self) -> list[str]: |
| return [c.category for c in self.data.interview] |
|
|
| def iv_questions(self, cat_name: str) -> list[str]: |
| """Return questions for a category name, or all questions if 'All'.""" |
| if cat_name == "All": |
| return [q for c in self.data.interview for q in c.questions] |
| for c in self.data.interview: |
| if c.category == cat_name: |
| return c.questions |
| return [] |
|
|