Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,32 @@ def __handleDotdictFormat(self, lInputListParams : list, lParams: list = []) ->
else:
return self.__handleDotdictFormat(lInputListParams, lParams)

def __checkAndCreateNewElement(self, sKey: str, value):
'''
This method check and create new elements if they are not exist.
'''
rootKey = re.sub("\[.*\]", "", sKey)
subElements = re.findall("\[\s*'([0-9A-Za-z_]+[0-9A-Za-z\.\-_]*)'\s*\]", sKey)
if len(subElements) <= 1:
return
else:
for index, element in enumerate(subElements):
if index < len(subElements) - 1:
rootKey = rootKey + "['" + element + "']"
sExec = "dumpData = " + rootKey
try:
exec(sExec)
except:
sExec = rootKey + " = {}"
try:
exec(sExec, globals())
except Exception as error:
self.__reset()
errorMsg = f"Could not set variable '{sKey}' with value '{value}'! Reason: {error}"
raise Exception(errorMsg)
else:
continue

def __updateAndReplaceNestedParam(self, oJson : dict, bNested : bool = False, recursive : bool = False):
'''
This method replaces all nested parameters in key and value of a json object .
Expand All @@ -527,32 +553,11 @@ def __updateAndReplaceNestedParam(self, oJson : dict, bNested : bool = False, re
Output Json object as dictionary with all variables resolved.
'''

def __checkAndCreateNewElement(sKey: str):
'''
This method check and create new elements if they are not exist.
'''
rootKey = re.sub("\[.*\]", "", sKey)
subElements = re.findall("\[\s*'([0-9A-Za-z_]+[0-9A-Za-z\.\-_]*)'\s*\]", sKey)
if len(subElements) <= 1:
return
else:
for index, element in enumerate(subElements):
if index < len(subElements) - 1:
rootKey = rootKey + "['" + element + "']"
sExec = "dumpData = " + rootKey
try:
exec(sExec)
except:
sExec = rootKey + " = {}"
exec(sExec, globals())
else:
continue

def __jsonUpdated(k, v, oJson, bNested, keyNested = ''):
if keyNested != '':
del oJson[keyNested]
if '[' in k:
__checkAndCreateNewElement(k)
self.__checkAndCreateNewElement(k, v)
sExec = k + " = \"" + v + "\"" if isinstance(v, str) else k + " = " + str(v)
try:
exec(sExec, globals())
Expand Down Expand Up @@ -936,6 +941,14 @@ def __handleListElements(sInput : str) -> str:
oJson, bNested = self.__updateAndReplaceNestedParam(oJson)
for k, v in self.dUpdatedParams.items():
if '[' in k:
rootElement = k.split('[', 1)[0]
if rootElement in oJson:
self.__checkAndCreateNewElement(k, v)
sExec = "oJson['" + rootElement + "'] = " + rootElement
try:
exec(sExec)
except:
pass
if isinstance(v, str):
sExec = "oJson['" + k.split('[', 1)[0] + "'][" + k.split('[', 1)[1] + " = \"" + v + "\""
else:
Expand Down
2 changes: 1 addition & 1 deletion JsonPreprocessor/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
# Version and date of JsonPreprocessor
#
VERSION = "0.3.0"
VERSION_DATE = "06.09.2023"
VERSION_DATE = "12.09.2023"

