eth2.0-specs/scripts/phase0/function_puller.py

83 lines
3.6 KiB
Python
Raw Normal View History

2019-04-07 07:02:20 +00:00
from typing import List
def get_spec(file_name: str) -> List[str]:
code_lines = []
pulling_from = None
current_name = None
2019-05-27 19:14:59 +00:00
# list of current type definition being parsed, or None otherwise
current_typedef = None
2019-05-27 19:14:59 +00:00
# list of (name, definition lines list) tuples.
type_defs = []
2019-05-27 19:14:59 +00:00
for linenum, line in enumerate(open(file_name).readlines()):
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]
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:
pulling_from = None
2019-05-27 19:14:59 +00:00
if current_typedef is not None:
type_defs.append((current_name, current_typedef))
current_typedef = None
else:
2019-05-27 19:14:59 +00:00
if pulling_from is not None:
# Add some whitespace between functions
2019-05-27 19:14:59 +00:00
if line[:3] == 'def' or line[:5] == 'class':
code_lines.append('')
code_lines.append('')
2019-05-27 19:14:59 +00:00
# Check for SSZ type definitions
if len(line) > 18 and line[:6] == 'class ' and line[-12:] == '(Container):':
name = line[6:-12]
# Check consistency with markdown header
assert name == current_name
current_typedef = []
if current_typedef is not None:
current_typedef.append(line)
code_lines.append(line)
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:
code_lines.append(row[0] + ' = ' + (row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')))
# Build type-def re-initialization
2019-05-09 06:25:08 +00:00
code_lines.append('\n')
code_lines.append('ssz_types = [\n')
2019-05-27 19:14:59 +00:00
for (ssz_type_name, _) in type_defs:
code_lines.append(f' {ssz_type_name},')
2019-05-09 06:25:08 +00:00
code_lines.append(']')
2019-04-25 08:03:02 +00:00
code_lines.append('\n')
2019-05-27 19:14:59 +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:
if len(type_line) > 0:
code_lines.append(' ' + type_line)
code_lines.append('\n')
for (ssz_type_name, _) in type_defs:
code_lines.append(f' global_vars["{ssz_type_name}"] = {ssz_type_name},')
code_lines.append(' global_vars["ssz_types"] = [')
for (ssz_type_name, _) in type_defs:
code_lines.append(f' {ssz_type_name},')
code_lines.append(' ]')
code_lines.append('\n')
code_lines.append('def get_ssz_type_by_name(name: str) -> Container:')
2019-04-25 08:03:02 +00:00
code_lines.append(' return globals()[name]')
code_lines.append('')
return code_lines