add reading files

This commit is contained in:
Nayan Sawyer
2026-04-02 19:25:59 -04:00
parent 84d1b56333
commit 0f57a323d5

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
import argparse import argparse
import json import json
import re
import sys import sys
import time import time
from pathlib import Path from pathlib import Path
@@ -57,6 +58,11 @@ def resolve_coord_map(payload: dict[str, Any], name: str) -> dict[str, Any]:
raise KeyError(f"Unknown slot {name!r} (not in anvil or recipe).") raise KeyError(f"Unknown slot {name!r} (not in anvil or recipe).")
def parse_slots_file(content: str) -> list[str]:
"""Split slot names on commas, spaces, tabs, and newlines; empty tokens are dropped."""
return [t for t in re.split(r"[\s,]+", content.strip()) if t]
def parse_slot_token(token: str) -> tuple[str, bool]: def parse_slot_token(token: str) -> tuple[str, bool]:
"""Underscore prefix means hold Shift for that click (_i2 -> i2 with shift).""" """Underscore prefix means hold Shift for that click (_i2 -> i2 with shift)."""
if token.startswith("_"): if token.startswith("_"):
@@ -159,11 +165,25 @@ def main() -> None:
default=None, default=None,
help=f"Path to coords.json (default: {DEFAULT_COORDS_PATH}).", help=f"Path to coords.json (default: {DEFAULT_COORDS_PATH}).",
) )
parser.add_argument(
"-f",
"--file",
type=Path,
default=None,
metavar="PATH",
help=(
"Read slot names from this file (commas, spaces, and newlines separate tokens). "
"When set, positional SLOT arguments are ignored."
),
)
parser.add_argument( parser.add_argument(
"slots", "slots",
nargs="*", nargs="*",
metavar="SLOT", metavar="SLOT",
help="Slot names from coords.json, e.g. M D P G i9 h6. Use _NAME for shift-click.", help=(
"Slot names from coords.json, e.g. M D P G i9 h6. Use _NAME for shift-click. "
"Ignored when --file is set."
),
) )
args = parser.parse_args() args = parser.parse_args()
path = args.coords_file.expanduser().resolve() if args.coords_file else DEFAULT_COORDS_PATH path = args.coords_file.expanduser().resolve() if args.coords_file else DEFAULT_COORDS_PATH
@@ -172,7 +192,17 @@ def main() -> None:
print(f"Missing coords file: {path}", file=sys.stderr) print(f"Missing coords file: {path}", file=sys.stderr)
sys.exit(1) sys.exit(1)
slots = args.slots if args.slots else DEFAULT_TEST_SLOTS if args.file is not None:
slots_path = args.file.expanduser().resolve()
if not slots_path.is_file():
print(f"Missing slots file: {slots_path}", file=sys.stderr)
sys.exit(1)
slots = parse_slots_file(slots_path.read_text(encoding="utf-8"))
if not slots:
print("Slots file is empty or contains no slot names.", file=sys.stderr)
sys.exit(1)
else:
slots = args.slots if args.slots else DEFAULT_TEST_SLOTS
time.sleep(args.start_delay / 1000) time.sleep(args.start_delay / 1000)