From dbd96d8d0c8c2181f578b3f01cc400872b737d40 Mon Sep 17 00:00:00 2001 From: Anthony Islas Date: Thu, 23 Jan 2025 15:57:25 -0700 Subject: [PATCH] Add support for primitive json comments To provide some support for comments within the test configs a simple JSON with comments decoder class has been introduced. This only provides support for leading // style comments in an effort to remain lightweight and work with native Python installs, no dependencies on external packages. This should prove sufficient to allow a modicum of annotation to test configs. --- .ci/JSONCDecoder.py | 12 ++++++++++++ .ci/runner.py | 18 +++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .ci/JSONCDecoder.py diff --git a/.ci/JSONCDecoder.py b/.ci/JSONCDecoder.py new file mode 100644 index 0000000..dbf746b --- /dev/null +++ b/.ci/JSONCDecoder.py @@ -0,0 +1,12 @@ +# https://stackoverflow.com/a/72168909 +import json +from typing import Any +class JSONCDecoder( json.JSONDecoder ): + def __init__( self, **kw ) : + super().__init__( **kw ) + + def decode( self, s : str ) -> Any : + # Sanitize the input string for leading // comments ONLY and replace with + # blank line so that line numbers are preserved + s = '\n'.join( l if not l.lstrip().startswith( "//" ) else "" for l in s.split( '\n' ) ) + return super().decode( s ) diff --git a/.ci/runner.py b/.ci/runner.py index 99257d8..42a638a 100755 --- a/.ci/runner.py +++ b/.ci/runner.py @@ -12,7 +12,7 @@ from contextlib import redirect_stdout from datetime import timedelta - +import JSONCDecoder import SubmitCommon as sc from SubmitAction import SubmitAction @@ -518,7 +518,7 @@ def runSuite( options ) : with redirect_stdout( redirect ) : testSuite = Suite( basename, - json.load( fp ), + json.load( fp, cls=JSONCDecoder.JSONCDecoder ), opts, options, parent=options.globalPrefix, @@ -529,13 +529,13 @@ def runSuite( options ) : # print( options.message ) else : testSuite = Suite( - basename, - json.load( fp ), - opts, - options, - parent=options.globalPrefix, - rootDir=root - ) + basename, + json.load( fp, cls=JSONCDecoder.JSONCDecoder ), + opts, + options, + parent=options.globalPrefix, + rootDir=root + ) success, logs = testSuite.run( options.tests ) # if success and options.message : # print( options.message )