Skip to content

Commit

Permalink
Enhancement 316 - Code improvement in jsonLoad & jsonLoads
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Aug 26, 2024
1 parent 7bab61e commit 0583c1f
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def __processImportFiles(self, input_data : dict) -> dict:
# self.__reset()
raise Exception(f"Cyclic imported json file '{abs_path_file}'!")

oJsonImport = self.jsonLoad(abs_path_file, masterFile=False)
oJsonImport = self.jsonLoad(abs_path_file)
self.jsonPath = currJsonPath
tmpOutdict = copy.deepcopy(out_dict)
for k1, v1 in tmpOutdict.items():
Expand Down Expand Up @@ -1372,7 +1372,7 @@ def __keyNameValidation(self, sInput):
self.__reset()
raise Exception(errorMsg)

def jsonLoad(self, jFile : str, masterFile : bool = True):
def jsonLoad(self, jFile : str):
"""
This method is the entry point of JsonPreprocessor.
Expand All @@ -1386,12 +1386,6 @@ def jsonLoad(self, jFile : str, masterFile : bool = True):
Path and name of main JSON file. The path can be absolute or relative and is also allowed to contain environment variables.
* ``masterFile``
/ *Condition*: optional / *Type*: bool /
Identifies the entry level when loading JSONP file in comparison with imported files levels. Default value is True
**Returns:**
* ``oJson``
Expand All @@ -1400,6 +1394,8 @@ def jsonLoad(self, jFile : str, masterFile : bool = True):
Preprocessed JSON file(s) as Python dictionary
"""
# Identifies the entry level when loading JSONP file in comparison with imported files levels.
masterFile = True if self.recursive_level==0 else False
jFile = CString.NormalizePath(jFile, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))
self.handlingFile.append(jFile)
if masterFile:
Expand All @@ -1415,9 +1411,9 @@ def jsonLoad(self, jFile : str, masterFile : bool = True):
except Exception as reason:
self.__reset()
raise Exception(f"Could not read json file '{jFile}' due to: '{reason}'!")
return self.jsonLoads(sJsonData, firstLevel=masterFile)
return self.jsonLoads(sJsonData)

def jsonLoads(self, sJsonpContent : str, referenceDir : str = '', firstLevel : bool = True):
def jsonLoads(self, sJsonpContent : str, referenceDir : str = ''):
"""
``jsonLoads`` loads the JSONP content, preprocesses it and returns the preprocessed result as Python dictionary.
Expand All @@ -1435,12 +1431,6 @@ def jsonLoads(self, sJsonpContent : str, referenceDir : str = '', firstLevel : b
A reference path for loading imported files.
* ``firstLevel``
/ *Condition*: optional / *Type*: bool /
Identifies the entry level when loading JSONP content in comparison with imported files levels.
**Returns:**
* ``oJson``
Expand Down Expand Up @@ -1517,6 +1507,8 @@ def __handleLastElement(sInput : str) -> str:
sInput = sInput.replace(sParam, '"' + sParam + '"')
return sInput

# Identifies the entry level when loading JSONP content in comparison with imported files levels.
firstLevel = True if self.recursive_level==0 else False
if referenceDir != '':
self.jsonPath = CString.NormalizePath(referenceDir, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))
if not os.path.exists(self.jsonPath):
Expand Down

0 comments on commit 0583c1f

Please sign in to comment.