json-parser-prolog-lisp/test_prolog.py
2022-12-05 22:05:23 +01:00

36 lines
1 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Given the following bidirectional Prolog predicate that can serialize and deserialize JSON:
jsonread(Filename, JsonObj)
This script takes a JSON file path as an argument, runs SWI-Prolog
loading the `jsonparse.pl` library and runs a goal that parses the specified JSON file.
The Python script must exit with 0 if the jsonread/2 predicate returned true, 1 otherwise.
"""
import sys
import subprocess
def run_prolog(json_file_path):
"""
Run SWI-Prolog using subprocess loading the `jsonparse.pl` library and runs a goal that parses the specified JSON file.
"""
# Run SWI-Prolog using subprocess
try:
subprocess.run(["swipl", "-s", "Prolog/jsonparse.pl", "-g",
"jsonread('{}', _)".format(json_file_path), "-t", "halt"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError:
return False
return True
if __name__ == "__main__":
if run_prolog(sys.argv[1]):
sys.exit(0)
else:
sys.exit(1)