Skip to content

Commit

Permalink
Merge pull request #41 from asam-ev/2024-09-25
Browse files Browse the repository at this point in the history
Add maintenance work
  • Loading branch information
andreaskern74 authored Sep 26, 2024
2 parents 2097f1b + 03ee1c8 commit c9e5493
Show file tree
Hide file tree
Showing 11 changed files with 809 additions and 53 deletions.
6 changes: 2 additions & 4 deletions examples/json_validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ options:
- No issues found

```
$ python python json_validator.py \
-c config/valid.xml
$ python json_validator.py -c config/valid.xml
2024-08-05 10:38:07,978 - Initializing checks
2024-08-05 10:38:07,979 - JsonFile = data/valid.json
2024-08-05 10:38:07,979 - resultFile = json_bundle_report.xqar
Expand All @@ -48,8 +47,7 @@ $ python python json_validator.py \
- Issues found on file

```
$ python python json_validator.py \
-c config/invalid.xml
$ python json_validator.py -c config/invalid.xml
2024-08-05 10:38:11,946 - Initializing checks
2024-08-05 10:38:11,946 - JsonFile = data/invalid.json
2024-08-05 10:38:11,946 - resultFile = json_bundle_report.xqar
Expand Down
17 changes: 17 additions & 0 deletions examples/json_validator/config/skipped.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version='1.0' encoding='UTF-8'?>
<Config>

<Param name="JsonFile" value="data/non_existing_file.json" />

<CheckerBundle application="jsonBundle">
<Param name="resultFile" value="json_bundle_report.xqar" />
<Checker checkerId="jsonChecker" maxLevel="1" minLevel="3" />
</CheckerBundle>


<ReportModule application="TextReport">
<Param name="strInputFile" value="Result.xqar" />
<Param name="strReportFile" value="Report.txt" />
</ReportModule>

</Config>
55 changes: 33 additions & 22 deletions examples/json_validator/json_validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path
import argparse
import logging
from datetime import datetime
Expand All @@ -19,7 +19,7 @@ def is_valid_json(file_path):
with open(file_path, "r") as file:
json.load(file)
return True
except (json.JSONDecodeError, FileNotFoundError, IOError):
except json.JSONDecodeError:
return False


Expand All @@ -46,7 +46,7 @@ def main():
logging.info(f"JsonFile = {json_file}")
logging.info(f"resultFile = {result_file}")

# 1. Register checker bundle
# Register checker bundle
result.register_checker_bundle(
name=BUNDLE_NAME,
build_date=datetime.today().strftime("%Y-%m-%d"),
Expand All @@ -56,7 +56,7 @@ def main():
)
result.set_result_version(version=BUNDLE_VERSION)

# 2. Register checker
# Register checker
result.register_checker(
checker_bundle_name=BUNDLE_NAME,
checker_id=CHECKER_ID,
Expand All @@ -74,29 +74,40 @@ def main():
rule_full_name="valid_schema",
)

# Execute the check logic
is_valid = is_valid_json(json_file)

if not is_valid:
issue_id = result.register_issue(
# Check the precondition (whether the input file exists).
file_path = Path(json_file)
if file_path.exists():
# Execute the check logic as the precondition holds
is_valid = is_valid_json(json_file)

if not is_valid:
issue_id = result.register_issue(
checker_bundle_name=BUNDLE_NAME,
checker_id=CHECKER_ID,
description="The input file is not a valid json file.",
level=IssueSeverity.ERROR,
rule_uid=rule_uid,
)

logging.info(
f"Issues found - {result.get_checker_issue_count(checker_bundle_name=BUNDLE_NAME, checker_id=CHECKER_ID)}"
)
result.set_checker_status(
checker_bundle_name=BUNDLE_NAME,
checker_id=CHECKER_ID,
description="Issue flagging when input file contains no valid json info",
level=IssueSeverity.ERROR,
rule_uid=rule_uid,
status=StatusType.COMPLETED,
)
else:
# Skip the check as the precondition does not hold
result.set_checker_status(
checker_bundle_name=BUNDLE_NAME,
checker_id=CHECKER_ID,
status=StatusType.SKIPPED,
)

logging.info(
f"Issues found - {result.get_checker_issue_count(checker_bundle_name=BUNDLE_NAME, checker_id=CHECKER_ID)}"
)
result.set_checker_status(
checker_bundle_name=BUNDLE_NAME,
checker_id=CHECKER_ID,
status=StatusType.COMPLETED,
)

# 4. Write result file
# Write result file
result.write_to_file(result_file)
result.write_markdown_doc("generated_documentation.md")

logging.info(f"Report file written to {result_file}")
logging.info(f"Done")
Expand Down
2 changes: 1 addition & 1 deletion examples/json_validator/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
qc_baselib @ git+https://github.com/asam-ev/qc-baselib-py@develop
asam-qc-baselib @ git+https://github.com/asam-ev/qc-baselib-py@develop
8 changes: 7 additions & 1 deletion qc_baselib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

from typing import Union, List
from typing import Union, List, Dict
from .models import config, common, IssueSeverity


Expand Down Expand Up @@ -109,6 +109,12 @@ def get_config_param(self, param_name: str) -> Union[str, int, float, None]:

return param.value

def get_all_global_config_param(self) -> Dict[str, Union[str, int, float]]:
params = {}
for param in self._configuration.params:
params[param.name] = param.value
return params

def get_checker_bundle_param(
self, checker_bundle_name: str, param_name: str
) -> Union[str, int, float, None]:
Expand Down
1 change: 1 addition & 0 deletions qc_baselib/models/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class StatusType(str, enum.Enum):


class CheckerType(BaseXmlModel, validate_assignment=True, search_mode="unordered"):
params: List[ParamType] = element(tag="Param", default=[])
addressed_rule: List[RuleType] = element(tag="AddressedRule", default=[])
issues: List[IssueType] = element(tag="Issue", default=[])
metadata: List[MetadataType] = element(tag="Metadata", default=[])
Expand Down
Loading

0 comments on commit c9e5493

Please sign in to comment.