75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""Command line for the show tools. Each command prints one JSON object."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from .spec import PROJECT_ROOT
|
|
from .tools import list_fixtures, run_script, sample_animation, show_schema, validate_show, write_show
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(prog="showfile", description="Build and test QLC+ shows.")
|
|
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
|
|
sub.add_parser("list-fixtures", help="List fixture modes and channel numbers.")
|
|
sub.add_parser("schema", help="Print the show spec JSON Schema.")
|
|
|
|
validate = sub.add_parser("validate", help="Validate a show spec without writing.")
|
|
validate.add_argument("spec")
|
|
|
|
write = sub.add_parser("write", help="Compile a show spec to a .qxw.")
|
|
write.add_argument("spec")
|
|
write.add_argument("-o", "--output", required=True)
|
|
|
|
sample = sub.add_parser("sample", help="Sample an efx or path block.")
|
|
sample.add_argument("block")
|
|
sample.add_argument("--steps", type=int, default=32)
|
|
|
|
run = sub.add_parser("run", help="Play a Python animation script or a JSON show.")
|
|
run.add_argument("script")
|
|
run.add_argument("--ip", help="Art-Net destination. Omit to print samples.")
|
|
run.add_argument("--seconds", type=float)
|
|
run.add_argument("--hz", type=float, default=40.0)
|
|
run.add_argument("--universe", type=int, default=0)
|
|
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
payload = _dispatch(args)
|
|
except (OSError, json.JSONDecodeError) as exc:
|
|
payload = {"ok": False, "errors": [{"path": "", "message": str(exc)}]}
|
|
json.dump(payload, sys.stdout, indent=2)
|
|
sys.stdout.write("\n")
|
|
return 0 if payload.get("ok", True) else 1
|
|
|
|
|
|
def _dispatch(args: argparse.Namespace) -> dict[str, Any]:
|
|
if args.cmd == "list-fixtures":
|
|
return list_fixtures()
|
|
if args.cmd == "schema":
|
|
return show_schema()
|
|
if args.cmd == "validate":
|
|
return validate_show(_load(args.spec))
|
|
if args.cmd == "write":
|
|
return write_show(_load(args.spec), args.output)
|
|
if args.cmd == "sample":
|
|
return sample_animation(_load(args.block), args.steps)
|
|
if args.cmd == "run":
|
|
return run_script(args.script, seconds=args.seconds, ip=args.ip, universe=args.universe, hz=args.hz)
|
|
raise AssertionError(args.cmd)
|
|
|
|
|
|
def _load(path: str) -> dict[str, Any]:
|
|
source = Path(path)
|
|
if not source.is_absolute():
|
|
source = PROJECT_ROOT / source
|
|
return json.loads(source.read_text(encoding="utf-8"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|