Skip to content

Commit

Permalink
Merge pull request #5 from COVESA/throw_errors
Browse files Browse the repository at this point in the history
default behavior was to silently log errors.
  • Loading branch information
MohitYadav-codes authored Jun 17, 2024
2 parents 55338b6 + 55a2849 commit 1c44e46
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
24 changes: 18 additions & 6 deletions akm_tools/akm_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def create_and_combine_json_from_a_folder(dir_path):
return list_to_return


def parse_and_validate_data(model_folder, schema, extended_schema_dir, overlays):
def parse_and_validate_data(model_folder, schema, extended_schema_dir, extended_data_dir,raise_errors):
model_data_list = create_and_combine_json_from_a_folder(model_folder)
overlay_data_list = create_and_combine_json_from_a_folder(overlays)
all_data = model_data_list + overlay_data_list
extended_data_list = create_and_combine_json_from_a_folder(extended_data_dir)
all_data = model_data_list + extended_data_list
validator_object = AKMDataValidator(schema=schema)
validated_model_data = validator_object.validate_data_instances(all_data, extended_schema_dir=extended_schema_dir)
validated_model_data = validator_object.validate_contexts(all_data=validated_model_data) ## passing valid instances
validator_object.log_errors()

validator_object.log_errors(raise_errors)
return validated_model_data


Expand Down Expand Up @@ -94,6 +94,14 @@ def main():
choices=export_functions.keys(),
help="Specifies the export format",
)

## export options
parser.add_argument(
"--silent_mode",
action="store_true",
help="log but dont throw errors",
default=False
)
parser.add_argument("-f", "--export_file_path", type=str, help="Path for export file")
args = parser.parse_args()

Expand All @@ -103,7 +111,11 @@ def main():
with open(args.schema, "r") as f:
schema = json.load(f)
validated_model_data = parse_and_validate_data(
args.model_data_folder, schema, args.extended_schema_dir, args.extended_data_dir
model_folder=args.model_data_folder,
schema=schema,
extended_schema_dir=args.extended_schema_dir,
extended_data_dir=args.extended_data_dir,
raise_errors=not(args.silent_mode)
)

if args.export_format:
Expand Down
2 changes: 1 addition & 1 deletion akm_tools/validation/custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, instances: List[Dict]):
class BaseInstanceOverwiteException(Exception):
def __init__(self, base_instance, extended_instance):
err_msg = (
f"The extended instace :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
)
super().__init__(err_msg)
self.message = err_msg
Expand Down
5 changes: 4 additions & 1 deletion akm_tools/validation/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ def _configure_data_instance_validators(self):
]
return data_instance_validator_objects

def log_errors(self):
def log_errors(self,raise_errors):
for error_msg in self.validation_errors:
logging.error(error_msg)
if len (self.validation_errors)>0:
if raise_errors:
raise Exception('There were errors during validaton, please check the log file')

def validate_contexts(self, all_data: List[Dict]):
print("Validating Data Contexts")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_BaseInstanceOverwiteException():
raise BaseInstanceOverwiteException(base_instance, extended_instance)
assert (
str(excinfo.value)
== f"The extended instace :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
== f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
)


Expand Down

0 comments on commit 1c44e46

Please sign in to comment.