-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_sequence_loader.py
More file actions
42 lines (34 loc) · 1.42 KB
/
input_sequence_loader.py
File metadata and controls
42 lines (34 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Utilities to load saved input sequences (mouse/keyboard events).
"""
import json
def load_sequence(path="sequence.json"):
"""Load a recorded sequence file and return a normalized dict.
Returns dict with `initial_pos`, `initial_window_title`, and `events`.
Accepts newer dict format or older plain-list format for backward compat.
"""
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict) and "events" in data:
return {
"initial_pos": data.get("initial_pos"),
"initial_window_title": data.get("initial_window_title"),
"events": data["events"],
}
# older format: just a list of events
return {"initial_pos": None, "initial_window_title": None, "events": data}
def load_sequence_text(json_text):
"""Parse JSON text into the same normalized dict returned by `load_sequence`."""
data = json.loads(json_text)
if isinstance(data, dict) and "events" in data:
return {
"initial_pos": data.get("initial_pos"),
"initial_window_title": data.get("initial_window_title"),
"events": data["events"],
}
return {"initial_pos": None, "initial_window_title": None, "events": data}
if __name__ == "__main__":
# Minimal self-test: do not print by default when imported.
seq = load_sequence()
ev = seq.get("events") or []
print(f"Loaded {len(ev)} events")