| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import argparse
- import sys
- from pathlib import Path
- import yaml
- def validate_state(state):
- errors = []
- if not isinstance(state, dict):
- return ["document root must be a mapping"]
- # Establish the declared source map before validating looper references.
- sources = state.get("sources")
- if not isinstance(sources, list):
- return ["sources must be a list"]
- source_by_id = {}
- physical_inputs = set()
- capture_ports = set()
- for index, source in enumerate(sources):
- location = f"sources[{index}]"
- if not isinstance(source, dict):
- errors.append(f"{location} must be a mapping")
- continue
- source_id = source.get("id")
- if not isinstance(source_id, str) or not source_id:
- errors.append(f"{location}.id must be a non-empty string")
- elif source_id in source_by_id:
- errors.append(f"duplicate source id: {source_id}")
- else:
- source_by_id[source_id] = source
- physical_input = source.get("physical_input")
- if not isinstance(physical_input, int) or isinstance(physical_input, bool):
- errors.append(f"{location}.physical_input must be an integer")
- elif physical_input in physical_inputs:
- errors.append(f"duplicate physical input: {physical_input}")
- else:
- physical_inputs.add(physical_input)
- capture_port = source.get("capture_port")
- if not isinstance(capture_port, str) or not capture_port:
- errors.append(f"{location}.capture_port must be a non-empty string")
- elif capture_port in capture_ports:
- errors.append(f"duplicate capture port: {capture_port}")
- else:
- capture_ports.add(capture_port)
- # Keep the first four-channel milestone tied to its four named instruments.
- expected_source_ids = {"guitar", "bass", "voice", "synth"}
- if set(source_by_id) != expected_source_ids:
- errors.append("source ids must be guitar, bass, voice, and synth exactly once")
- if len(sources) != 4:
- errors.append(f"exactly four sources are required, got {len(sources)}")
- if physical_inputs != {1, 2, 3, 4}:
- errors.append("physical inputs must cover 1 through 4 exactly once")
- # The required looper is one instance containing one four-channel loop.
- system = state.get("system")
- components = system.get("components") if isinstance(system, dict) else None
- if not isinstance(components, list):
- errors.append("system.components must be a list")
- components = []
- loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
- if len(loopers) != 1:
- errors.append(f"exactly one looper component is required, got {len(loopers)}")
- elif loopers[0].get("instance_count") != 1:
- errors.append("looper.instance_count must be 1")
- elif loopers[0].get("loop_count") != 1:
- errors.append("looper.loop_count must be 1")
- elif loopers[0].get("channels_per_loop") != 4:
- errors.append("looper.channels_per_loop must be 4")
- routing = state.get("routing")
- if not isinstance(routing, dict):
- errors.append("routing must be a mapping")
- return errors
- looper_inputs = routing.get("looper_inputs")
- if not isinstance(looper_inputs, list):
- errors.append("routing.looper_inputs must be a list")
- looper_inputs = []
- # Check that each source is assigned exactly once across all loop channels.
- input_channels = set()
- input_sources = set()
- for index, loop_input in enumerate(looper_inputs):
- location = f"routing.looper_inputs[{index}]"
- if not isinstance(loop_input, dict):
- errors.append(f"{location} must be a mapping")
- continue
- source_id = loop_input.get("source")
- channel = loop_input.get("channel")
- if source_id not in source_by_id:
- errors.append(f"{location}.source references unknown source: {source_id}")
- if source_id in input_sources:
- errors.append(f"source is assigned to multiple looper inputs: {source_id}")
- input_sources.add(source_id)
- if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
- errors.append(f"{location}.channel must be an integer from 1 through 4")
- elif channel in input_channels:
- errors.append(f"duplicate looper input channel: {channel}")
- else:
- input_channels.add(channel)
- if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
- errors.append("looper inputs must cover channels 1 through 4 exactly once")
- if input_sources != set(source_by_id):
- errors.append("each declared source must map to exactly one looper input")
- mix = routing.get("mix")
- if not isinstance(mix, dict):
- errors.append("routing.mix must be a mapping")
- else:
- output_port = mix.get("output_port")
- if not isinstance(output_port, str) or not output_port:
- errors.append("routing.mix.output_port must be a non-empty string")
- mix_id = mix.get("id")
- if not isinstance(mix_id, str) or not mix_id:
- errors.append("routing.mix.id must be a non-empty string")
- includes = mix.get("includes")
- if not isinstance(includes, list) or "looper_returns" not in includes:
- errors.append("routing.mix.includes must include looper_returns")
- # Loop returns join the output mix but must never feed the loop inputs.
- returns = routing.get("looper_returns")
- if not isinstance(returns, dict):
- errors.append("routing.looper_returns must be a mapping")
- elif returns.get("feed_looper_inputs") is not False:
- errors.append("routing.looper_returns.feed_looper_inputs must be false")
- connections = state.get("connections", [])
- if not isinstance(connections, list):
- errors.append("connections must be a list")
- connections = []
- # An explicitly-forbidden connection documents policy; any other status is actionable.
- for index, connection in enumerate(connections):
- if not isinstance(connection, dict):
- errors.append(f"connections[{index}] must be a mapping")
- continue
- if (
- connection.get("from") == "looper_returns"
- and connection.get("to") in {
- "looper_inputs",
- "looper_input_1",
- "looper_input_2",
- "looper_input_3",
- "looper_input_4",
- }
- and connection.get("status") != "explicitly-forbidden"
- ):
- errors.append(f"connections[{index}] routes looper returns into looper inputs")
- return errors
- def main(argv=None):
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "state_file",
- nargs="?",
- type=Path,
- default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
- )
- args = parser.parse_args(argv)
- try:
- with args.state_file.open(encoding="utf-8") as state_stream:
- state = yaml.safe_load(state_stream)
- except (OSError, yaml.YAMLError) as error:
- print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
- return 2
- errors = validate_state(state)
- if errors:
- for error in errors:
- print(f"ERROR: {error}", file=sys.stderr)
- return 1
- print(f"Valid desired state: {args.state_file}")
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|