Skip to content

Commit

Permalink
Merge pull request #17 from jyejare/optional_var_files
Browse files Browse the repository at this point in the history
Optional Variations and Constants files and setttings
  • Loading branch information
jyejare authored Apr 26, 2024
2 parents ad7b965 + 7fbeef6 commit e8a2f56
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
18 changes: 6 additions & 12 deletions candore/modules/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ def __init__(self, settings):
self.record_evs = False
self.variations = Variations(settings)
self.constants = Constants(settings)
self.expected_variations = self.variations.expected_variations
self.skipped_variations = self.variations.skipped_variations
self.expected_constants = self.constants.expected_constants
self.skipped_constants = self.constants.skipped_constants



def remove_verifed_key(self, key):
reversed_bk = self.big_key[::-1]
Expand All @@ -35,7 +29,7 @@ def record_variation(self, pre, post, var_details=None):
big_key = [str(itm) for itm in self.big_key]
full_path = "/".join(big_key)
var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
if var_full_path in self.expected_variations or var_full_path in self.skipped_variations:
if var_full_path in self.variations.expected_variations or var_full_path in self.variations.skipped_variations:
if self.record_evs:
variation = {
"pre": pre,
Expand All @@ -44,8 +38,8 @@ def record_variation(self, pre, post, var_details=None):
}
self.big_diff.update({full_path: variation})
elif (
var_full_path not in self.expected_variations
and var_full_path not in self.skipped_variations
var_full_path not in self.variations.expected_variations
and var_full_path not in self.variations.skipped_variations
):
variation = {"pre": pre, "post": post, "variation": var_details or ""}
self.big_diff.update({full_path: variation})
Expand All @@ -54,7 +48,7 @@ def record_constants(self, pre, post, var_details=None):
big_key = [str(itm) for itm in self.big_key]
full_path = "/".join(big_key)
var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
if var_full_path in self.expected_constants or var_full_path in self.skipped_constants:
if var_full_path in self.constants.expected_constants or var_full_path in self.constants.skipped_constants:
if self.record_evs:
variation = {
"pre": pre,
Expand All @@ -63,8 +57,8 @@ def record_constants(self, pre, post, var_details=None):
}
self.big_constant.update({full_path: variation})
elif (
var_full_path not in self.expected_constants
and var_full_path not in self.skipped_constants
var_full_path not in self.constants.expected_constants
and var_full_path not in self.constants.skipped_constants
):
variation = {"pre": pre, "post": post, "constant": var_details or ""}
self.big_constant.update({full_path: variation})
Expand Down
19 changes: 11 additions & 8 deletions candore/modules/variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
from functools import cached_property
from candore.utils import yaml_reader, get_yaml_paths

import yaml


class Variations:
def __init__(self, settings):
self.settings = settings

@cached_property
def variations(self):
yaml_data = yaml_reader(file_path=self.settings.candore.var_file)
yaml_data = yaml_reader(file_path=getattr(self.settings.candore, "var_file", None))
return yaml_data

@cached_property
def expected_variations(self):
return get_yaml_paths(yaml_data=self.variations.get("expected_variations"))
yaml_data = self.variations.get("expected_variations") if self.variations else None
return get_yaml_paths(yaml_data=yaml_data)


@cached_property
def skipped_variations(self):
return get_yaml_paths(yaml_data=self.variations.get("skipped_variations"))
yaml_data = self.variations.get("skipped_variations") if self.variations else None
return get_yaml_paths(yaml_data=yaml_data)


class Constants:
Expand All @@ -32,13 +33,15 @@ def __init__(self, settings):

@cached_property
def constants(self):
yaml_data = yaml_reader(file_path=self.settings.candore.constant_file)
yaml_data = yaml_reader(file_path=getattr(self.settings.candore, "constant_file", None))
return yaml_data

@cached_property
def expected_constants(self):
return get_yaml_paths(yaml_data=self.constants.get("expected_constants"))
yaml_data = self.constants.get("expected_constants") if self.constants else None
return get_yaml_paths(yaml_data=yaml_data)

@cached_property
def skipped_constants(self):
return get_yaml_paths(yaml_data=self.constants.get("skipped_constants"))
yaml_data = self.constants.get("skipped_constants") if self.constants else None
return get_yaml_paths(yaml_data=yaml_data)
22 changes: 13 additions & 9 deletions candore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ def is_list_contains_dict(_list):


def yaml_reader(file_path=None):
if not file_path:
return None
templates_path = Path(file_path)
if not templates_path.exists():
print(f"The file {templates_path} does not exist.")
print(f"Warning! The file {templates_path} does not exist.")
return None
with templates_path.open() as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
return yaml_data


def get_yaml_paths(yaml_data, prefix="", separator="/"):
paths = []
if isinstance(yaml_data, dict):
for key, value in yaml_data.items():
paths.extend(get_yaml_paths(value, f"{prefix}{key}{separator}"))
elif isinstance(yaml_data, list):
for item in yaml_data:
paths.extend(get_yaml_paths(item, prefix, separator))
else:
paths.append(f"{prefix}{yaml_data}")
if yaml_data:
if isinstance(yaml_data, dict):
for key, value in yaml_data.items():
paths.extend(get_yaml_paths(value, f"{prefix}{key}{separator}"))
elif isinstance(yaml_data, list):
for item in yaml_data:
paths.extend(get_yaml_paths(item, prefix, separator))
else:
paths.append(f"{prefix}{yaml_data}")
return paths

0 comments on commit e8a2f56

Please sign in to comment.