Skip to content

Commit 7fbeef6

Browse files
committed
Optional Variations and Constants files and setttings
1 parent ad7b965 commit 7fbeef6

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

candore/modules/comparator.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ def __init__(self, settings):
1212
self.record_evs = False
1313
self.variations = Variations(settings)
1414
self.constants = Constants(settings)
15-
self.expected_variations = self.variations.expected_variations
16-
self.skipped_variations = self.variations.skipped_variations
17-
self.expected_constants = self.constants.expected_constants
18-
self.skipped_constants = self.constants.skipped_constants
19-
20-
2115

2216
def remove_verifed_key(self, key):
2317
reversed_bk = self.big_key[::-1]
@@ -35,7 +29,7 @@ def record_variation(self, pre, post, var_details=None):
3529
big_key = [str(itm) for itm in self.big_key]
3630
full_path = "/".join(big_key)
3731
var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
38-
if var_full_path in self.expected_variations or var_full_path in self.skipped_variations:
32+
if var_full_path in self.variations.expected_variations or var_full_path in self.variations.skipped_variations:
3933
if self.record_evs:
4034
variation = {
4135
"pre": pre,
@@ -44,8 +38,8 @@ def record_variation(self, pre, post, var_details=None):
4438
}
4539
self.big_diff.update({full_path: variation})
4640
elif (
47-
var_full_path not in self.expected_variations
48-
and var_full_path not in self.skipped_variations
41+
var_full_path not in self.variations.expected_variations
42+
and var_full_path not in self.variations.skipped_variations
4943
):
5044
variation = {"pre": pre, "post": post, "variation": var_details or ""}
5145
self.big_diff.update({full_path: variation})
@@ -54,7 +48,7 @@ def record_constants(self, pre, post, var_details=None):
5448
big_key = [str(itm) for itm in self.big_key]
5549
full_path = "/".join(big_key)
5650
var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
57-
if var_full_path in self.expected_constants or var_full_path in self.skipped_constants:
51+
if var_full_path in self.constants.expected_constants or var_full_path in self.constants.skipped_constants:
5852
if self.record_evs:
5953
variation = {
6054
"pre": pre,
@@ -63,8 +57,8 @@ def record_constants(self, pre, post, var_details=None):
6357
}
6458
self.big_constant.update({full_path: variation})
6559
elif (
66-
var_full_path not in self.expected_constants
67-
and var_full_path not in self.skipped_constants
60+
var_full_path not in self.constants.expected_constants
61+
and var_full_path not in self.constants.skipped_constants
6862
):
6963
variation = {"pre": pre, "post": post, "constant": var_details or ""}
7064
self.big_constant.update({full_path: variation})

candore/modules/variations.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55
from functools import cached_property
66
from candore.utils import yaml_reader, get_yaml_paths
77

8-
import yaml
9-
108

119
class Variations:
1210
def __init__(self, settings):
1311
self.settings = settings
1412

1513
@cached_property
1614
def variations(self):
17-
yaml_data = yaml_reader(file_path=self.settings.candore.var_file)
15+
yaml_data = yaml_reader(file_path=getattr(self.settings.candore, "var_file", None))
1816
return yaml_data
1917

2018
@cached_property
2119
def expected_variations(self):
22-
return get_yaml_paths(yaml_data=self.variations.get("expected_variations"))
20+
yaml_data = self.variations.get("expected_variations") if self.variations else None
21+
return get_yaml_paths(yaml_data=yaml_data)
22+
2323

2424
@cached_property
2525
def skipped_variations(self):
26-
return get_yaml_paths(yaml_data=self.variations.get("skipped_variations"))
26+
yaml_data = self.variations.get("skipped_variations") if self.variations else None
27+
return get_yaml_paths(yaml_data=yaml_data)
2728

2829

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

3334
@cached_property
3435
def constants(self):
35-
yaml_data = yaml_reader(file_path=self.settings.candore.constant_file)
36+
yaml_data = yaml_reader(file_path=getattr(self.settings.candore, "constant_file", None))
3637
return yaml_data
3738

3839
@cached_property
3940
def expected_constants(self):
40-
return get_yaml_paths(yaml_data=self.constants.get("expected_constants"))
41+
yaml_data = self.constants.get("expected_constants") if self.constants else None
42+
return get_yaml_paths(yaml_data=yaml_data)
4143

4244
@cached_property
4345
def skipped_constants(self):
44-
return get_yaml_paths(yaml_data=self.constants.get("skipped_constants"))
46+
yaml_data = self.constants.get("skipped_constants") if self.constants else None
47+
return get_yaml_paths(yaml_data=yaml_data)

candore/utils.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ def is_list_contains_dict(_list):
1818

1919

2020
def yaml_reader(file_path=None):
21+
if not file_path:
22+
return None
2123
templates_path = Path(file_path)
2224
if not templates_path.exists():
23-
print(f"The file {templates_path} does not exist.")
25+
print(f"Warning! The file {templates_path} does not exist.")
26+
return None
2427
with templates_path.open() as yaml_file:
2528
yaml_data = yaml.safe_load(yaml_file)
2629
return yaml_data
2730

2831

2932
def get_yaml_paths(yaml_data, prefix="", separator="/"):
3033
paths = []
31-
if isinstance(yaml_data, dict):
32-
for key, value in yaml_data.items():
33-
paths.extend(get_yaml_paths(value, f"{prefix}{key}{separator}"))
34-
elif isinstance(yaml_data, list):
35-
for item in yaml_data:
36-
paths.extend(get_yaml_paths(item, prefix, separator))
37-
else:
38-
paths.append(f"{prefix}{yaml_data}")
34+
if yaml_data:
35+
if isinstance(yaml_data, dict):
36+
for key, value in yaml_data.items():
37+
paths.extend(get_yaml_paths(value, f"{prefix}{key}{separator}"))
38+
elif isinstance(yaml_data, list):
39+
for item in yaml_data:
40+
paths.extend(get_yaml_paths(item, prefix, separator))
41+
else:
42+
paths.append(f"{prefix}{yaml_data}")
3943
return paths

0 commit comments

Comments
 (0)