-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpython-parser.ts
More file actions
50 lines (42 loc) · 1.3 KB
/
python-parser.ts
File metadata and controls
50 lines (42 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { AbstractParser, EnclosingContext } from "../../constants";
import * as path from "path";
import { execSync } from "child_process"; // Use to execute Python script
import * as fs from "fs";
export class PythonParser implements AbstractParser {
private pythonScriptPath: string;
constructor() {
this.pythonScriptPath = path.resolve(__dirname, "python_parser.py");
}
findEnclosingContext(
file: string,
lineStart: number,
lineEnd: number
): EnclosingContext | null {
try {
const result = execSync(
`python ${this.pythonScriptPath} "${file}" ${lineStart} ${lineEnd}`,
{ encoding: "utf8" }
);
const context = JSON.parse(result);
if (context.error) {
console.error("Error from Python script:", context.error);
return null;
}
return {
enclosingContext: context,
} as EnclosingContext;
} catch (error) {
console.error("Failed to execute Python script:", error.message);
return null;
}
}
dryRun(file: string): { valid: boolean; error: string } {
try {
fs.readFileSync(file, "utf8");
execSync(`python ${this.pythonScriptPath} "${file}" 1 1`);
return { valid: true, error: "" };
} catch (error) {
return { valid: false, error: error.message };
}
}
}