Files

80 lines
3.0 KiB
Python

"""Patch a Show from a stage map."""
from __future__ import annotations
import json
from dataclasses import dataclass, field
from pathlib import Path
from .model import Fixture, Show
PROJECT_ROOT = Path(__file__).resolve().parents[1]
@dataclass
class StageRig:
path: Path
name: str
cans: list[Fixture] = field(default_factory=list)
movers: list[Fixture] = field(default_factory=list)
def __getitem__(self, key: str) -> Fixture:
for fixture in (*self.cans, *self.movers):
if fixture.key == key:
return fixture
raise KeyError(key)
def patch_stage(show: Show, path: str | Path | None = None) -> StageRig:
"""Patch every fixture in the stage map and group cans and movers."""
source = _map_path(path, show.root or PROJECT_ROOT)
document = json.loads(source.read_text(encoding="utf-8"))
definitions = document.get("definitions")
fixtures = document.get("fixtures")
if not isinstance(definitions, dict) or not isinstance(fixtures, dict):
raise ValueError(f"{source} needs definitions and fixtures")
rig = StageRig(path=source, name=str(document.get("name") or source.stem))
for key, raw in fixtures.items():
if not isinstance(raw, dict):
raise ValueError(f"{key} is not a fixture")
defin = definitions.get(raw.get("def"))
if not isinstance(defin, dict):
raise ValueError(f"{key} has no definition {raw.get('def')!r}")
fixture = show.patch(
str(defin["manufacturer"]),
str(defin["model"]),
str(defin["mode"]),
address=int(raw["address"]),
universe=int(raw.get("universe", 0)),
name=str(raw.get("label") or key),
key=str(key),
)
listed = raw.get("channels")
if isinstance(listed, int) and listed != fixture.channels:
raise ValueError(f"{key} says {listed} channels; {defin['mode']} has {fixture.channels}")
fixture.x = float(raw["x"]) if "x" in raw else None
fixture.y = float(raw["y"]) if "y" in raw else None
fixture.direction = float(raw["direction"]) if "direction" in raw else None
fixture.zero = float(raw["zero"]) if "zero" in raw else None
fixture.kind = str(raw.get("kind") or "")
if fixture.kind == "mover":
rig.movers.append(fixture)
else:
rig.cans.append(fixture)
if rig.cans:
show.group("Cans", rig.cans, columns=len(rig.cans))
if rig.movers:
show.group("Movers", rig.movers, columns=len(rig.movers))
return rig
def _map_path(path: str | Path | None, root: Path) -> Path:
source = Path(path) if path is not None else root / "stage" / "map.json"
base = root.resolve()
target = source.resolve() if source.is_absolute() else (base / source).resolve()
if target != base and base not in target.parents:
raise ValueError(f"{path} is outside the project")
if not target.is_file():
raise ValueError(f"stage map not found: {path or target}")
return target