-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoteGenerator.py
More file actions
165 lines (132 loc) · 4.76 KB
/
noteGenerator.py
File metadata and controls
165 lines (132 loc) · 4.76 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import os
import re
import sys
# Regex
NOTES_ADDR_REGEX = re.compile(r'0x[0-9A-Fa-f]{5,8}')
USER_ADDR_REGEX = re.compile(r'N\d+:0x([0-9A-Fa-f]{8})')
PLACEHOLDER_REGEX = re.compile(r'\{(\w+)\}')
def hex_to_int(value):
"""Convert hex or decimal string to int."""
if isinstance(value, int):
return value
value = str(value).strip()
if value.lower().startswith("0x"):
return int(value, 16)
return int(value)
def normalize_address(addr):
"""Return 8-digit uppercase hex address."""
return f"{addr:08X}"
def load_text(path):
if not os.path.exists(path):
return ""
with open(path, "r", encoding="utf-8") as f:
return f.read()
def extract_notes_addresses(text):
return {normalize_address(int(m, 16)) for m in NOTES_ADDR_REGEX.findall(text)}
def extract_user_addresses(text):
return {m.upper() for m in USER_ADDR_REGEX.findall(text)}
# ============================
# 🔥 SCHEMA NORMALIZATION FIX
# ============================
def normalize_schema_numeric_keys(obj):
"""
Convert dictionary keys that look numeric (hex or decimal)
into integer keys recursively.
"""
if isinstance(obj, dict):
new = {}
for k, v in obj.items():
new_key = k
if isinstance(k, str):
try:
new_key = hex_to_int(k)
except ValueError:
pass
new[new_key] = normalize_schema_numeric_keys(v)
return new
if isinstance(obj, list):
return [normalize_schema_numeric_keys(v) for v in obj]
return obj
def build_context(outer_idx, inner_idx, schema, placeholders):
"""
Build context dictionary for placeholder substitution.
"""
context = {
"index": outer_idx,
"index_1": outer_idx + 1,
"inner_index": inner_idx,
"inner_index_1": inner_idx + 1
}
for placeholder in placeholders:
if placeholder in context:
continue
source = schema.get(placeholder)
if isinstance(source, dict):
# VALUE-BASED lookup (hex == decimal)
context[placeholder] = source.get(outer_idx, "")
elif isinstance(source, list):
if outer_idx < len(source):
context[placeholder] = source[outer_idx]
else:
context[placeholder] = ""
else:
context[placeholder] = source
return context
def main():
if len(sys.argv) < 2:
print("Usage: python noteGenerator.py <schema_file.json> [--real]")
sys.exit(1)
schema_path = sys.argv[1]
real_run = "--real" in sys.argv
with open(schema_path, "r", encoding="utf-8") as f:
schema = json.load(f)
# 🔥 Normalize schema numeric keys ONCE
schema = normalize_schema_numeric_keys(schema)
notes_text = load_text(schema["files"]["notes"])
user_text = load_text(schema["files"]["user"])
documented = extract_notes_addresses(notes_text) | extract_user_addresses(user_text)
additions = []
for block in schema["blocks"]:
start = hex_to_int(block["start"])
stride = hex_to_int(block["stride"])
number = hex_to_int(block.get("number", 0))
inner_repeat = hex_to_int(block.get("inner_repeat", 1))
inner_stride = hex_to_int(block.get("inner_stride", 0))
total_iterations = number + 1
# Collect placeholders used
placeholders = set()
for line in block["note"]:
placeholders.update(PLACEHOLDER_REGEX.findall(line))
for outer_idx in range(total_iterations):
entry_base = start + (outer_idx * stride)
for inner_idx in range(inner_repeat):
addr = entry_base + (inner_idx * inner_stride)
norm = normalize_address(addr)
if norm in documented:
continue
context = build_context(
outer_idx=outer_idx,
inner_idx=inner_idx,
schema=schema,
placeholders=placeholders
)
body = "\\r\\n".join(
line.format(**context) for line in block["note"]
)
record = f'N0:0x{norm}:"{body}\\r\\n"'
additions.append(record)
documented.add(norm)
if real_run:
if additions:
with open(schema["files"]["user"], "a", encoding="utf-8") as f:
f.write("\n" + "\n".join(additions))
print(f"[OK] Added {len(additions)} entries to {schema['files']['user']}")
else:
print("[OK] No new entries needed")
else:
print("[DRY RUN] Generated entries:")
for line in additions:
print(line)
if __name__ == "__main__":
main()