Skip to content

Commit

Permalink
Ticket 381 and Enhancement - Invalid syntax freezes the JsonPreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Nov 15, 2024
1 parent 855d7cb commit 2fc7b76
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(self, syntax: CSyntaxType = CSyntaxType.python , currentCfg : dict
self.currentCfg = currentCfg
self.dUpdatedParams = {}
self.lDotInParamName = []
self.bDuplicatedKeys = True
self.bJSONPreCheck = False
self.jsonCheck = {}
self.JPGlobals = {}
self.pythonTypeError = ["object is not subscriptable",
Expand Down Expand Up @@ -241,7 +241,7 @@ def __reset(self) -> None:
self.recursive_level = 0
self.dUpdatedParams = {}
self.lDotInParamName = []
self.bDuplicatedKeys = True
self.bJSONPreCheck = False
self.jsonCheck = {}
self.JPGlobals = {}

Expand Down Expand Up @@ -277,7 +277,7 @@ def __processImportFiles(self, input_data : dict) -> dict:
self.__reset()
raise Exception(errorMsg)
if '${' in value:
if self.bDuplicatedKeys: # self.bDuplicatedKeys is set False when handling pre-check JSON files by __preCheckJsonFile()
if not self.bJSONPreCheck: # self.bJSONPreCheck is set True when handling pre-check JSON files by __preCheckJsonFile()
value = self.lDynamicImports.pop(0)
if '${' in value:
dynamicImported = re.search(rf'^(.*){CNameMangling.DYNAMICIMPORTED.value}(.*)$', value)
Expand Down Expand Up @@ -325,7 +325,7 @@ def __processImportFiles(self, input_data : dict) -> dict:

self.recursive_level = self.recursive_level - 1 # descrease recursive level
else:
if self.bDuplicatedKeys:
if not self.bJSONPreCheck:
specialCharacters = r'$[]{}'
tmpOutdict = copy.deepcopy(out_dict)
for k1, v1 in tmpOutdict.items():
Expand Down Expand Up @@ -569,7 +569,7 @@ def __getNestedValue(sNestedParam : str):
exec(sExec, locals(), ldict)
tmpValue = ldict['value']
except Exception as error:
if self.iDynamicImport > 0:
if self.bJSONPreCheck:
sNestedParam = self.__removeTokenStr(sNestedParam)
tmpValue = sNestedParam.replace('$$', '$')
pass
Expand Down Expand Up @@ -614,7 +614,7 @@ def __getNestedValue(sNestedParam : str):
for var in referVars:
sVar = self.__handleDotInNestedParam(var[0]) if re.search(r'\${.+\..+}', var[0]) else var[0]
tmpValue = __getNestedValue(sVar)
if self.iDynamicImport > 0:
if self.bJSONPreCheck:
if "${" in tmpValue and bConvertToStr:
tmpValue = tmpValue + CNameMangling.STRINGCONVERT.value
if (isinstance(tmpValue, list) or isinstance(tmpValue, dict)) and bConvertToStr:
Expand Down Expand Up @@ -698,7 +698,7 @@ def __getNestedValue(sNestedParam : str):
exec(sExec, locals(), ldict)
tmpValue = ldict['value']
except Exception as error:
if self.iDynamicImport > 0:
if self.bJSONPreCheck:
sNestedParam = self.__removeTokenStr(sNestedParam)
tmpValue = sNestedParam.replace('$$', '$')
pass
Expand Down Expand Up @@ -1214,7 +1214,7 @@ def __handleList(lInput : list, bNested : bool) -> list:
else:
v = CString.NormalizePath(dynamicImported[2], sReferencePathAbs = dynamicImported[1])
if v == sLoopCheck:
if self.iDynamicImport == 0:
if not self.bJSONPreCheck:
self.__reset()
raise Exception(f"Invalid expression found: '{self.__removeTokenStr(initValue)}'.")
else:
Expand Down Expand Up @@ -1315,9 +1315,13 @@ def __checkNestedParam(self, sInput : str, bKey=False, bCheckKeyName=False) -> b
pattern = rf"^\${{\s*[^{re.escape(self.specialCharacters)}]+\s*}}(\[.*\])+$"
pattern1 = rf"\${{.+}}(\[.+\])*[^\[]*\${{"
pattern2 = r"\[[a-zA-Z0-9\.\-\+\${}'\s]*:[a-zA-Z0-9\.\-\+\${}'\s]*\]" # Slicing pattern
if CNameMangling.DYNAMICIMPORTED.value in sInput:
dynamicImported = re.search(rf'^(.*){CNameMangling.DYNAMICIMPORTED.value}(.*)$', sInput)
sInput = dynamicImported[2]
# Checks special character in parameters
sTmpInput = sInput
bSpecialCharInParam = False
sCheckInput = sTmpInput
while sTmpInput.count("${") > 1:
lParams = re.findall(r'\${([^\$}]*)}', sTmpInput)
for param in lParams:
Expand All @@ -1326,8 +1330,9 @@ def __checkNestedParam(self, sInput : str, bKey=False, bCheckKeyName=False) -> b
bSpecialCharInParam = True
break
sTmpInput = sTmpInput.replace('${' + param + '}', '')
if bSpecialCharInParam:
if bSpecialCharInParam or sCheckInput==sTmpInput:
break
sCheckInput = sTmpInput
if "${" not in sInput:
return True
errorMsg = None
Expand All @@ -1354,17 +1359,17 @@ def __checkNestedParam(self, sInput : str, bKey=False, bCheckKeyName=False) -> b
re.match(r"^[\s\"]*\${[^!@#%\^&\*\(\)=|;,<>?/`~]+[\s\"]*$", sInput)):
errorMsg = f"Invalid syntax! One or more than one closed curly bracket is missing in \
expression '{self.__removeTokenStr(sInput.strip())}'."
elif not re.match(r"^\${.+[}\]]+$", sInput) or (re.search(pattern1, sInput) and not bKey):
if self.iDynamicImport==0:
if CNameMangling.STRINGCONVERT.value not in sInput and CNameMangling.DUPLICATEDKEY_01.value not in sInput:
sTmpInput = re.sub(r"(\.\${[a-zA-Z0-9\.\_]+}(\[[^\[]+\])*)", "", sInput)
if not re.match(r"^\s*\${[a-zA-Z0-9\.\_]+}(\[[^\[]+\])*\s*$", sTmpInput):
errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput)}' - The double quotes are missing!!!"
elif CNameMangling.STRINGCONVERT.value in sInput:
sInput = sInput.replace(CNameMangling.STRINGCONVERT.value, '')
if re.match(r'^\${[^}]+}+(\[+[^\]]+\]+)*$', sInput) and \
(sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]")):
errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}' - The brackets mismatch!!!"
elif (not re.match(r"^\${.+[}\]]+$", sInput) or (re.search(pattern1, sInput) and not bKey)) \
and not self.bJSONPreCheck:
if CNameMangling.STRINGCONVERT.value not in sInput and CNameMangling.DUPLICATEDKEY_01.value not in sInput:
sTmpInput = re.sub(r"(\.\${[a-zA-Z0-9\.\_]+}(\[[^\[]+\])*)", "", sInput)
if not re.match(r"^\s*\${[a-zA-Z0-9\.\_]+}(\[[^\[]+\])*\s*$", sTmpInput):
errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput)}' - The double quotes are missing!!!"
elif CNameMangling.STRINGCONVERT.value in sInput:
sInput = sInput.replace(CNameMangling.STRINGCONVERT.value, '')
if re.match(r'^\${[^}]+}+(\[+[^\]]+\]+)*$', sInput) and \
(sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]")):
errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}' - The brackets mismatch!!!"
elif sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]"):
if CNameMangling.STRINGCONVERT.value not in sInput:
errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}' - The brackets mismatch!!!"
Expand Down Expand Up @@ -1848,10 +1853,10 @@ def __handleLastElement(sInput : str) -> str:
# verifying duplicated keys later. The pre-check method also checks dynamic
# imported files in JSON files.
if firstLevel:
self.bDuplicatedKeys = False
self.bJSONPreCheck = True
sDummyData = self.__preCheckJsonFile(sJsonDataUpdated, CJSONDecoder)
self.iDynamicImport = 0
self.bDuplicatedKeys = True
self.bJSONPreCheck = False

# Load Json object with checking duplicated keys feature is enabled.
# The duplicated keys feature uses the self.jsonCheck object to check duplicated keys.
Expand Down

0 comments on commit 2fc7b76

Please sign in to comment.