diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index 92e58d2d..2b625faa 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -68,6 +68,7 @@ class CNameMangling(Enum): STRINGCONVERT = "__ConvertParameterToString__" LISTINDEX = "__IndexOfList__" SLICEINDEX = "__SlicingIndex__" + STRINGVALUE = "__StringValueMake-up__" class CPythonJSONDecoder(json.JSONDecoder): """ @@ -888,116 +889,6 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''): del tmpJson return oJson, bNested - def __checkAndUpdateKeyValue(self, sInputStr: str, nestedKey = False) -> str: - """ -This function checks and makes up all nested parameters in JSON configuration files. - -**Arguments:** - -* ``sInputStr*`` - - / *Condition*: required / *Type*: str / - - Key or value which is parsed from JSON configuration file. - -**Returns:** - The string after nested parameters are made up. - - Ex: - - Nested param ${abc}['xyz'] -> "${abc}['xyz']" - - Nested param "${abc}['xyz']" -> "${abc}['xyz']__ConvertParameterToString__" - """ - def __recursiveNestedHandling(sInputStr: str, lNestedParam: list) -> str: - """ -This method handles nested parameters are called recursively in a string value. - """ - tmpList = [] - for item in lNestedParam: - item = re.sub(r'([$()\[\]])', r'\\\1', item) - pattern = rf"(\${{\s*[^{re.escape(self.specialCharacters)}]*" + item + \ - rf"[^{re.escape(self.specialCharacters)}]*\s*}}(\[\s*.+\s*\])*)" - if re.search(pattern, sInputStr, re.UNICODE): - sInputStr = re.sub("(" + pattern + ")", "\\1" + CNameMangling.STRINGCONVERT.value, sInputStr, re.UNICODE) - tmpResults = re.findall("(" + pattern + CNameMangling.STRINGCONVERT.value + ")", sInputStr, re.UNICODE) - for result in tmpResults: - tmpList.append(result[0]) - if tmpList != []: - sInputStr = __recursiveNestedHandling(sInputStr, tmpList) - - return sInputStr - - variablePattern = rf"[^{re.escape(self.specialCharacters)}]+" - indexPattern = r"\[[\s\-\+\d]*\]|\[.*:.*\]" - dictPattern = r"\[+\s*'.+'\s*\]+|\[+\s*\d+\s*\]+|\[+\s*\${\s*" + variablePattern + r"\s*}.*\]+|" + indexPattern - nestedPattern = r"\${\s*" + variablePattern + r"(\.*\${\s*" + variablePattern + r"\s*})*" + r"\s*}(" + dictPattern + r")*" - valueStrPattern = r"[\"|\']\s*[0-9A-Za-z_\-\s*]+[\"|\']" - valueNumberPattern = r"[0-9\.]+" - - if "${" in sInputStr: - if re.match(r"^\s*" + nestedPattern + r"[\s,\]}]*$", sInputStr, re.UNICODE): - iCheck = sInputStr.count("]") - sInputStr.count("[") - if iCheck > 0: - tmpItems = sInputStr.split("]") - index = len(tmpItems) - iCheck - sParam = "]".join(tmpItems[:index]) - dReplacements = {"$":"\$", "[":"\[", "]":"\]", "-":"\-", "+":"\+"} - paramPattern = self.__multipleReplace(sParam, dReplacements) - sInputStr = re.sub("(" + paramPattern + ")", "\"\\1\"", sInputStr, re.UNICODE) - else: - sInputStr = re.sub("(" + nestedPattern + ")", "\"\\1\"", sInputStr, re.UNICODE) - nestedParam = re.sub(r"^\s*\"(.+)\".*$", "\\1", sInputStr) - self.lNestedParams.append(nestedParam) - elif re.match(r"^\s*\"\s*" + nestedPattern + r"\"[\s,\]}]*$", sInputStr, re.UNICODE): - nestedParam = re.sub(r"^\s*\"(.+)\".*$", "\\1", sInputStr) - self.lNestedParams.append(nestedParam) - sInputStr = sInputStr.replace(nestedParam, nestedParam + CNameMangling.STRINGCONVERT.value) - elif ((re.match(r"[\s{\[]*\".+\"\s*", sInputStr) and sInputStr.count("\"")==2) \ - or (re.match(r"^\s*\${.+}[,\s]*$", sInputStr) and sInputStr.count("{")==sInputStr.count("}") \ - and not re.search(r"(? str: - items = re.split("\s*,\s*", sInput) - j=0 - newItem = "" - for item in items: - j+=1 - if j dict: listKeys = list(dInput.keys()) dictValues = {} @@ -1235,6 +1114,28 @@ def __checkKeynameFormat(oJson : dict): self.__checkNestedParam(item) elif isinstance(v, dict): __checkKeynameFormat(v) + + def __handleLastElement(sInput : str) -> str: + ''' +This function handle a last element of a list or dictionary + ''' + param = re.search(r'(' + nestedPattern + r')', sInput) + if param is not None and re.match(r'^[\s\[\]{}]*$', sInput.replace(param[0], '')): + sParam = param[0] + if sParam.count('[')1 and not re.match(r'^\[|{.+$', item.strip()): + dReplacements = {"$":"\$", "[":"\[", "]":"\]", ".":"\.", "-":"\-", "+":"\+"} + tmpPattern1 = self.__multipleReplace(preItem, dReplacements) + tmpPattern2 = self.__multipleReplace(curItem, dReplacements) + if re.search(tmpPattern1 + '\s*:\s*' + tmpPattern2, curLine): + item = re.sub(r'^\s*(.+)\s*', '"\\1"', item) + bHandle = True + if not bHandle: + subItems = item.split(',') + iSubItems = len(subItems) -1 if subItems[-1]=='' else len(subItems) + newSubItem = "" + j=1 + for subItem in subItems: + if "${" in subItem: + if iSubItems>1 and j