- event bus systen - all components are independent - preparation for web frontend
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""What the reactions get handed: the three objects, the bus, and a little shared state."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from dataclasses import dataclass, field
|
|
|
|
from musicmouse.bus import EventBus
|
|
from musicmouse.clock import Clock, RealClock
|
|
from musicmouse.config import Config, FigureColors
|
|
from musicmouse.devices.mouse import MusicMouseDevice
|
|
from musicmouse.devices.player import Player
|
|
from musicmouse.media import Playlist
|
|
|
|
_log = logging.getLogger(__name__)
|
|
|
|
__all__ = ["App", "AppState"]
|
|
|
|
|
|
@dataclass
|
|
class AppState:
|
|
"""State that belongs to no single device but is shared between reactions."""
|
|
|
|
#: Figure that was taken off the reader mid-playlist, so putting it back resumes
|
|
#: instead of starting over. Cleared once its playlist runs out.
|
|
last_partially_played_figure: str | None = None
|
|
|
|
|
|
@dataclass
|
|
class App:
|
|
config: Config
|
|
bus: EventBus
|
|
mouse: MusicMouseDevice
|
|
player: Player
|
|
playlists: dict[str, Playlist]
|
|
clock: Clock = field(default_factory=RealClock)
|
|
state: AppState = field(default_factory=AppState)
|
|
|
|
def colors(self, figure: str) -> FigureColors:
|
|
return self.config.figures[figure].colors
|
|
|
|
def playlist(self, figure: str) -> Playlist | None:
|
|
playlist = self.playlists.get(figure)
|
|
if playlist is None:
|
|
_log.warning("No playlist for figure %r", figure)
|
|
return playlist
|