diff --git a/scripts/phase0/build_spec.py b/scripts/phase0/build_spec.py index fa7d1fb68..98b1cad78 100644 --- a/scripts/phase0/build_spec.py +++ b/scripts/phase0/build_spec.py @@ -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: ") + build_phase0_spec(sys.argv[1], sys.argv[2]) + diff --git a/scripts/phase0/function_puller.py b/scripts/phase0/function_puller.py index 2cd0139c5..d0f3f66f1 100644 --- a/scripts/phase0/function_puller.py +++ b/scripts/phase0/function_puller.py @@ -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 diff --git a/test_libs/config_helpers/README.md b/test_libs/config_helpers/README.md new file mode 100644 index 000000000..184482082 --- /dev/null +++ b/test_libs/config_helpers/README.md @@ -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. diff --git a/test_libs/config_helpers/preset_loader/__init__.py b/test_libs/config_helpers/preset_loader/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test_libs/config_helpers/preset_loader/loader.py b/test_libs/config_helpers/preset_loader/loader.py new file mode 100644 index 000000000..043c58805 --- /dev/null +++ b/test_libs/config_helpers/preset_loader/loader.py @@ -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) diff --git a/test_libs/config_helpers/requirements.txt b/test_libs/config_helpers/requirements.txt new file mode 100644 index 000000000..e441a474b --- /dev/null +++ b/test_libs/config_helpers/requirements.txt @@ -0,0 +1 @@ +ruamel.yaml==0.15.87 diff --git a/test_libs/config_helpers/setup.py b/test_libs/config_helpers/setup.py new file mode 100644 index 000000000..90ad94ee4 --- /dev/null +++ b/test_libs/config_helpers/setup.py @@ -0,0 +1,9 @@ +from distutils.core import setup + +setup( + name='config_helpers', + packages=['preset_loader'], + install_requires=[ + "ruamel.yaml==0.15.87" + ] +) diff --git a/test_libs/gen_helpers/setup.py b/test_libs/gen_helpers/setup.py index 88b971bf3..5de27a6db 100644 --- a/test_libs/gen_helpers/setup.py +++ b/test_libs/gen_helpers/setup.py @@ -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",