Skip to content

Commit

Permalink
Update example to include skipped check and documentation generator (#36
Browse files Browse the repository at this point in the history
)

Signed-off-by: hoangtungdinh <[email protected]>
Co-authored-by: hoangtungdinh <[email protected]>
  • Loading branch information
hoangtungdinh and hoangtungdinh authored Sep 16, 2024
1 parent f7ea664 commit 7cac0a9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 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

0 comments on commit 7cac0a9

Please sign in to comment.