Skip to content

Commit 2c5c30f

Browse files
committed
Ticket 389 - Update error message for cyclic import detection.
1 parent feafe18 commit 2c5c30f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

JsonPreprocessor/CJsonPreprocessor.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,17 @@ def __processImportFiles(self, input_data : dict) -> dict:
408408
if not self.bDynamicImport or not self.bJSONPreCheck or self.currentNode.value==abs_path_file:
409409
importPath = self.currentNode.getPathToRoot() # Get the import path from importTree to check Cyclic import
410410
if abs_path_file in importPath:
411-
raise Exception(f"Cyclic import detection while handling the file '{abs_path_file}'!")
411+
previousImport1 = importPath[0]
412+
previousImport2 = importPath[-1]
413+
for path in importPath:
414+
if path == abs_path_file:
415+
break
416+
previousImport1 = path
417+
if previousImport1 == abs_path_file or previousImport2 == abs_path_file:
418+
errorMsg = f"Cyclic import detection: The file '{abs_path_file}' imports itself."
419+
else:
420+
errorMsg = f"Cyclic import detection: The file '{abs_path_file}' is imported by '{previousImport1}' and by file '{previousImport2}'."
421+
raise Exception(errorMsg)
412422
oJsonImport = self.jsonLoad(abs_path_file)
413423
if not self.bJSONPreCheck and self.currentNode.parent is not None:
414424
self.currentNode = self.currentNode.parent
@@ -1914,16 +1924,16 @@ def __handleLastElement(sInput : str) -> str:
19141924
if not isinstance(sJsonpContent, str):
19151925
self.__reset()
19161926
raise Exception(f'Expected a string, but got a value of type {type(sJsonpContent)}')
1917-
if self.importTree is None:
1918-
self.importTree = CTreeNode('Root')
1919-
self.currentNode = self.importTree
19201927
# Identifies the entry level when loading JSONP content in comparison with imported files levels.
19211928
firstLevel = True if self.recursive_level==0 else False
19221929
if referenceDir is not None:
19231930
self.jsonPath = CString.NormalizePath(referenceDir, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))
19241931
if not os.path.exists(self.jsonPath):
19251932
self.__reset()
19261933
raise Exception(f"Reference directory '{referenceDir}' is not existing!")
1934+
if self.importTree is None:
1935+
self.importTree = CTreeNode(f'Root:{self.jsonPath}')
1936+
self.currentNode = self.importTree
19271937
if self.masterFile is None or not firstLevel:
19281938
try:
19291939
sJsonData= self.__loadAndRemoveComments(sJsonpContent, isFile=False)
@@ -2096,6 +2106,10 @@ def __handleLastElement(sInput : str) -> str:
20962106
self.recursive_level = 0
20972107
self.bDynamicImport = False
20982108
self.handlingFile = [] if self.masterFile is None else [self.masterFile]
2109+
if not regex.match(f'^Root:.+$', self.importTree.value):
2110+
self.jsonPath = os.path.dirname(self.importTree.value)
2111+
else:
2112+
self.jsonPath = regex.sub(r'(^Root:)', '', self.importTree.value)
20992113
self.importTree.children = {}
21002114
self.currentNode = self.importTree
21012115
self.bJSONPreCheck = False

0 commit comments

Comments
 (0)