25
25
import java .nio .charset .StandardCharsets ;
26
26
import java .nio .file .Files ;
27
27
import java .util .List ;
28
+ import java .util .Map ;
28
29
import javax .annotation .Nullable ;
29
30
import org .sonar .plugins .python .api .PythonCheck ;
30
31
import org .sonar .plugins .python .api .PythonFile ;
@@ -52,6 +53,14 @@ public static PythonVisitorContext scanFile(File file, PythonCheck... visitors)
52
53
return context ;
53
54
}
54
55
56
+ public static PythonVisitorContext scanNotebookFile (File file , Map <Integer , IPythonLocation > locations , String content , PythonCheck ... visitors ) {
57
+ PythonVisitorContext context = createNotebookContext (file , locations , content , "" , ProjectLevelSymbolTable .empty (), CacheContextImpl .dummyCache ());
58
+ for (PythonCheck visitor : visitors ) {
59
+ visitor .scanFile (context );
60
+ }
61
+ return context ;
62
+ }
63
+
55
64
public static PythonVisitorContext createContext (File file ) {
56
65
return createContext (file , null );
57
66
}
@@ -67,6 +76,13 @@ public static PythonVisitorContext createContext(File file, @Nullable File worki
67
76
return new PythonVisitorContext (rootTree , pythonFile , workingDirectory , packageName , projectLevelSymbolTable , cacheContext );
68
77
}
69
78
79
+ public static PythonVisitorContext createNotebookContext (File file , Map <Integer , IPythonLocation > locations , String content , String packageName ,
80
+ ProjectLevelSymbolTable projectLevelSymbolTable , CacheContext cacheContext ) {
81
+ TestPythonFile pythonFile = new TestPythonFile (file );
82
+ FileInput rootTree = parseNotebookFile (locations , content );
83
+ return new PythonVisitorContext (rootTree , pythonFile , null , packageName , projectLevelSymbolTable , cacheContext );
84
+ }
85
+
70
86
public static ProjectLevelSymbolTable globalSymbols (List <File > files , File baseDir ) {
71
87
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable ();
72
88
for (File file : files ) {
@@ -81,9 +97,16 @@ public static ProjectLevelSymbolTable globalSymbols(List<File> files, File baseD
81
97
return projectLevelSymbolTable ;
82
98
}
83
99
100
+ private static FileInput parseNotebookFile (Map <Integer , IPythonLocation > locations , String content ) {
101
+ var parser = PythonParser .createIPythonParser ();
102
+ var treeMaker = new IPythonTreeMaker (locations );
103
+ var astNode = parser .parse (content );
104
+ return treeMaker .fileInput (astNode );
105
+ }
106
+
84
107
private static FileInput parseFile (TestPythonFile file ) {
85
108
var parser = file .isIPython () ? PythonParser .createIPythonParser () : PythonParser .create ();
86
- var treeMaker = file .isIPython () ? new IPythonTreeMaker () : new PythonTreeMaker ();
109
+ var treeMaker = file .isIPython () ? new IPythonTreeMaker (Map . of () ) : new PythonTreeMaker ();
87
110
88
111
var astNode = parser .parse (file .content ());
89
112
return treeMaker .fileInput (astNode );
0 commit comments