Skip to content

Commit

Permalink
Switch to fastjsonschema
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleberre committed Jul 17, 2024
1 parent 9fb6279 commit 112bb89
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions toolchain/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions toolchain/mfc/case.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion toolchain/mfc/run/case_dicts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from functools import cache

import fastjsonschema

from enum import Enum
from ..state import ARG

Expand Down Expand Up @@ -347,7 +351,8 @@ def analytic(self):

SCHEMA = {
"type": "object",
"properties": _properties
"properties": _properties,
"additionalProperties": False,
}


Expand All @@ -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)
5 changes: 3 additions & 2 deletions toolchain/mfc/run/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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 = {}
Expand Down
7 changes: 3 additions & 4 deletions toolchain/mfc/test/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -262,7 +262,6 @@ def to_case(self) -> TestCase:

if self.functor:
self.functor(dictionary)
# print(dictionary)

return TestCase(self.trace, dictionary, self.ppn)

Expand All @@ -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:
Expand All @@ -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)
4 changes: 2 additions & 2 deletions toolchain/mfc/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion toolchain/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pylint
argparse
fprettify
dataclasses
jsonschema
fastjsonschema

0 comments on commit 112bb89

Please sign in to comment.