-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec_converter.py
57 lines (41 loc) · 1.38 KB
/
spec_converter.py
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
import re
IMPORTS = """
from typing import Dict, Tuple, Set, Sequence, List
from eth2spec.utils.ssz.ssz_typing import Bytes32, uint64, uint256, uint8
from dataclasses import dataclass
from utils import bytes_to_uint64, uint_to_bytes, hash, ENDIANNESS
"""
def extract_code_blocks(text_blob):
pattern = r"^```(?:\w+)?\s*\n(.*?)(?=^```)```"
code_blocks = re.findall(pattern, text_blob, re.DOTALL | re.MULTILINE)
output = ""
for block in code_blocks:
output += block
output += "\n"
return output
def extract_table_values(text_blob):
rows = re.findall(r"\|([^|]+)\|([^|]+)\|", text_blob)
cleaned_rows = [[cell.strip() for cell in row] for row in rows]
output = ""
for row in cleaned_rows:
if "Name" in row or "Value" in row:
continue
if "---" in row[0] or "---" in row[1]:
continue
output += row[0] + " = " + row[1]
output += "\n"
return output
def process_file(filepath):
output = ""
output += IMPORTS
output += "\n\n\n"
with open(filepath, "r") as f:
text_blob = f.read()
output += extract_table_values(text_blob)
output += "\n\n\n"
output += extract_code_blocks(text_blob)
return output
if __name__ == "__main__":
output = process_file("./rated_list.md")
with open("./simulator/node.py", "w") as f:
f.write(output)