Skip to content

Commit

Permalink
Refactor configurator to have validate_config function
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Sep 6, 2024
1 parent 9ff99df commit 769bc90
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions rebench/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,22 @@ def _match(filters, bench):
return False


def validate_config(data):
validator = Core(
source_data=data,
schema_files=[dirname(__file__) + "/rebench-schema.yml"])
validator.validate(raise_exception=True)


def load_config(file_name):
"""
Load the file, verify that it conforms to the schema,
and return the configuration.
"""
config_data = None
try:
with open(file_name, 'r') as conf_file: # pylint: disable=unspecified-encoding
data = yaml.safe_load(conf_file)
validator = Core(
source_data=data,
schema_files=[dirname(__file__) + "/rebench-schema.yml"])
try:
validator.validate(raise_exception=True)
validate_gauge_adapters(data)

# add file name and directory to config to be able to use it when loading
# for instance gauge adapters
data['__file__'] = file_name
data['__dir__'] = dirname(abspath(file_name))
except SchemaError as err:
errors = [escape_braces(val_err) for val_err in validator.validation_errors]
raise UIError(
"Validation of " + file_name + " failed.\n{ind}" +
"\n{ind}".join(errors) + "\n", err)
return data
config_data = yaml.safe_load(conf_file)
except IOError as err:
if err.errno == 2:
assert err.strerror == "No such file or directory"
Expand All @@ -153,6 +144,21 @@ def load_config(file_name):
raise UIError("Parsing of the config file "
+ file_name + " failed.\nError " + str(err) + "\n", err)

try:
validate_config(config_data)
validate_gauge_adapters(config_data)

# add file name and directory to config to be able to use it when loading
# for instance gauge adapters
config_data['__file__'] = file_name
config_data['__dir__'] = dirname(abspath(file_name))
except SchemaError as err:
errors = [escape_braces(val_err) for val_err in validator.validation_errors]
raise UIError(
"Validation of " + file_name + " failed.\n{ind}" +
"\n{ind}".join(errors) + "\n", err)
return config_data


def validate_gauge_adapters(raw_config):
benchmark_suites = raw_config.get('benchmark_suites', {})
Expand Down

0 comments on commit 769bc90

Please sign in to comment.