18 changes: 18 additions & 0 deletions test/JPP_TestUsecases.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,43 @@ JPP_0101|DATA_INTEGRITY|GOODCASE|JSON file with string containing several separa
JPP_0102|DATA_INTEGRITY|GOODCASE|JSON file with string containing more special characters, masked special characters and escape sequences
JPP_0200|PARAMETER_SUBSTITUTION|GOODCASE|JSON file with nested parameter / string parameter substitution in parameter value
JPP_0201|PARAMETER_SUBSTITUTION|GOODCASE|JSON file with nested parameter / string parameter substitution in parameter name
JPP_0202|PARAMETER_SUBSTITUTION|GOODCASE|JSON file with nested parameter / index parameter substitution in parameter name / standard notation
JPP_0203|PARAMETER_SUBSTITUTION|GOODCASE|JSON file with nested parameter / index parameter substitution in parameter name / dotdict notation
JPP_0204|PARAMETER_SUBSTITUTION|GOODCASE|JSON file with nested parameter / index parameter substitution in parameter value / standard notation
JPP_0250|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / string parameter substitution in parameter value / innermost parameter not existing
JPP_0251|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / string parameter substitution in parameter name / in between parameter not existing
JPP_0252|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / index parameter substitution in parameter name / standard notation / index parameter not existing
JPP_0253|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / index parameter substitution in parameter name / dotdict notation / index parameter not existing
JPP_0254|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / index parameter substitution in parameter value / standard notation / index parameter not existing
JPP_0255|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / index parameter substitution in parameter value / dotdict notation / index parameter not existing
JPP_0257|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / key parameter substitution in parameter name / dotdict notation / milestone number not existing
JPP_0259|PARAMETER_SUBSTITUTION|BADCASE|JSON file with nested parameter / key parameter substitution in parameter value / dotdict notation / milestone number not existing
JPP_0300|VALUE_DETECTION|GOODCASE|JSON file with parameter of type 'list' / index (in square brackets) defined outside the curly brackets (valid syntax)
JPP_0301|VALUE_DETECTION|GOODCASE|JSON file with expression containing more closing elements '}' than opening elements '${' (valid syntax)
JPP_0302|VALUE_DETECTION|GOODCASE|JSON file with expression starting with '${' and ending with '}' / no further matching '${' and '}' in between (valid syntax)
JPP_0304|VALUE_DETECTION|GOODCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (valid syntax)
JPP_0350|VALUE_DETECTION|BADCASE|JSON file with parameter of type 'list' / index (in square brackets) defined inside the curly brackets (invalid syntax 1)
JPP_0351|VALUE_DETECTION|BADCASE|JSON file with parameter of type 'list' / index (in square brackets) defined inside the curly brackets (invalid syntax 2)
JPP_0352|VALUE_DETECTION|BADCASE|JSON file with parameter of type 'list' / index (in square brackets) defined inside the curly brackets (invalid syntax 3)
JPP_0353|VALUE_DETECTION|BADCASE|JSON file with expression containing more opening elements '${' than closing elements '}' (invalid syntax 1)
JPP_0354|VALUE_DETECTION|BADCASE|JSON file with expression containing more opening elements '${' than closing elements '}' (invalid syntax 2)
JPP_0357|VALUE_DETECTION|BADCASE|JSON file with expression containing more opening elements '${' than closing elements '}' (invalid syntax 5)
JPP_0359|VALUE_DETECTION|BADCASE|JSON file with expression containing more opening elements '${' than closing elements '}' (invalid syntax 6)
JPP_0361|VALUE_DETECTION|BADCASE|JSON file with expression containing more closing elements '}' than opening elements '${' (invalid syntax 1)
JPP_0367|VALUE_DETECTION|BADCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (invalid syntax 1)
JPP_0368|VALUE_DETECTION|BADCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (invalid syntax 2)
JPP_0369|VALUE_DETECTION|BADCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (invalid syntax 3)
JPP_0370|VALUE_DETECTION|BADCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (invalid syntax 4)
JPP_0371|VALUE_DETECTION|BADCASE|JSON file with expression starting with '${' and ending with '}', further matching '${' and '}' in between (not all nested) (invalid syntax 5)
JPP_0500|COMPOSITE_EXPRESSIONS|GOODCASE|JSON file with composite data structure (nested lists and dictionaries 1)
JPP_0501|COMPOSITE_EXPRESSIONS|GOODCASE|JSON file with composite data structure (nested lists and dictionaries 2)
JPP_0502|COMPOSITE_EXPRESSIONS|GOODCASE|JSON file with composite data structure (nested lists and dictionaries 3 / some key names with dots inside)
JPP_0506|COMPOSITE_EXPRESSIONS|GOODCASE|JSON file with composite strings containing several combinations of curly brackets and special characters before
JPP_0550|COMPOSITE_EXPRESSIONS|BADCASE|JSON file with composite data structure (nested lists and dictionaries / some key names with dots inside)
JPP_0900|COMMON_SYNTAX_VIOLATIONS|GOODCASE|JSON file with syntax error, that is commented out
JPP_0950|COMMON_SYNTAX_VIOLATIONS|BADCASE|JSON file with syntax error (1)
JPP_0951|COMMON_SYNTAX_VIOLATIONS|BADCASE|JSON file with syntax error (2)
JPP_0952|COMMON_SYNTAX_VIOLATIONS|BADCASE|JSON file with syntax error (3)
JPP_0953|COMMON_SYNTAX_VIOLATIONS|BADCASE|JSON file with syntax error (4): file is completely empty
JPP_0954|COMMON_SYNTAX_VIOLATIONS|BADCASE|JSON file with syntax error (5): file is empty (multiple pairs of brackets only)
JPP_1000|IMPLICIT_CREATION|GOODCASE|JSON file with dictionary keys to be created implicitly
Loading