Skip to content

Commit c3924e4

Browse files
authored
Adding procedure extraction to index
1 parent 3321fdd commit c3924e4

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

tools/index/index.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,50 @@ def parse_proc_table(proc_table):
2727
procs[elements[1].split(".")[-1].strip()] = elements[2].strip()
2828
return procs
2929

30+
def parse_test_table(test_table, ref_links):
31+
tests = {}
32+
for line in test_table[3:]:
33+
if len(line) != 0:
34+
elements = line.split("|")
35+
letter = elements[1].split(".")[-1].strip()
36+
link_field = elements[2].strip()
37+
38+
if not link_field or link_field.lower() == "none":
39+
tests[letter] = None
40+
else:
41+
# Pattern: [text][ref-key]
42+
match = re.match(r'\[([^\]]+)\]\[([^\]]+)\]', link_field)
43+
if match:
44+
text = match.group(1)
45+
ref_key = match.group(2).lower()
46+
else:
47+
# Pattern: [text]
48+
match = re.match(r'\[([^\]]+)\]', link_field)
49+
if match:
50+
text = match.group(1)
51+
ref_key = text.lower()
52+
else:
53+
text = link_field
54+
ref_key = None
55+
56+
url = ref_links.get(ref_key) if ref_key else None
57+
tests[letter] = {"text": text, "url": url}
58+
return tests
59+
3060
def parse_trr_meta(TRR_path):
3161
TRR_dict = {} #dict to hold all the values parsed from the TRR meta
32-
33-
#file_path = os.path.join(DID_path, "README.md")
34-
file = open(TRR_path, "r")
62+
63+
with open(TRR_path, "r") as f:
64+
content = f.read()
65+
66+
# Build reference link dictionary from the whole file
67+
# Markdown ref format: [key]: https://url
68+
ref_links = {}
69+
for m in re.finditer(r'^\[([^\]]+)\]:\s+(\S+)', content, re.MULTILINE):
70+
ref_links[m.group(1).lower()] = m.group(2)
71+
72+
import io
73+
file = io.StringIO(content)
3574

3675
#Parse the TRR README.md line by line
3776
for line in file:
@@ -83,7 +122,20 @@ def parse_trr_meta(TRR_path):
83122

84123
proc_dict = parse_proc_table(proc_table) #load all the data from the procedures table into the dict
85124
TRR_dict['procedures'] = proc_dict
86-
125+
126+
# The while loop above may have stopped at a ### subsection header.
127+
# Advance until we find the next level-2 (## ) section header.
128+
while not proc_line.startswith("## "):
129+
proc_line = next(file)
130+
131+
if proc_line.strip() == "## Available Emulation Tests":
132+
test_table = []
133+
test_line = next(file)
134+
while not test_line.startswith("##"):
135+
test_table.append(test_line.strip())
136+
test_line = next(file)
137+
TRR_dict['tests'] = parse_test_table(test_table, ref_links)
138+
87139
return(TRR_dict)
88140

89141
def update_index(trr_dict, index):
@@ -101,6 +153,8 @@ def update_index(trr_dict, index):
101153
trr['platforms'] = trr_dict['platforms']
102154
trr['procedures'] = trr_dict['procedures']
103155
trr['tactics'] = trr_dict['tactics']
156+
if 'tests' in trr_dict:
157+
trr['tests'] = trr_dict['tests']
104158

105159
if not found_id: #ID isn't in index already, add it
106160
#add a publication date
@@ -229,4 +283,4 @@ def assign_new_id(orig_file, nextID):
229283
with open("index.json", "w") as f:
230284
f.write(json.dumps(index, indent=2))
231285

232-
sys.exit(0) #exit successfully
286+
sys.exit(0) #exit successfully

0 commit comments

Comments
 (0)