intro configuration support pkg

This commit is contained in:
protolambda 2019-04-07 17:02:20 +10:00
parent 9f32995693
commit 9eb640dd3b
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
8 changed files with 65 additions and 7 deletions

View File

@ -2,13 +2,14 @@ import sys
import function_puller
def build_spec(sourcefile, outfile):
def build_phase0_spec(sourcefile, outfile):
code_lines = []
code_lines.append("""
from typing import (
Any,
Callable,
Dict,
List,
NewType,
Tuple,
@ -41,7 +42,7 @@ Any = None
Store = None
""")
code_lines += function_puller.get_lines(sourcefile)
code_lines += function_puller.get_spec(sourcefile)
code_lines.append("""
# Monkey patch validator get committee code
@ -78,7 +79,16 @@ def hash(x):
ret = _hash(x)
hash_cache[x] = ret
return ret
""")
# Access to overwrite spec constants based on configuration
def apply_constants_preset(preset: Dict[str, Any]):
global_vars = globals()
for k, v in preset:
global_vars[k] = v
# Deal with derived constants
GENESIS_EPOCH = slot_to_epoch(GENESIS_SLOT)
""")
with open(outfile, 'w') as out:
out.write("\n".join(code_lines))
@ -86,5 +96,6 @@ def hash(x):
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Error: spec source and outfile must defined")
build_spec(sys.argv[1], sys.argv[2])
print("Usage: <source phase0> <output phase0 pyspec>")
build_phase0_spec(sys.argv[1], sys.argv[2])

View File

@ -1,7 +1,8 @@
import sys
from typing import List
def get_lines(file_name):
def get_spec(file_name) -> List[str]:
code_lines = []
pulling_from = None
current_name = None

View File

@ -0,0 +1,19 @@
# ETH 2.0 config helpers
`preset_loader`: A util to load constants-presets with.
See [Constants-presets documentation](../../configs/constants_presets/README.md).
Usage:
```python
configs_path = 'configs/'
...
import preset_loader
from eth2spec.phase0 import spec
my_presets = preset_loader.load_presets(configs_path, 'main_net')
spec.apply_constants_preset(my_presets)
```
WARNING: this overwrites globals, make sure to prevent accidental collisions with other usage of the same imported specs package.

View File

@ -0,0 +1,18 @@
from typing import Dict, Any
from ruamel.yaml import (
YAML,
)
from pathlib import Path
from os.path import join
def load_presets(configs_dir, presets_name) -> Dict[str, Any]:
"""
Loads the given preset
:param presets_name: The name of the generator. (lowercase snake_case)
:return: Dictionary, mapping of constant-name -> constant-value
"""
path = Path(join(configs_dir, 'constant_presets', presets_name+'.yaml'))
yaml = YAML(typ='safe')
return yaml.load(path)

View File

@ -0,0 +1 @@
ruamel.yaml==0.15.87

View File

@ -0,0 +1,9 @@
from distutils.core import setup
setup(
name='config_helpers',
packages=['preset_loader'],
install_requires=[
"ruamel.yaml==0.15.87"
]
)

View File

@ -2,7 +2,6 @@ from distutils.core import setup
setup(
name='gen_helpers',
version='1.0',
packages=['gen_base'],
install_requires=[
"ruamel.yaml==0.15.87",