trainer_info_view.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. Trainer SubView: Info (F1).
  4. Zeigt allgemeine Trainer-Informationen als StatusTable:
  5. Name, Version, Autor, Modell-Typ, Trainer-Typ.
  6. """
  7. from __future__ import annotations
  8. from textual.app import ComposeResult
  9. from textual.containers import Vertical
  10. from textual.widgets import Static
  11. from trixy_core.tui.views.base import MainView
  12. from trixy_core.tui.widgets.status_table import StatusTable
  13. class TrainerInfoView(MainView):
  14. """F1 im Trainer-Kontext: Allgemeine Trainer-Informationen."""
  15. TITLE = "Info"
  16. VIEW_ID = "trainer_info"
  17. def __init__(self, **kwargs) -> None:
  18. super().__init__(**kwargs)
  19. self._trainer_data: dict = {}
  20. self._detail_data: object | None = None
  21. def set_trainer(self, trainer_data: dict) -> None:
  22. """Setzt die Trainer-Basisdaten."""
  23. self._trainer_data = trainer_data
  24. self._detail_data = None
  25. def compose(self) -> ComposeResult:
  26. with Vertical(classes="trainer-section"):
  27. yield Static("[bold]Trainer-Informationen[/bold]", classes="trainer-section-header")
  28. yield StatusTable(id="trainer-info-table")
  29. with Vertical(classes="trainer-section"):
  30. yield Static("[bold]Beschreibung[/bold]", classes="trainer-section-header")
  31. yield Static("", id="trainer-info-desc")
  32. async def on_activate(self) -> None:
  33. """Laedt Detail-Daten vom Server."""
  34. await self._load_detail()
  35. async def refresh_data(self) -> None:
  36. """Kein periodisches Update noetig fuer statische Info."""
  37. pass
  38. async def _load_detail(self) -> None:
  39. """Laedt detaillierte Trainer-Daten."""
  40. if not self.connection or not self._trainer_data:
  41. return
  42. trainer_id = self._trainer_data.get("trainer_id", "")
  43. if not trainer_id:
  44. return
  45. try:
  46. detail = await self.connection.request_trainer_detail(trainer_id)
  47. except Exception:
  48. return
  49. if detail is None or detail.error:
  50. return
  51. self._detail_data = detail
  52. self._populate(detail)
  53. def _populate(self, detail: object) -> None:
  54. """Befuellt die Anzeige mit Detail-Daten."""
  55. table = self.query_one("#trainer-info-table", StatusTable)
  56. table.update_data([
  57. ("Name", getattr(detail, "name", "")),
  58. ("Version", getattr(detail, "version", "")),
  59. ("Autor", getattr(detail, "author", "")),
  60. ("Modell-Typ", getattr(detail, "model_type", "")),
  61. ("Trainer-Typ", getattr(detail, "trainer_type", "")),
  62. ("Trainer-ID", getattr(detail, "trainer_id", "")),
  63. ])
  64. desc = self.query_one("#trainer-info-desc", Static)
  65. desc.update(getattr(detail, "description", "Keine Beschreibung"))