-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhopper_apply_annotations.py
More file actions
125 lines (95 loc) · 3.78 KB
/
hopper_apply_annotations.py
File metadata and controls
125 lines (95 loc) · 3.78 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# hopper_apply_annotations.py — run inside Hopper's script engine
# Applies labels, comments, tags, colors, and bookmarks from a JSON file to the current document.
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
Document: Any # provided by Hopper's script engine at runtime
VERSION = "1.5.2"
# ---------------------------------------------------------------------------
# Utility helpers (inlined — no external module dependencies)
# ---------------------------------------------------------------------------
def load_json(path: str | Path) -> Any:
return json.loads(Path(path).read_text(encoding="utf-8"))
def parse_address(value: object) -> int:
if isinstance(value, int):
return value
text = str(value).strip().lower()
if text.startswith("0x"):
return int(text, 16)
return int(text, 10)
def parse_color(value: object) -> int:
if isinstance(value, int):
return value
text = str(value).strip().lower()
if text.startswith("#"):
text = text[1:]
if text.startswith("0x"):
text = text[2:]
return int(text, 16)
# ---------------------------------------------------------------------------
# Annotation logic
# ---------------------------------------------------------------------------
def resolve_input_path(document: Any) -> Path:
try:
executable_path = Path(document.getExecutableFilePath())
start_dir = str(executable_path.parent)
except Exception:
start_dir = str(Path.home())
selected = Document.askFile("Select annotations JSON", start_dir, False)
if not selected:
raise RuntimeError("No annotations JSON selected.")
return Path(selected)
def apply_annotations(document: Any, payload: dict[str, Any]) -> dict[str, int]:
summary = {
"labels": 0,
"comments": 0,
"inline_comments": 0,
"tags": 0,
"colors": 0,
"bookmarks": 0,
}
for record in payload.get("labels", []):
document.setNameAtAddress(parse_address(record["address"]), str(record["name"]))
summary["labels"] += 1
for record in payload.get("comments", []):
document.setCommentAtAddress(parse_address(record["address"]), str(record["comment"]))
summary["comments"] += 1
for record in payload.get("inline_comments", []):
document.setInlineCommentAtAddress(
parse_address(record["address"]),
str(record["comment"]),
)
summary["inline_comments"] += 1
for record in payload.get("tags", []):
address = parse_address(record["address"])
tag = document.buildTag(str(record["tag"]))
document.addTagAtAddress(tag, address)
summary["tags"] += 1
for record in payload.get("colors", []):
document.setColorAtAddress(
parse_color(record["color"]),
parse_address(record["address"]),
)
summary["colors"] += 1
for record in payload.get("bookmarks", []):
document.setBookmarkAtAddress(
parse_address(record["address"]),
record.get("name"),
)
summary["bookmarks"] += 1
if any(summary.values()):
document.refreshView()
return summary
# ---------------------------------------------------------------------------
# Script entry — runs immediately inside Hopper
# ---------------------------------------------------------------------------
doc = Document.getCurrentDocument()
if doc is None:
raise RuntimeError("No active Hopper document.")
input_path = resolve_input_path(doc)
payload = load_json(input_path)
summary = apply_annotations(doc, payload)
doc.log(f"[hopper_apply_annotations] Applied from: {input_path}")
for key, value in summary.items():
doc.log(f"[hopper_apply_annotations] {key}: {value}")