validate_desired_state.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import argparse
  2. import sys
  3. from pathlib import Path
  4. import yaml
  5. def validate_state(state):
  6. errors = []
  7. if not isinstance(state, dict):
  8. return ["document root must be a mapping"]
  9. # Establish the declared source map before validating looper references.
  10. sources = state.get("sources")
  11. if not isinstance(sources, list):
  12. return ["sources must be a list"]
  13. source_by_id = {}
  14. physical_inputs = set()
  15. capture_ports = set()
  16. for index, source in enumerate(sources):
  17. location = f"sources[{index}]"
  18. if not isinstance(source, dict):
  19. errors.append(f"{location} must be a mapping")
  20. continue
  21. source_id = source.get("id")
  22. if not isinstance(source_id, str) or not source_id:
  23. errors.append(f"{location}.id must be a non-empty string")
  24. elif source_id in source_by_id:
  25. errors.append(f"duplicate source id: {source_id}")
  26. else:
  27. source_by_id[source_id] = source
  28. physical_input = source.get("physical_input")
  29. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  30. errors.append(f"{location}.physical_input must be an integer")
  31. elif physical_input in physical_inputs:
  32. errors.append(f"duplicate physical input: {physical_input}")
  33. else:
  34. physical_inputs.add(physical_input)
  35. capture_port = source.get("capture_port")
  36. if not isinstance(capture_port, str) or not capture_port:
  37. errors.append(f"{location}.capture_port must be a non-empty string")
  38. elif capture_port in capture_ports:
  39. errors.append(f"duplicate capture port: {capture_port}")
  40. else:
  41. capture_ports.add(capture_port)
  42. # Keep the first four-channel milestone tied to its four named instruments.
  43. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  44. if set(source_by_id) != expected_source_ids:
  45. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  46. if len(sources) != 4:
  47. errors.append(f"exactly four sources are required, got {len(sources)}")
  48. if physical_inputs != {1, 2, 3, 4}:
  49. errors.append("physical inputs must cover 1 through 4 exactly once")
  50. # The required looper is one instance containing one four-channel loop.
  51. system = state.get("system")
  52. components = system.get("components") if isinstance(system, dict) else None
  53. if not isinstance(components, list):
  54. errors.append("system.components must be a list")
  55. components = []
  56. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  57. if len(loopers) != 1:
  58. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  59. elif loopers[0].get("instance_count") != 1:
  60. errors.append("looper.instance_count must be 1")
  61. elif loopers[0].get("loop_count") != 1:
  62. errors.append("looper.loop_count must be 1")
  63. elif loopers[0].get("channels_per_loop") != 4:
  64. errors.append("looper.channels_per_loop must be 4")
  65. routing = state.get("routing")
  66. if not isinstance(routing, dict):
  67. errors.append("routing must be a mapping")
  68. return errors
  69. looper_inputs = routing.get("looper_inputs")
  70. if not isinstance(looper_inputs, list):
  71. errors.append("routing.looper_inputs must be a list")
  72. looper_inputs = []
  73. # Check that each source is assigned exactly once across all loop channels.
  74. input_channels = set()
  75. input_sources = set()
  76. for index, loop_input in enumerate(looper_inputs):
  77. location = f"routing.looper_inputs[{index}]"
  78. if not isinstance(loop_input, dict):
  79. errors.append(f"{location} must be a mapping")
  80. continue
  81. source_id = loop_input.get("source")
  82. channel = loop_input.get("channel")
  83. if source_id not in source_by_id:
  84. errors.append(f"{location}.source references unknown source: {source_id}")
  85. if source_id in input_sources:
  86. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  87. input_sources.add(source_id)
  88. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  89. errors.append(f"{location}.channel must be an integer from 1 through 4")
  90. elif channel in input_channels:
  91. errors.append(f"duplicate looper input channel: {channel}")
  92. else:
  93. input_channels.add(channel)
  94. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  95. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  96. if input_sources != set(source_by_id):
  97. errors.append("each declared source must map to exactly one looper input")
  98. mix = routing.get("mix")
  99. if not isinstance(mix, dict):
  100. errors.append("routing.mix must be a mapping")
  101. else:
  102. output_port = mix.get("output_port")
  103. if not isinstance(output_port, str) or not output_port:
  104. errors.append("routing.mix.output_port must be a non-empty string")
  105. mix_id = mix.get("id")
  106. if not isinstance(mix_id, str) or not mix_id:
  107. errors.append("routing.mix.id must be a non-empty string")
  108. includes = mix.get("includes")
  109. if not isinstance(includes, list) or "looper_returns" not in includes:
  110. errors.append("routing.mix.includes must include looper_returns")
  111. # Loop returns join the output mix but must never feed the loop inputs.
  112. returns = routing.get("looper_returns")
  113. if not isinstance(returns, dict):
  114. errors.append("routing.looper_returns must be a mapping")
  115. elif returns.get("feed_looper_inputs") is not False:
  116. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  117. connections = state.get("connections", [])
  118. if not isinstance(connections, list):
  119. errors.append("connections must be a list")
  120. connections = []
  121. # An explicitly-forbidden connection documents policy; any other status is actionable.
  122. for index, connection in enumerate(connections):
  123. if not isinstance(connection, dict):
  124. errors.append(f"connections[{index}] must be a mapping")
  125. continue
  126. if (
  127. connection.get("from") == "looper_returns"
  128. and connection.get("to") in {
  129. "looper_inputs",
  130. "looper_input_1",
  131. "looper_input_2",
  132. "looper_input_3",
  133. "looper_input_4",
  134. }
  135. and connection.get("status") != "explicitly-forbidden"
  136. ):
  137. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  138. return errors
  139. def main(argv=None):
  140. parser = argparse.ArgumentParser()
  141. parser.add_argument(
  142. "state_file",
  143. nargs="?",
  144. type=Path,
  145. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  146. )
  147. args = parser.parse_args(argv)
  148. try:
  149. with args.state_file.open(encoding="utf-8") as state_stream:
  150. state = yaml.safe_load(state_stream)
  151. except (OSError, yaml.YAMLError) as error:
  152. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  153. return 2
  154. errors = validate_state(state)
  155. if errors:
  156. for error in errors:
  157. print(f"ERROR: {error}", file=sys.stderr)
  158. return 1
  159. print(f"Valid desired state: {args.state_file}")
  160. return 0
  161. if __name__ == "__main__":
  162. raise SystemExit(main())