From 112bb899ce4d64781ad5d74223bd1e25fda68cb7 Mon Sep 17 00:00:00 2001 From: Henry LE BERRE Date: Thu, 18 Jul 2024 00:41:05 +0200 Subject: [PATCH] Switch to fastjsonschema --- toolchain/dependencies/CMakeLists.txt | 1 + toolchain/mfc/case.py | 12 +++++------- toolchain/mfc/run/case_dicts.py | 12 +++++++++++- toolchain/mfc/run/input.py | 5 +++-- toolchain/mfc/test/case.py | 7 +++---- toolchain/mfc/test/test.py | 4 ++-- toolchain/requirements.txt | 2 +- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/toolchain/dependencies/CMakeLists.txt b/toolchain/dependencies/CMakeLists.txt index baa3f3991e..f0519a5cb0 100644 --- a/toolchain/dependencies/CMakeLists.txt +++ b/toolchain/dependencies/CMakeLists.txt @@ -68,6 +68,7 @@ if (MFC_SILO) GIT_REPOSITORY "https://github.com/LLNL/Silo" GIT_TAG 438477c80d32a3e1757d4584b993f382cace1535 GIT_PROGRESS ON + GIT_SHALLOW ON PATCH_COMMAND "${GIT_EXECUTABLE}" stash && "${GIT_EXECUTABLE}" apply "${CMAKE_SOURCE_DIR}/Silo.patch" && "${GIT_EXECUTABLE}" apply "${CMAKE_SOURCE_DIR}/Silo-GNU-13.patch" diff --git a/toolchain/mfc/case.py b/toolchain/mfc/case.py index 2d05be3e08..354042e9c3 100644 --- a/toolchain/mfc/case.py +++ b/toolchain/mfc/case.py @@ -1,5 +1,4 @@ -import re, json, math, copy, dataclasses, jsonschema -import jsonschema.exceptions +import re, json, math, copy, dataclasses, fastjsonschema from . import common from . import build @@ -71,13 +70,12 @@ def validate_params(self, origin_txt: str = None): is assigned a vlaie of the wrong type, this method throws an exception highlighting the violating parameter and specifying what it expects.''' try: - jsonschema.validate(self.params, case_dicts.SCHEMA) - except jsonschema.exceptions.ValidationError as e: - exception_txt = f"Case parameter '{e.path[0]}' is of the wrong type. Expected type: '{e.schema['type']}'. Got value '{e.instance}' of type '{type(e.instance).__name__}'." + case_dicts.get_validator()(self.params) + except fastjsonschema.JsonSchemaException as e: if origin_txt: - exception_txt = f"Origin: {origin_txt}. {exception_txt}" + raise common.MFCException(f"{origin_txt}: {e}") - raise common.MFCException(exception_txt) + raise common.MFCException(f"{e}") def __get_ndims(self) -> int: return 1 + min(int(self.params.get("n", 0)), 1) + min(int(self.params.get("p", 0)), 1) diff --git a/toolchain/mfc/run/case_dicts.py b/toolchain/mfc/run/case_dicts.py index b7f4360e84..3b631cd552 100644 --- a/toolchain/mfc/run/case_dicts.py +++ b/toolchain/mfc/run/case_dicts.py @@ -1,3 +1,7 @@ +from functools import cache + +import fastjsonschema + from enum import Enum from ..state import ARG @@ -347,7 +351,8 @@ def analytic(self): SCHEMA = { "type": "object", - "properties": _properties + "properties": _properties, + "additionalProperties": False, } @@ -362,3 +367,8 @@ def get_input_dict_keys(target_name: str) -> list: return result return [ x for x in result if x not in CASE_OPTIMIZATION ] + + +@cache +def get_validator(): + return fastjsonschema.compile(SCHEMA) diff --git a/toolchain/mfc/run/input.py b/toolchain/mfc/run/input.py index bc791c9bfb..63c120b0cb 100644 --- a/toolchain/mfc/run/input.py +++ b/toolchain/mfc/run/input.py @@ -89,7 +89,7 @@ def clean(self, _targets) -> None: # Load the input file -def load(filepath: str = None, args: typing.List[str] = None, empty_data: dict = None) -> MFCInputFile: +def load(filepath: str = None, args: typing.List[str] = None, empty_data: dict = None, do_print: bool = True) -> MFCInputFile: if not filepath: if empty_data is None: raise common.MFCException("Please provide an input file.") @@ -100,7 +100,8 @@ def load(filepath: str = None, args: typing.List[str] = None, empty_data: dict = filename: str = filepath.strip() - cons.print(f"Acquiring [bold magenta]{filename}[/bold magenta]...") + if do_print: + cons.print(f"Acquiring [bold magenta]{filename}[/bold magenta]...") dirpath: str = os.path.abspath(os.path.dirname(filename)) dictionary: dict = {} diff --git a/toolchain/mfc/test/case.py b/toolchain/mfc/test/case.py index 9b70f5fe5d..5215639146 100644 --- a/toolchain/mfc/test/case.py +++ b/toolchain/mfc/test/case.py @@ -247,7 +247,7 @@ def get_uuid(self) -> str: def to_case(self) -> TestCase: dictionary = {} if self.path: - dictionary.update(input.load(self.path, self.args).params) + dictionary.update(input.load(self.path, self.args, do_print=False).params) for key, value in dictionary.items(): if not isinstance(value, str): @@ -262,7 +262,6 @@ def to_case(self) -> TestCase: if self.functor: self.functor(dictionary) - # print(dictionary) return TestCase(self.trace, dictionary, self.ppn) @@ -288,7 +287,7 @@ def pop(self) -> None: # pylint: disable=too-many-arguments def define_case_f(trace: str, path: str, args: List[str] = None, newMods: dict = None, ppn: int = None, functor: Callable = None) -> TestCaseBuilder: - return TestCaseBuilder(trace, newMods or {}, path, args or [], ppn, functor) + return TestCaseBuilder(trace, newMods or {}, path, args or [], ppn or 1, functor) def define_case_d(stack: CaseGeneratorStack, newTrace: str, newMods: dict, ppn: int = None, functor: Callable = None) -> TestCaseBuilder: @@ -307,4 +306,4 @@ def define_case_d(stack: CaseGeneratorStack, newTrace: str, newMods: dict, ppn: if not common.isspace(trace): traces.append(trace) - return TestCaseBuilder(' -> '.join(traces), mods, None, [], ppn, functor) + return TestCaseBuilder(' -> '.join(traces), mods, None, [], ppn or 1, functor) diff --git a/toolchain/mfc/test/test.py b/toolchain/mfc/test/test.py index 42376d2710..d0a6c98f2b 100644 --- a/toolchain/mfc/test/test.py +++ b/toolchain/mfc/test/test.py @@ -62,7 +62,7 @@ def test(): # pylint: disable=global-statement, global-variable-not-assigned global nFAIL - cases = [ _.to_case() for _ in list_cases() ] + cases = list_cases() # Delete UUIDs that are not in the list of cases from tests/ if ARG("remove_old_tests"): @@ -75,7 +75,7 @@ def test(): return - cases = __filter(cases) + cases = [ _.to_case() for _ in __filter(cases) ] if ARG("list"): table = rich.table.Table(title="MFC Test Cases", box=rich.table.box.SIMPLE) diff --git a/toolchain/requirements.txt b/toolchain/requirements.txt index 523bf9fe9c..9bef946769 100644 --- a/toolchain/requirements.txt +++ b/toolchain/requirements.txt @@ -10,4 +10,4 @@ pylint argparse fprettify dataclasses -jsonschema +fastjsonschema