diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index 4d0583bd..3ffc8ad7 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -178,6 +178,7 @@ def __init__(self, syntax: CSyntaxType = CSyntaxType.json , currentCfg : dict = self.syntax = syntax self.currentCfg = currentCfg self.lUpdatedParams = [] + self.lNestedParams = [] def __sNormalizePath(self, sPath : str) -> str: @@ -501,7 +502,16 @@ def jsonLoad(self, jFile : str, masterFile : bool = True): except Exception as reason: raise Exception(f"Could not read json file '{jFile}' due to: '{reason}'!") - + for line in sJsonData.splitlines(): + if re.match('\s*\"\s*.+\"\s*:\s*.+', line.lower()): + key_value = re.split('\"\s*:\s*', line) + if re.match('^\s*\${\s*', key_value[0].lower()): + key_value[0] = re.sub('^\s*\"', '', key_value[0]) + self.lNestedParams.append(key_value[0]) + if re.match('^.*\s*\${\s*', key_value[1].lower()): + key_value[1] = re.sub('^\s*\"\s*(.+)\s*\"\s*,*\s*$', '\\1', key_value[1]) + self.lNestedParams.append(key_value[1]) + currentDir = os.getcwd() os.chdir(jsonPath) @@ -524,6 +534,19 @@ def jsonLoad(self, jFile : str, masterFile : bool = True): if masterFile: for k, v in oJson.items(): globals().update({k:v}) + + # Checking availability of nested parameters before updating and replacing. + for param in self.lNestedParams: + parseNestedParam = self.__nestedParamHandler(param) + tmpParseNestedParam = re.sub('\\${\s*(.*?)\s*}', '\\1', parseNestedParam) + sExec = "value = " + tmpParseNestedParam if isinstance(tmpParseNestedParam, str) else \ + "value = " + str(tmpParseNestedParam) + try: + ldict = {} + exec(sExec, globals(), ldict) + except: + raise Exception(f"The variable '{tmpParseNestedParam}' is not available!") + oJson = self.__updateAndReplaceNestedParam(oJson) return oJson \ No newline at end of file diff --git a/JsonPreprocessor/version.py b/JsonPreprocessor/version.py index 75c285c2..fe023377 100644 --- a/JsonPreprocessor/version.py +++ b/JsonPreprocessor/version.py @@ -18,6 +18,6 @@ # # Version and date of JsonPreprocessor # -VERSION = "0.1.3" -VERSION_DATE = "05.07.2022" +VERSION = "0.1.4" +VERSION_DATE = "14.09.2022"