2019-03-18 16:18:57 +00:00
|
|
|
import sys
|
2019-04-07 07:02:20 +00:00
|
|
|
from typing import List
|
2019-03-18 16:18:57 +00:00
|
|
|
|
|
|
|
|
2019-05-07 11:13:22 +00:00
|
|
|
def get_spec(file_name: str, phase:int = 0) -> List[str]:
|
2019-03-18 16:18:57 +00:00
|
|
|
code_lines = []
|
|
|
|
pulling_from = None
|
|
|
|
current_name = None
|
2019-04-07 13:36:05 +00:00
|
|
|
current_typedef = None
|
2019-05-07 11:13:22 +00:00
|
|
|
is_update_section = False
|
|
|
|
update_section_depth = None
|
2019-04-07 13:36:05 +00:00
|
|
|
type_defs = []
|
2019-05-07 11:13:22 +00:00
|
|
|
for linenum, line in enumerate(open(file_name).readlines()):
|
2019-03-18 16:18:57 +00:00
|
|
|
line = line.rstrip()
|
|
|
|
if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`':
|
2019-03-19 03:39:19 +00:00
|
|
|
current_name = line[line[:-1].rfind('`') + 1: -1]
|
2019-05-07 11:13:22 +00:00
|
|
|
if pulling_from is None and len(line) > 0 and line[0] == '#' and line.endswith('updates'):
|
|
|
|
is_update_section = True
|
|
|
|
update_section_depth = max(i for i in range(10) if line.startswith('#' * i))
|
|
|
|
elif pulling_from is None and len(line) > 0 and line[0] == '#' and is_update_section:
|
|
|
|
section_depth = max(i for i in range(10) if line.startswith('#' * i))
|
|
|
|
if section_depth <= update_section_depth:
|
|
|
|
is_update_section = False
|
|
|
|
update_section_depth = None
|
2019-03-18 16:18:57 +00:00
|
|
|
if line[:9] == '```python':
|
|
|
|
assert pulling_from is None
|
|
|
|
pulling_from = linenum + 1
|
|
|
|
elif line[:3] == '```':
|
|
|
|
if pulling_from is None:
|
|
|
|
pulling_from = linenum
|
|
|
|
else:
|
2019-04-07 13:36:05 +00:00
|
|
|
if current_typedef is not None:
|
2019-03-18 16:18:57 +00:00
|
|
|
assert code_lines[-1] == '}'
|
|
|
|
code_lines[-1] = '})'
|
2019-04-07 13:36:05 +00:00
|
|
|
current_typedef[-1] = '})'
|
|
|
|
type_defs.append((current_name, current_typedef))
|
2019-03-18 16:18:57 +00:00
|
|
|
pulling_from = None
|
2019-04-07 13:36:05 +00:00
|
|
|
current_typedef = None
|
2019-03-18 16:18:57 +00:00
|
|
|
else:
|
|
|
|
if pulling_from == linenum and line == '{':
|
2019-05-07 11:13:22 +00:00
|
|
|
if is_update_section:
|
2019-05-08 22:30:08 +00:00
|
|
|
code_lines.append('%s = SSZTypeExtension("%s", {' % (current_name, current_name))
|
|
|
|
current_typedef = ['global_vars["%s"] = SSZTypeExtension("%s", {' % (current_name, current_name)]
|
2019-05-07 11:13:22 +00:00
|
|
|
else:
|
2019-05-08 22:30:08 +00:00
|
|
|
code_lines.append('%s = SSZType("%s", {' % (current_name, current_name))
|
|
|
|
current_typedef = ['global_vars["%s"] = SSZType("%s", {' % (current_name, current_name)]
|
2019-03-18 16:18:57 +00:00
|
|
|
elif pulling_from is not None:
|
2019-03-27 17:21:07 +00:00
|
|
|
# Add some whitespace between functions
|
|
|
|
if line[:3] == 'def':
|
2019-04-07 13:36:05 +00:00
|
|
|
code_lines.append('')
|
|
|
|
code_lines.append('')
|
2019-03-18 16:18:57 +00:00
|
|
|
code_lines.append(line)
|
2019-04-07 13:36:05 +00:00
|
|
|
# Remember type def lines
|
|
|
|
if current_typedef is not None:
|
|
|
|
current_typedef.append(line)
|
2019-03-18 16:18:57 +00:00
|
|
|
elif pulling_from is None and len(line) > 0 and line[0] == '|':
|
|
|
|
row = line[1:].split('|')
|
|
|
|
if len(row) >= 2:
|
|
|
|
for i in range(2):
|
|
|
|
row[i] = row[i].strip().strip('`')
|
|
|
|
if '`' in row[i]:
|
|
|
|
row[i] = row[i][:row[i].find('`')]
|
|
|
|
eligible = True
|
|
|
|
if row[0][0] not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_':
|
|
|
|
eligible = False
|
|
|
|
for c in row[0]:
|
|
|
|
if c not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789':
|
|
|
|
eligible = False
|
|
|
|
if eligible:
|
2019-04-14 09:38:37 +00:00
|
|
|
code_lines.append(row[0] + ' = ' + (row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')))
|
2019-04-07 13:36:05 +00:00
|
|
|
# Build type-def re-initialization
|
2019-05-09 06:25:08 +00:00
|
|
|
code_lines.append('\n')
|
2019-04-07 13:36:05 +00:00
|
|
|
code_lines.append('def init_SSZ_types():')
|
|
|
|
code_lines.append(' global_vars = globals()')
|
|
|
|
for ssz_type_name, ssz_type in type_defs:
|
|
|
|
code_lines.append('')
|
|
|
|
for type_line in ssz_type:
|
2019-05-09 06:25:08 +00:00
|
|
|
if len(type_line) > 0:
|
|
|
|
code_lines.append(' ' + type_line)
|
2019-04-25 08:03:02 +00:00
|
|
|
code_lines.append('\n')
|
2019-05-09 06:25:08 +00:00
|
|
|
code_lines.append('ssz_types = [\n')
|
|
|
|
for (ssz_type_name, _) in type_defs:
|
|
|
|
code_lines.append(f' {ssz_type_name},\n')
|
|
|
|
code_lines.append(']')
|
2019-04-25 08:03:02 +00:00
|
|
|
code_lines.append('\n')
|
|
|
|
code_lines.append('def get_ssz_type_by_name(name: str) -> SSZType:')
|
|
|
|
code_lines.append(' return globals()[name]')
|
2019-04-16 10:50:13 +00:00
|
|
|
code_lines.append('')
|
2019-03-18 16:18:57 +00:00
|
|
|
return code_lines
|