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
25 changes: 24 additions & 1 deletion JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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
4 changes: 2 additions & 2 deletions JsonPreprocessor/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"