Show available machine configs and let the user pick one.
()
| 36 | |
| 37 | |
| 38 | def pick_machine() -> str: |
| 39 | """Show available machine configs and let the user pick one.""" |
| 40 | scratch_dir = MZ_ROOT / "misc" / "scratch" |
| 41 | configs: list[tuple[str, str, str]] = [] |
| 42 | for f in sorted(scratch_dir.glob("*.json"), key=lambda f: _natsort_key(f.stem)): |
| 43 | with open(f) as fh: |
| 44 | descs = [MachineDesc.model_validate(obj) for obj in multi_json(fh.read())] |
| 45 | for d in descs: |
| 46 | configs.append((f.stem, d.instance_type, d.name)) |
| 47 | |
| 48 | render_table( |
| 49 | ["#", "CONFIG", "INSTANCE TYPE", "NAME"], |
| 50 | [ |
| 51 | [str(i), stem, instance_type, name] |
| 52 | for i, (stem, instance_type, name) in enumerate(configs, 1) |
| 53 | ], |
| 54 | ) |
| 55 | |
| 56 | while True: |
| 57 | choice = input("Select a machine config [#]: ").strip() |
| 58 | try: |
| 59 | idx = int(choice) |
| 60 | if 1 <= idx <= len(configs): |
| 61 | return configs[idx - 1][0] |
| 62 | except ValueError: |
| 63 | # Allow typing the config name directly |
| 64 | for stem, _, _ in configs: |
| 65 | if choice == stem: |
| 66 | return stem |
| 67 | print( |
| 68 | f"Invalid choice: {choice!r}. Enter a number 1-{len(configs)} or a config name." |
| 69 | ) |
| 70 | |
| 71 | |
| 72 | def multi_json(s: str) -> list[dict[Any, Any]]: |
no test coverage detected